php a-z sort using explode PLEASE NEED HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

php a-z sort using explode PLEASE NEED HELP!

Post by phphelpme »

Hi,I am trying to get a horizontal list of values from a php file into a select list within a form and order them a-z in the dropdown list.

The php file looks like this:

catid,category,catid,category, etc etc

In otherwords: 1111,fruit,1112,veg, etc etc

I am exploding this data into a variable called $load and I have managed to get the values to appear in a list for selection but can not seem to order them by a-z. With each cat name their is a cat id, so this would have to be ordered too. So the id stays with the correct cat name.

Can anyone assist in getting my coding to accomplish this please.

The following code is what I have so far and it shows a dropdown box with the cat name values and the value to pass on is the cat id.

Code: Select all

<form action="<? echo $_SERVER['PHP_SELF']; ?>?sessionid=<?php echo $sessionid; ?>" method="POST">
<br>
<select name="add" id="add">

  <?php      
 
$loadfile = "../add/add.php";
$fh = fopen($loadfile, 'r');
$theData = fread($fh, filesize($loadfile));
fclose($fh);
$load = explode(",", $theData);

for ($counter=0; $counter<count($load); $counter+=2) {
$cnt = $counter+1;
 
echo "<option name='members[]' value='$load[$counter]'>$load[$cnt]</option><br>";

}

?>
</select>
<p>
<center><input type='submit' class='grab' name ='submit' value='Save'></center>
</div>
</form>
Any help would be totally amazing. thanks in advance!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: php a-z sort using explode PLEASE NEED HELP!

Post by Darhazer »

Use the uasort function
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: php a-z sort using explode PLEASE NEED HELP!

Post by phphelpme »

I am not sure how I would use uasort in my coding :/

My php file contains a horizontal line number,name,number,name, etc and I need to organise these still keeping the related values together. i.e. number and name, number and name, number and name.

Looking at the function you mentioned it seems only to be able to sort one column value. I may be wrong but I am saying I dont really understand how I would implement this into my coding.

Are you able to give an example with a simular coding structure to mine at all? It all seems so different on that site link.

Thanks for your reply Darhazer.

Best wishes
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: php a-z sort using explode PLEASE NEED HELP!

Post by phphelpme »

Can anyone else help with this coding as I still am struggling to get the a-z to function. I need to order dropdown list by category name but still keep the associated numeric value id with it. :banghead:
Post Reply