Page 1 of 1

[SOLVED] Use of Arrays

Posted: Wed Jun 23, 2004 7:10 am
by Garry
Hi, I am fairly new to PHP, and very new to its use of Arrays.
I am trying to fill an array from a MySQL database that is already populated, then I want to manipulate the data within the array rather than referring back to the db all the time - a fairly standard practice, I'm sure.
But I'm struggling to get it to work. The code below shows that data is filling the array from the database (part 1), but part 2 prints nothing, where I would expect it to print the clubid and clubname variables for each row in the array. Can anyone put me out of my misery please? :(

Code: Select all

<?php
 /* part 1 */
 while ( $ClubRow = @ mysql_fetch_array($ClubQuery) )
 {
  /* Fill array here */

   $squad = array("$index" => array(
   "clubid"   => $ClubRow[0],
   "clubname" => $ClubRow[1],
   "country"  => $ClubRow[2],
   "balls"    => $ClubRow[3],
   "defpts"   => $ClubRow[4],
   "midpts"   => $ClubRow[5],
   "attpts"   => $ClubRow[6],
   "uefacomp" => $ClubRow[7]));
echo $index;
echo $squad[$index]['clubname'];
echo $squad[$index]['country'];
$index=$index+1;
 }

 /* part 2*/
for($index=1; $index<300; $index++)
{
     $squad[$index]['clubid'] .
     $squad[$index]['clubname'];
}

?>
feyd | added

Code: Select all

tags :: [/color][url=http://forums.devnetwork.net/viewtopic.php?t=21171][color=red]:arrow: [u][b]Posting Code in the Forums[/b][/u][/color][/url]

RE: Use of arrays

Posted: Wed Jun 23, 2004 7:44 am
by envirojon
In part 2 you are concatenating the two strings but not sending them to output using echo or print.

Posted: Wed Jun 23, 2004 7:55 am
by Garry
:oops: Thanks. Corrected:

for($index=1; $index<300; $index++)
{
echo $squad[$index]['clubid'] .
$squad[$index]['clubname'];
}

but now I only get the very last entry (which for academic interest is no. 235) and no others.

Posted: Wed Jun 23, 2004 8:20 am
by markl999

Code: Select all

while ( $ClubRow = mysql_fetch_array($ClubQuery) ){
    $squad[] = $ClubRow;
}
foreach($squad as $foo){
    echo $foo['clubid'].' '.$foo['clubname'].'<br />';
}

Posted: Wed Jun 23, 2004 8:27 am
by Garry
:D Thanks guys. That seems to work.
I'm sure I'll be back with more (dumb) questions!
Cheers.

Posted: Wed Jun 23, 2004 10:30 am
by Garry
OK, moving on...
I now need to get at data from the array.
So, for example, if clubid=22 (in a variable called $position), I need to pull out other variables from the array, such as clubname, country and balls. How do I get at the other variables from the array when e.g. clubid=22?

Posted: Wed Jun 23, 2004 10:36 am
by markl999

Code: Select all

while ( $ClubRow = mysql_fetch_array($ClubQuery) ){
    $squad[$ClubRow['clubid']] = $ClubRow;
}
foreach($squad as $key=>$val){
    echo $key.' '.$val['clubname'].'<br />';
}
//to get club id 22's info
var_dump($squad[22]); //to dump the raw output
//or
echo $squad[22]['clubname'];

Posted: Wed Jun 23, 2004 11:10 am
by Garry
Thanks again, markl999.
I'll be back.