Page 1 of 1

php a-z sort using explode PLEASE NEED HELP!

Posted: Sun Jan 02, 2011 12:41 pm
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!

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

Posted: Sun Jan 02, 2011 2:10 pm
by Darhazer
Use the uasort function

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

Posted: Sun Jan 02, 2011 3:39 pm
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

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

Posted: Wed Jan 05, 2011 5:10 pm
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: