1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Sort Order?

Discussion in 'PHP' started by Amsterdam, Jul 12, 2011.

  1. #1
    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:

    <?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 } ?>
    Code (markup):

    Are there any PHP geniuses out there that can help?

    T
     
    Amsterdam, Jul 12, 2011 IP
  2. gviger

    gviger Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this

    <?php
    
    	for ($i=0; $i<count($categoryArray); $i++){
    		$categoryOrdered[ strval($i)] = $categoryArray[$i]['cat_name'];
    	}
    	asort($categoryOrdered);
    ?>
    
    	<?php foreach ($categoryOrdered as $key => $val){ 
    $i = intval( $key );
    ?>
    	<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 } ?>
    Code (markup):
    It will sort by 'cat_name'
     
    Last edited: Jul 12, 2011
    gviger, Jul 12, 2011 IP
    Amsterdam likes this.
  3. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Thanks for the hlep gviger. Your code looks flawless and I can't see any reason why it shouldn't work but when I edited the file and uploaded it didn't make a jot of difference to the sort order :-(

    I'm guessing that there must be something else overiding it????

    T
     
    Amsterdam, Jul 12, 2011 IP
  4. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Ok, I've sorted it now. I was editing the wrong area of the file :&

    Much thanks to gviger - Please PM me your Paypal email so I can buy you a beer.

    T
     
    Amsterdam, Jul 13, 2011 IP