The solution I posted:

    echo '<OPTION

did indeed take the first letter of $squads[1] as option value. I wondered
at the time why Chad would want the first letter of the name as option value
(that was his original code); I didn't realise that he wanted the id as
option value.

This code works fine:

        $fd = fopen ($teams, "r");
        while (!feof ($fd))
                $list = fgets($fd, 4096);
                $squads = explode("|", $list);
                $alphabetical[$squads[1]] = $squads[0];
        fclose ($fd);
        ksort ($alphabetical);
        echo "<select>";
        foreach ($alphabetical as $name => $id)
                echo '<OPTION VALUE="'.$id.'">'.$name.'</OPTION>'."\n";
        echo "</select>";

(I think it is Marek's code - I added the <select>).

Building on the above code and if you you wanted to have every column in an
array: Instead of having squad_compares, usorts and the like, why not just
link on id to associative arrays for each column, e.g.

        $fd = fopen ($teams, "r");
        while (!feof ($fd))
                $list = fgets($fd, 4096);
                $squads = explode("|", $list);
                $alphabetical[$squads[1]] = $squads[0];
                $stadium[$squads[0]] = $squads[2];
                $city[$squads[0]] = $squads[3];
        fclose ($fd);
        ksort ($alphabetical);
        echo "<select>";
        foreach ($alphabetical as $name => $id)
                echo '<OPTION VALUE="'.$id.'">'.$name." -
        echo "</select>";

If you wanted to access the appropriate data you would use $stadium[$id] if
you had the id, or $stadium[$alphabetical[$name]] if you had the name.


Rudolf Visagie
Principal Software Developer
Digital Healthcare Solutions
Tel: 011 6901019
Cell: 082 895 1598

-----Original Message-----
From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, September 18, 2002 12:21 PM
To: 'Chad Winger'; [EMAIL PROTECTED]
Subject: RE: [PHP] Help.....still stuck after 3 days :/

> -----Original Message-----
> From: Chad Winger [mailto:[EMAIL PROTECTED]]
> Sent: 18 September 2002 02:32
> however there is a little problem with the end result.
> your code:
> <?php
>   include ("C:\Program
> Files\EasyPHP\www\florentiaviola.com\control\config.php");
>   $fd = fopen ($teams, "r");
>   while (!feof ($fd))
>   {
>    $list = fgets($fd, 4096);
>    $squads = explode("|", $list);
>    $alphabetical[] = $squads[1];
>   }
>    sort ($alphabetical);
>    reset ($alphabetical);
>    for ($i = 0; $i <= count($alphabetical) - 1; $i++)
>    {
>    echo '<OPTION VALUE="'.
> $alphabetical[$i][0].'">'.$alphabetical[$i].'</OPTION>'."\n";
>    }
>    fclose ($fd);
> ?>

The problem with this script is it only sorts the team names, without
re-ordering the other columns of your array to match.  And the notation
$alphabetical[$i][0] is actually accessing the first character of the string
(tema name) in $alphabetical[$i] -- hence the result you see.

What you need to do is create an array or arrays containing all the content
you need to sort, and then sort them in parallel based on the team names.

My instinct would be to build a nested array of your values thusly:

   while (!feof ($fd))
    $list = fgets($fd, 4096);
    $squads[] = explode("|", $list);

which would build an array like this:

   $squads[0][0] => 01
   $squads[0][1] => Brescello
   $squads[0][2] => stadium
   $squads[0][3] => city
   $squads[1][0] => 02
   $squads[1][1] => Aglianese
   $squads[1][2] => stadium
   $squads[1][3] => city
   $squads[2][0] => 03
   $squads[2][1] => Casteldisangro
   $squads[2][2] => stadium
   $squads[2][3] => city


However, a little research in the PHP online manual looking for sorting
functions reveals that there's no simple function to sort this array the way
you want. There are two ways around this:

1. Build your arrays differently; whilst this is possible, it needs more
work and is not nearly as elegant (unless you need them this way for other
stuff in your script).  I'll leave this as an exercise for the reader, as
there at least 2 possible answers, but the sorting functions you might use
are asort() or array_multisort().

2. Use a more complex sorting method that sorts the above array as you want.
This is not actually as hard as it sounds, as there are several user notes
in the online manual under the entry for usort() that address exactly this
problem.  Basically, you need to define a function that says how to compare
two $squads[] rows for sorting -- in this case, by comparing the
$squads[][1] entries, so:

  function squad_compare($a, $b) // $a, $b each passed a $squads[] row
     return strcasecmp($a[1], $b[1]);  // returns <0 if $a sorts first,
                                       // 0 if equal, >0 if $b sorts first
                                       // (case-INsensitive comparison)

and then use this in a usort() (sort with user-defined comparison) call:

  usort($squads, 'squad_compare');

I've broken this down into component steps for easier understanding, but you
can actually do it all-in-one if you don't need to use the compare function
elsewhere; again, this is adapted from a user note on the usort() manual

   usort($squads, create_function('$a,$b', 'return strcasecmp($a[1],

Finally, you can iterate through the results and generate your form:

  foreach($squads as $squad_info):
    echo '<OPTION

Hope this helps somewhat!



Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to