Month: October 2011

PHP natsort (Natural Sort) MultiDimension Array popped with Mysql Data

Hi Guys

Don’t ask me how I did that, but this method is working fine for me. We often have problems in sorting the multidimension array in natural way. This includes when we sort list of postcodes. ForExample I have a list of postcodes in my database and when I sort them ASC they appear as

AB1
AB10
AB12
AB2
AB21
AB22

This is not ideal as we want AB1, AB2, AB10, AB12, AB21

SO I wrote this piece of code to sort my data

$records=$this->postcodes_list;	


$keys = array();
$values = array();
$keys[]='0';


foreach ($records as $index => $row) {
//   $key = $row->postcode_s.'__'.$index; // Add the index to create an unique key.
   $key = $row->postcode_s; ////postcode_s is the column name which contains the postcodes
   $keys[] = $key;
   $values[$key] = $row; 
}

natsort($keys);///this method naturally sort the keys
$sortedValues = array();
foreach($keys as $index) {
  $sortedValues[] = $values[$index]; 
}


/*foreach ($sortedValues as $i=>$a){
	echo "Postcode : $a, Index: $i 
"; } */ foreach ($keys as $i=>$a){ echo "Postcode : $a, Index: $i
"; }

$records=$this->postcodes_list; ///Contains the list of postcodes

Pagination in Your Joomla Component

While I was working today to fix how to sort the pagination in my Joomla component, I found this way of pagination to work for me. Hope this will help you too   First of all I edited my Model which interacts with database. I made a function to display some content from the database which is as follows

LOCATION /com_findengg/models/findengg.php

 function displayDeadRegions()
/* THIS CODE IS TO BE MODIFIED AS PER YOUR MODEL. THE ONLY THING I CHANGED IN MY MODEL IS I ADDED $mainframe variable $limit  $limitstart and modified the query */

{
   global $mainframe;
   $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); 
   $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); 
   $db =& JFactory::getDBO();
   $query = 'SELECT * FROM `jos_findmyengineer_dead` ORDER BY `jos_findmyengineer_dead`.`date_logged` DESC ';
   $db->setQuery( $query, $limitstart, $limit );
   if( !$db->query() ) 
      { $errorMessage = $this->getDBO()->getErrorMsg();
        JError::raiseError( 500, 'Error in GETTING List of Dead RegionS: ' . $errorMessage ); 
      } 
   $dead_regions = $db->loadObjectList(); // Return the list of product types return $dead_regions; }///end of function dead regions  

In the same model I made another function to get the page parameters

LOCATION /com_findengg/models/findengg.php


  function getPageNavParameters($tablename) {
    global $mainframe;
    $db =& JFactory::getDBO(); 
    $table = $db->nameQuote( $tablename ); 
    $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); 
    $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); 
    $query = "SELECT COUNT(*) FROM " . $table; 
    $db->setQuery( $query ); 
    $total = $db->loadResult();
    jimport('joomla.html.pagination');
    $pageNav = new JPagination( $total, $limitstart, $limit ); 
    return $pageNav; 
    }//end of function getPageNavParameters 

Now I edited the view.html.php file. The place where I was making call to my layout, I called this function as follows

LOCATION /com_findengg/views/findengg/view.html.php

 
          $layout = JRequest::getVar( 'layout', 'default' ); 
          $this->setLayout($layout); 
          switch ($layout) { 
            case deadregions:{ 
                              $dead_regions=$model->displayDeadRegions();
                              $this->assignRef( 'dead_regions', $dead_regions);
                              $db_table_name='jos_findmyengineer_dead'; 
                              $pageNav=$model->getPageNavParameters($db_table_name);
                              $this->assignRef( 'pagination', $pageNav); 
                              break; } 
            default:{ 
                            } 
       } parent::display( );
    }
 } 

Finally in my template layout file in folder tmpl I added this code in the table

LOCATION /com_findengg/views/tmpl/deadregions.php

 	
	 echo pagination->getListFooter(); 

Handy Php scripts

echo "<script>alert('Record Not found');</script>";
echo "<SCRIPT LANGUAGE='javascript'>location.href='inbound.php';</SCRIPT>";
echo "<SCRIPT LANGUAGE='javascript'>location.reload(true);</SCRIPT>";

$sql = "SELECT * FROM `items` WHERE `item_barcode` = '$item_barcode' ";
$result= jms_mysql_query($sql , $db_link);
$no_of_rows=mysql_num_rows($result);