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:

?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]] = $squads[0];
  }
   fclose ($fd);

   ksort ($alphabetical);
  

   foreach ($alphabetical as $name = $id)
   {
   echo 'OPTION VALUE='.$id.''.$name.'/OPTION'.\n;
   }



?

returns the follwoing html:
OPTION VALUE=AAglianese/OPTION
OPTION VALUE=BBrescello/OPTION
OPTION VALUE=CCasteldisangro/OPTION
OPTION VALUE=CCastelnuovo/OPTION
OPTION VALUE=FFano/OPTION
OPTION VALUE=FFlorentia Viola/OPTION
OPTION VALUE=FForl/OPTION
OPTION VALUE=GGrosseto/OPTION
OPTION VALUE=GGualdo/OPTION


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 'OPTION VALUE='.
$alphabetical[$i][0].''.$alphabetical[$i].'/OPTION'.\n;

to read

echo 'OPTION VALUE='. $squads[0].''.$alphabetical[$i].'/OPTION'.\n;

then the HTML output becomes

OPTION VALUE=19Aglianese/OPTION
OPTION VALUE=19Brescello/OPTION
OPTION VALUE=19Casteldisangro/OPTION
OPTION VALUE=19Castelnuovo/OPTION
OPTION VALUE=19Fano/OPTION
OPTION VALUE=19Florentia Viola/OPTION
OPTION VALUE=19Forl/OPTION
OPTION VALUE=19Grosseto/OPTION
OPTION VALUE=19Gualdo/OPTION

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




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




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

2002-09-18 Thread Rudolf Visagie

Hi,

The solution I posted:

echo 'OPTION
VALUE='.$alphabetical[$i][0].''.$alphabetical[$i].'/OPTION'.\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 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. -
.$stadium[$id].'/OPTION'.\n;
}
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.

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