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.. 

Monday, March 7, 2011

Generate Moodle password

For generating a moodle password , first of all include the the configuration page then use the script given below
$password = “new password”;
$moodle_password = md5($password.$CFG->passwordsaltmain);
$password is the actual password and the passwordsaltmain is a hash key generated at moodle installation.

Avactis : Tax value is not displayed on checkout step 4

In avactis the 4th step of the checkout process that is
the landing page after payment the vat not displayed.
To solve this issue please, replace the following part of code:
default:
break;
}

foreach ($tax['TaxSubtotalAmountView'] as $taxView)
{
$this->_Tax_Item['Local_TaxName']=prepareHTMLDisplay($taxView['view']);

with this one:

default:
break;
}
$lastPlacedOrderID = modApiFunc('Checkout','getLastPlacedOrderID');
if ($lastPlacedOrderID !== NULL)
{
$oi = modApiFunc('Checkout', 'getOrderInfo', $lastPlacedOrderID,
modApiFunc('Localization',
'whichCurrencyToDisplayOrderIn',
$lastPlacedOrderID));
$tax['TaxSubtotalAmountView'] = array();
foreach($oi['Price']['tax_dops'] as $tax_info)
$tax['TaxSubtotalAmountView'][] = array(
'view' => preg_replace("/:$/", '', $tax_info["name"]),
'value' => $tax_info['value']
);
}
foreach ($tax['TaxSubtotalAmountView'] as $taxView)
{
$this->_Tax_Item['Local_TaxName']=prepareHTMLDisplay($taxView['view']);

in the"avactis-system/modules/checkout/views/checkout-order-cz.php" 
file.

Add new fields in joomla registration form

Let us add a custom field phone and mobile in registration form
libraries\joomla\database\table\user.php it’s the class file tor user table/user object
function __construct( &$db )
//add new field
var $phone = null;
var $mobile= null;
after that open
libraries\joomla\libraries\joomla\user\user.php
//add this code
var $phone = null;
var $mobile= null;
Add two fields in users table
ALTER TABLE jos_users ADD phone varchar(255) DEFAULT '' 
AFTER password;
ALTER TABLE jos_users ADD mobile varchar(255) DEFAULT '' 
AFTER phone;

Now go to Now go to administrator\components\com_users\views\
user\tmpl

<tr>
<td width="150">
<label for="phone">
 <?php echo JText::_( 'Phone' ); ?>
</label>
</td>
<td>
<input type="text" name="phone" id="phone" size="40"
 value="<?php echo $this->user->get('phone'); ?>" />
</td>
</tr>
<tr><td width="150">
<label for="mobile">
<?php echo JText::_( 'Mobile' ); ?>
</label>
</td>
<td>
<input type="text" name="mobile" id="mobile" size="40"
value="<?php echo $this->user->get('mobile'); ?>" />
</td>
</tr>
Also in components\com_user\views\register\tmpl
<tr>
<td height="40">
<label id="phonemsg" for="phone">
<?php echo JText::_( 'Phone' ); ?>:
</label>
</td>
<td>
<input type="text" name="phone" id="phone" size="40"
 value="<?php echo $this->escape($this->user->get( 'phone' ));?>" 
maxlength="50" /> *
</td>
</tr>
<tr>
<td height="40">
<label id="websitemsg" for="mobile">
<?php echo JText::_( 'Mobile' ); ?>:
</label>
</td>
<td>
<input type="text" name="mobile" id="mobile" size="40"
value="<?php echo $this->escape($this->user->get( 'mobile' ));?>" 
maxlength="50" /> *
</td>
</tr>