5 replies
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
#order #php #sort
  • Profile picture of the author timvaquera
    hi

    You have to change the database query in order to get the data returned in alphabetical order
    {{ DiscussionBoard.errors[4244447].message }}
  • Profile picture of the author weaveronline
    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
    Signature

    Thanks & Regards,
    Reach us at dukeduke600@gmail.com.
    Web Design| Logo Design | Banner Design | Web Development | Mobile Applications [iPhone/iPad/Android/Windows Phone]

    {{ DiscussionBoard.errors[4244551].message }}
  • Profile picture of the author ussher
    <?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 } ?>
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[4249222].message }}
    • Profile picture of the author ohio1975
      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.
      {{ DiscussionBoard.errors[4257381].message }}
  • Profile picture of the author paulleon
    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)
    ?>
    {{ DiscussionBoard.errors[4257899].message }}

Trending Topics