Pages

Wednesday, May 25, 2011

PHP dynamic navigation

Here is the code for dynamic nested navigation system.

Step1: Need to create a data base table " dyn_menu "
------------------------------------------------------------
CREATE TABLE `dyn_menu` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `label` VARCHAR(50) NOT NULL DEFAULT '',
  `parent_id` INT(11) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

------------------------------------------------------------

Step2: PHP script to generate menu system

------------------------------------------------------------

$sel = "select * from dyn_menu";
$res = mysql_query($sel);
while($dat = mysql_fetch_assoc($res))
{
    if($dat["parent_id"] == 0)
    {
        $parent_array[$dat["id"]]["label"] = $dat["label"];
    }
    else
    {
        $sub_array[$dat["id"]]["label"] = $dat["label"];
        $sub_array[$dat["id"]]["parent"] = $dat["parent_id"];
        $parent_array[$dat["parent_id"]]["count"]++;
    }
}




foreach($parent_array as $pkey => $pval)
{
    echo "<ul>";
    if(!isset($pval["count"]))
    {
        echo "<li>". $pval["label"] ."</li>";
    }
 else
    {  
     echo "<li>". $pval["label"] ."</li>";
        echo "<ul>";
       
        foreach($sub_array as $skey => $sval)
        {
            if($sval["parent"] == $pkey)
            {
                echo "<li>". $sval["label"] ."</li>";
            }
        }       
        echo "</ul>";
    }
    echo "</ul>";
}


-----------------------------------------------------

** I think it will help you, this is an example for level-1 sub menu system . For increasing menu level put the php script in recursive function and make necessary changes.

Tuesday, May 24, 2011

Extend / Modify Advanced Search Form in Drupal 6

We can easily extend/ modify advanced search form by editing  " node_form_alter " function on node.module file ( modules/node/node.module ).




<?php
function node_form_alter($form_id, &$form) {
  // Advanced node search form
  if ($form_id == 'search_form' && arg(1) == 'node' && user_access('use advanced search')) {
    // Keyword boxes:
    $form['advanced'] = array(
      '#type' => 'fieldset', 
      '#title' => t('Advanced search'), 
      '#collapsible' => TRUE, 
      '#collapsed' => TRUE, 
      '#attributes' => array('class' => 'search-advanced'),
    );
    $form['advanced']['keywords'] = array(
      '#prefix' => '<div class="criterion">', 
      '#suffix' => '</div>',
    );
    $form['advanced']['keywords']['or'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing any of the words'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['phrase'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing the phrase'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['negative'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing none of the words'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );

    // Taxonomy box:
    if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
      $form['advanced']['category'] = array(
        '#type' => 'select', 
        '#title' => t('Only in the category(s)'), 
        '#prefix' => '<div class="criterion">', 
        '#size' => 10, 
        '#suffix' => '</div>', 
        '#options' => $taxonomy, 
        '#multiple' => TRUE,
      );
    }

    // Node types:
    $types = array_map('check_plain', node_get_types('names'));
    $form['advanced']['type'] = array(
      '#type' => 'checkboxes', 
      '#title' => t('Only of the type(s)'), 
      '#prefix' => '<div class="criterion">', 
      '#suffix' => '</div>', 
      '#options' => $types,
    );
    $form['advanced']['submit'] = array(
      '#type' => 'submit', 
      '#value' => t('Advanced search'), 
      '#prefix' => '<div class="action">', 
      '#suffix' => '</div>',
    );

    $form['#validate']['node_search_validate'] = array();
  }
}
?>

Monday, May 23, 2011

Getting the time difference from two different days using date function

$startTime = "2011-01-10 12:10:46";
$endTime = "2011-03-15 10:39:09";

$timeDiff = floor((strtotime($endTime) - strtotime($startTime))/(60));
$timeStandard = ($timeDiff == 1)?"Minute":"Minutes";
if($timeDiff >= 60)
{
    $timeDiff = floor($timeDiff/60);
    $timeStandard = ($timeDiff == 1)?"Hour":"Hours";
   
    if($timeDiff >= 24)
    {
        $timeDiff = floor($timeDiff / 24);
         $timeStandard = ($timeDiff == 1)?"Day":"Days";
       
        if($timeDiff >= 7)
        {
            $timeDiff = floor($timeDiff / 7);
             $timeStandard = ($timeDiff == 1)?"Week":"Weeks";
           
            if($timeDiff >= 4)
            {
                $timeDiff = floor($timeDiff / 4);
                 $timeStandard = ($timeDiff == 1)?"Month":"Months";
            }
           
        }
    }
}

echo $timeDiff." ".$timeStandard;

Sunday, May 22, 2011

Example for jQuery AJAX post

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script>
   $(document).ready(function(){
        $("#loading").ajaxStart(function(){
   $(this).show();
 });
        $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

$("#loading").ajaxStop(function(){
      $(this).hide();
      });

   });
</script>

<div id="loading" style="display: none;">Loading...</div>

Wednesday, May 18, 2011

Get facebook friends list in an array

Here is the solution for getting facebook friends list in an array usinbg PHP

First of all you need to download latest facebook php sdk from https://github.com/facebook/php-sdk/downloads
 Then include facebook.php from the sdk directory to your php page.

 After that use below code to generate friends list in a array
 --------------------------------------------------------------------------------------------------------
$facebook = new Facebook(array(
  'appId'  => 'APP-ID',
  'secret' => 'APP-SECRET-KEY',
  'cookie' => true,
));

$friends = $facebook->api('/'. FACEBOOK-USER-ID .'/friends');
                $friendsList = array();
                foreach ($friends as $key=>$value)
                {
                   foreach ($value as $fkey=>$fvalue) {

                       $friendsList[] = $fvalue[id];
                   }

                }


print_r($friendsList);
 --------------------------------------------------------------------------------------------------------

Remove scroll bars from facebook iframe application

Step 1: Make "IFrame Size" to "Auto-resize" on the application settings page.

Step 2: Insert below code just before closing the head tag ( </head> ) on the child page.

<script type="text/javascript">
window.fbAsyncInit = function() {
FB.Canvas.setSize();
}
// Do things that will sometimes call sizeChangeCallback()
function sizeChangeCallback() {
FB.Canvas.setSize();
}
</script>


Step 3: Insert the below code just before closing body tag ( </body> ).


<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR-APP-ID-HERE',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>

** Please make sure to put your appid correctly.

Monday, March 21, 2011

Joomla Password generation

Use the below code for generating joomla password.

        $new_password = "Your password";
       $salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($new_password, $salt);
        $password = $crypt.':'.$salt;

I hope it will help you..