Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Array Sorting

Status
Not open for further replies.

YoungManRiver

IS-IT--Management
Feb 9, 2004
220
US
All,

Solved my dBase problem from:


only to find my list is unsorted. PHP manual says use "asort" or "ksort" but not having any luck. Please look at my code and let me know what is needed to get my sort working.

Code:
<?php
   require('db_connect.php');
   $sql = "SELECT cli_cnm FROM client ORDER BY cli_cnm;";
   $result = mysql_query($sql);
   while ($row = mysql_fetch_assoc($result)) {
      $rw_vl_cl[] = $row['cli_cnm'];
   }
   $af = 'C:\ACT\myact.dbf';        //LapTop
   $db = dbase_open($af, 0);
   $outline[] = array();
   $comp_list="";
   $k = 0;
   if ($db) {
      $record_numbers = dbase_numrecords($db);
      echo "G rec cnt => $record_numbers<br>";
      for ($i = 1; $i <= $record_numbers; $i++) {
          $row = dbase_get_record_with_names($db, $i);
          foreach ($row as $key => $val)  {
             if ($key == 'COMPANY') {
                if (!array_search($val, $rw_vl_cl)) {
                   if (!array_search($val, $outline[$k])) {
                      $k++;
                      $outline[$k] = array("comp" => $val,"rowno" => $i);
                   }
                }
             }    /* end if($key)   */
          }       /* end foreach    */
      }           /* end for($i)    */
   }              /* end if($db)    */
   dbase_close($db);
//   asort($outline);
   ksort($outline);
   echo "Set count => $k";
   for ($i = 1; $i <= $k; $i++) {
       $rw = $outline[$i]['rowno'];
       $vl = $outline[$i]['comp'];
       $comp_list .= "<option value=$rw>$vl</option>";
   }
?>

Thanks!

YMR
 
I see that you're accessing the array with numeric indexes. If that's the case, why are you using asort() and ksort()? The ksort() function sorts by key, which isn't going to do anything in your case. The asort() function sorts while retaining key values, so while it does change the underlying order of the array, you won't see it if you access the elements with numeric indexes.

Try using plain-old sort(), which will reset the indexes. Or, if those particular numeric indexes are important for some reason, you could also use asort() and change the final for loop to a foreach loop. That would give you the elements in the underlying order.
 
AdaHacker,

Ur idea worked sorta. Most of the companies in the file start with Capital letters for first letter, but there are some case where branding has the first letter a lower case letter and with just the sort command they sort out at the bottom.

How would I fix this?

YMR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top