RE: [PHP] Re: alphabetical order?
| From: | Rudolf Visagie | Date: | Mon, 16 Sep 2002 13:31:31 +0000 |
| Subject: | RE: [PHP] Re: alphabetical order? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-116515@lists.php.net to get a copy of this message | ||
Hi Chad,
I think you want to put the first letter of each line's squads[1] in an
array $alphabetical and then you want to sort the array. At the moment you
put 'B' in $alphabetical, then try to sort that and write the select option.
Then you put 'A' in $alphabetical, try to sort that and write the select
option. You get an error message
<b>Warning</b>: Wrong datatype in sort() call in <b>...squads.php</b> on
line <b>xx</b><br>
which you can't see because it is included in the <select> structure. View
the source code and you'll see it.
You need to read the whole input file and put the first letter in an array
for each line before you sort $alphabetical, and then write out the <select>
options values from the sorted array. At least, I think that's what you want
to do. Maybe something like this:
<body>
<?php
$teams = "squads";
$fd = fopen ($teams, "r");
while (!feof ($fd))
{
$list = fgets($fd, 4096);
$squads = explode("|", $list);
$alphabetical[] = $squads[1];
}
sort ($alphabetical);
echo "<select>";
for ($i = 0; $i <= count($alphabetical) - 1; $i++)
{
echo '<OPTION
VALUE="'.$alphabetical[$i][0].'">'.$alphabetical[$i].'</OPTION>'."\n";
}
echo "</select>";
fclose ($fd);
?>
</body>
Cheers
Rudolf Visagie
Principal Software Developer
Digital Healthcare Solutions
<mailto:rudolf@dhsolutions.co.za>
Tel: 011 6901019
Cell: 082 895 1598
-----Original Message-----
From: Chad Winger [mailto:cwinger@callistonet.com]
Sent: Monday, September 16, 2002 2:31 PM
To: php-general@lists.php.net
Subject: [PHP] Re: alphabetical order?
Ok lets see if i follow, cuz this isn't working for me. I think I'm missing
something conceptually. Someone tell me when I mistake.
//this is example of the text in the file
01|Brescello|stadium|city|
02|Aglianese|stadium|city|
//this part of the script reads each line, takes out the | and creates the
array $squads
$list = fgets($fd, 4096);
$squads = explode("|", $list);
//so now in theory I would have the following
$squads[0] = 01
$squads[1] = Brescello
$squads[2] = stadium
$squads[3]= city
//now i need a new array $alphabetical
$alphabetical = $squads[1][0];
//now i need to sort $alphabetical
sort ($alphabetical);
//so now I'm guessing I would have the following
$alphabetical[0] = $squads[1][0];
so here is my final code
<html>
<body>
<select>
<?php
include ("C:\path\to\config.php");
$fd = fopen ($teams, "r");
while (!feof ($fd))
{
$list = fgets($fd, 4096);
$squads = explode("|", $list);
$alphabetical[] = $squads[1][0];
sort ($alphabetical);
echo '<OPTION
VALUE="'.$alphabetical[0].'">'.$squads[1].'</OPTION>'."\n";
}
fclose ($fd);
?>
</select>
</html>
</body>
When I run that i get this:
<OPTION VALUE="B">Brescello</OPTION>
<OPTION VALUE="A">Aglianese</OPTION>
plus the error message sort() expects parameter 1 to be array
Im obviously missing a step or 20 :(
I've tried different things following the manual as per your suggestions,
such as array_push and ksort, but im just getting more and more errors..any
more suggestions?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php