> -----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

etc.

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:

123456789*123456789*123456789*123456789*123456789*123456789*123456789*123456
  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
page:

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

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

  foreach($squads as $squad_info):
    echo '<OPTION
VALUE="'.$squad_info[0].'">'.$squad_info[1].'</OPTION>'."\n";
  endforeach;

Hope this helps somewhat!

Cheers!

Mike

---------------------------------------------------------------------
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
Email: [EMAIL PROTECTED]
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

Reply via email to