PDA

View Full Version : Alphabetical order? Help!


Treetaliano
09-15-2002, 11:20 AM
If you've read some of my earlier posts, you know I'm a newbie at PHP, so now im stuck at a point, and I'd like to see if anyone has a suggestion.

Ok, here is the problem...i have a text file on the server, and in it are listed lines like this:

01|Brescello|stadium|city|
02|Aglianese|stadium|city|
03|Casteldisangro|stadium|city|

and the following code is part of a php file that creates a form.

<SELECT NAME="home">
<?php

include ("C:\path\to\config.php");

$fd = fopen ($teams, "r");
while (!feof ($fd))
{
$readlines = fgets($fd, 4096);
$squads = explode("|", $readlines);
if ($squads[1] == "Florentia Viola") {
echo '<OPTION SELECTED VALUE="'.$squads[0].'">Florentia
Viola</OPTION>'."\n";
} else {
echo '<OPTION VALUE="'.$squads[0].'">'.$squads[1].'</OPTION>'."\n";
}

}
fclose ($fd);

?>
</SELECT>

What is does is create options in a dropdown menu. Right now the outputted
HTML would be

<OPTION VALUE="01">Brescello</OPTION>
<OPTION VALUE="02">Aglianese</OPTION>
<OPTION VALUE="03">Casteldisangro</OPTION>

Problem is that I would like it alphabatized, like so:


<OPTION VALUE="02">Aglianese</OPTION>
<OPTION VALUE="01">Brescello</OPTION>
<OPTION VALUE="03">Casteldisangro</OPTION>

so aftter looking around a bit in the manual, I see this:

<?php

$order = array ($squads[1]);
sort ($order);
reset ($order);
while (list ($key, $val) = each ($order)) {
echo "order[".$key."] = ".$val."\n";


?>

so the array $order is declared, then is sorted. To me that means that I
have to declare each fruit in the array. In my array $squads in the example
below [0] is id number [1] is team name [2] is stadium and [4] is city. I
want to alphabatize only the team name i.e. $squads[1] then after the team
name is alphabatized (sorted) then the if...else statement is run ... so
this is what i tried

<html>
<body>
<select>
<?php

include ("C:\path\to\config.php");

$fd = fopen ($teams, "r");
while (!feof ($fd))
{
$readlines = fgets($fd, 4096);
$squads = explode("|", $readlines);
$order = array ($squads[1]);
sort ($order);
reset ($order);
while (list ($key, $val) = each ($order)) {
if ($squads[1] == "Florentia Viola") {
echo '<OPTION SELECTED VALUE="'.$squads[0].'">Florentia
Viola</OPTION>'."\n";
} else {
echo '<OPTION VALUE="'.$squads[0].'">'.$squads[1].'</OPTION>'."\n";
}
}

}
fclose ($fd);

?>
</html>
</body>
</select>


I still get the same result as before.
What am I doing wrong??

Thanks again

-Tree

mordred
09-16-2002, 12:36 AM
I haven't got the time to make up a good example from your snippets, but nevetheless I want to comment:

To me it seems like your reading the text file line by line, and you do the sorting of the elements in the same moment when you read that line in - which is not what you want to achieve.

Try do tweak your codes by these tips:

1. Create an array variable outside of your while loop that reads from the file.

2. After exploding the contents of each line, create a new array element where the sorting criteria (the team element) is the key and the rest you need is the value of that array element.

3. After you're finished with reading from the file, use ksort() to sort your array by its keys and then iterate over it and print the desired HTML output.

hth