RE: [PHP] Help.....still stuck after 3 days :/

2002-09-18 Thread Rudolf Visagie

Hi,

The solution I posted:

echo ''.$alphabetical[$i].''."\n";

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 "";
foreach ($alphabetical as $name => $id)
{
echo ''.$name.''."\n";
}
echo "";

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


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 "";
foreach ($alphabetical as $name => $id)
{
echo ''.$name." -
".$stadium[$id].''."\n";
}
echo "";

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.

Cheers

Rudolf Visagie
Principal Software Developer
Digital Healthcare Solutions
<mailto:[EMAIL PROTECTED]>
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:
> 
>  
>   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 ''.$alphabetical[$i].''."\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 

RE: [PHP] Help.....still stuck after 3 days :/

2002-09-18 Thread Ford, Mike [LSS]

> -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:
> 
>  
>   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 ''.$alphabetical[$i].''."\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 ''.$squad_info[1].''."\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




Re: [PHP] Help.....still stuck after 3 days :/

2002-09-18 Thread Marek Kilimajer

I would do it this way (it fails if some names are same):

Chad Winger wrote:

>Hello Rudolf,
>Thank you for responding to my post, I really appreciate it.
>
>I ran the script you sent me, and then I tried looking up the syntax to try
>to figure out what it means. As it would be pointless for me just to take
>suggestions that people send me and then not learn what they mean. The
>resulkt of your code does in fact alphabatize the teams in the select list,
>however there is a little problem with the end result.
>
>your code:
>
>
>  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]] = $squads[0];
>  }
>   fclose ($fd);
>
>   ksort ($alphabetical);
>  
>
>   foreach ($alphabetical as $name => $id)
>   {
>   echo ''.$name.''."\n";
>   }
>
>
>
>?>
>
>returns the follwoing html:
>Aglianese
>Brescello
>Casteldisangro
>Castelnuovo
>Fano
>Florentia Viola
>Forlě
>Grosseto
>Gualdo
>
>
>It is returning the first letter of $squads[1]. so the "id variable is
>getting lost.That variable is $squads[0]. So in effect that vairable is
>getting "lost" because when i modify the line
>
>echo ''.$alphabetical[$i].''."\n";
>
>to read
>
>echo ''.$alphabetical[$i].''."\n";
>
>then the HTML output becomes
>
>Aglianese
>Brescello
>Casteldisangro
>Castelnuovo
>Fano
>Florentia Viola
>Forlě
>Grosseto
>Gualdo
>
>In other words what I am trying to do is something to the effect of
>$alphabetical = $squads[1][0]. Then I want to sort that only by $squads[1].
>When the html is returned, I want to be able to echo the $squads[0] as the
>Value of the select dropdown.
>
>Thanks again,
>-Tree
>
>
>  
>


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




[PHP] Help.....still stuck after 3 days :/

2002-09-17 Thread Chad Winger

Hello Rudolf,
Thank you for responding to my post, I really appreciate it.

I ran the script you sent me, and then I tried looking up the syntax to try
to figure out what it means. As it would be pointless for me just to take
suggestions that people send me and then not learn what they mean. The
resulkt of your code does in fact alphabatize the teams in the select list,
however there is a little problem with the end result.

your code:

'.$alphabetical[$i].''."\n";
   }
   fclose ($fd);


?>

returns the follwoing html:
Aglianese
Brescello
Casteldisangro
Castelnuovo
Fano
Florentia Viola
Forlì
Grosseto
Gualdo


It is returning the first letter of $squads[1]. so the "id variable is
getting lost.That variable is $squads[0]. So in effect that vairable is
getting "lost" because when i modify the line

echo ''.$alphabetical[$i].''."\n";

to read

echo ''.$alphabetical[$i].''."\n";

then the HTML output becomes

Aglianese
Brescello
Casteldisangro
Castelnuovo
Fano
Florentia Viola
Forlì
Grosseto
Gualdo

In other words what I am trying to do is something to the effect of
$alphabetical = $squads[1][0]. Then I want to sort that only by $squads[1].
When the html is returned, I want to be able to echo the $squads[0] as the
Value of the select dropdown.

Thanks again,
-Tree



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