PHP Sort Order????

by 5 replies
6
Hi,

I will buy whoever solves this one a well deserved beer because it is driving me crazy trying to puzzle it out.

Anyway, I have a site created with CubeCart V3 (I know there are better alternatives but I'm stuck with it this time.) While adding a product the categories are displayed in a html option drop down box and the appropriate one is chosen from the list...

The trouble is the categories are displayed in some random order and there are now over 100 categories. You can imagine it is a real chore trying to sort through and find the right one every time a new product is added.

I have found the block of code that queries the database and displays the option drop down but I don't know how to get it to sort in ascending alphabetical order.

Here is the code block:
Code:
<?php for ($i=0; $i<count($categoryArray); $i++){ ?>
<option value="<?php echo $categoryArray[$i]['cat_id']; ?>" 
<?php if(isset($results[0]['cat_id']) && $categoryArray[$i]['cat_id']==$results[0]['cat_id']) { echo "selected='selected'"; } ?>>
<?php echo getCatDir($categoryArray[$i]['cat_name'],$categoryArray[$i]['cat_father_id'], $categoryArray[$i]['cat_id']); ?>
</option>
<?php } ?>
Are there any PHP geniuses out there that can help?

T
#programming #order #php #sort
  • hi

    You have to change the database query in order to get the data returned in alphabetical order
  • hi did u tried the sort function in php

    <?php

    $fruits = array("lemon", "orange", "banana", "apple");
    sort($fruits);
    foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
    }

    ?>

    Hope this helps...please mail me in case of any issues
  • <?php
    sort($categoryArray);
    for ($i=0; $i<count($categoryArray); $i++){ ?> <option value="<?php echo $categoryArray[$i]['cat_id']; ?>" <?php if(isset($results[0]['cat_id']) && $categoryArray[$i]['cat_id']==$results[0]['cat_id']) { echo "selected='selected'"; } ?>> <?php echo getCatDir($categoryArray[$i]['cat_name'],$categoryArray[$i]['cat_father_id'], $categoryArray[$i]['cat_id']); ?> </option> <?php } ?>
    • [1] reply
    • how are your arrays populated?

      if they come from a database query, then make sure your SQL has the ORDER BY clause you want.

      else, sort the array in php as in above examples.
  • Use this code for sorting orders:

    <?php
    // START Pre-sorting (Umlaut -> normal letters)
    $max = count($dat);
    for($totcnt = 0; $totcnt < $max; $totcnt++){
    $dat[$totcnt]=str_replace('&szlig;','ss_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&Auml;','Ae_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&auml;','ae_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&Ouml;','Oe_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&ouml;','oe_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&Uuml;','Ue_',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('&uuml;','ue_',$dat[$totcnt]);
    }
    // END Pre-sorting (Umlaut -> normal letters)

    // START Sorting //
    function compare_towns($a, $b)
    {
    return strnatcmp($a, $b);
    }
    usort($dat, 'compare_towns');
    // END Sorting //

    // START Post-sorting (normal letters -> Umlaut)
    for($totcnt = 0; $totcnt < $max; $totcnt++){
    $dat[$totcnt]=str_replace('ss_','&szlig;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('Ae_','&Auml;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('ae_','&auml;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('Oe_','&Ouml;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('oe_','&ouml;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('Ue_','&Uuml;',$dat[$totcnt]);
    $dat[$totcnt]=str_replace('ue_','&uuml;',$dat[$totcnt]);
    }
    // END Post-sorting (normal letters -> Umlaut)
    ?>
  • Banned
    [DELETED]

Next Topics on Trending Feed