Jump to content

Storing Two Data Pieces In Each Array Entry


member123

Recommended Posts

I am converting rows in a MySQL table into rows in an array.

 

The table looks like this:

 

player_id player age

1Joe24

2Craig31

3David29 ... with a bunch of rows like that.

 

So far my code looks like this:

   $result = bb_db_query("SELECT * FROM bb_players");
   while ($myrow=mysql_fetch_array($result)) {
      $players[$myrow['player_id']][$myrow['player']][] = $myrow['age'];
   }

 

Am I doing it correctly so far?

 

Also, could someone please help me create code that will print out all of this information using foreach() commands?  Thanks!

Try this

 

$result = bb_db_query("SELECT * FROM bb_players");

   while ($myrow=mysql_fetch_assoc($result)) {

// $each_row will now contain this - 1,Joe,24
// to turn it back into an array, do this - $each_row = explode(",", $each_row);
$each_row = implode(",", $myrow);


   }

 

 

 

The foreach part, you can do this

 

foreach ($each_row as $each_row) {

// this will give - 1<BR>Joe<BR>24
echo "$each_row<BR>";
}

Hi Jaymc, that first part looks like it's exactly what I'm looking for.  Looks like you found an interesting way to do that part.

 

However, for the second part, I'm going to be doing some analysis with the names and ages.  How do I separate that information?

 

Thanks.

Hi Jaymc, that first part looks like it's exactly what I'm looking for.  Looks like you found an interesting way to do that part.

 

However, for the second part, I'm going to be doing some analysis with the names and ages.  How do I separate that information?

 

Thanks.

 

foreach ($each_row as $each_row) {

// this will give - 1<BR>Joe<BR>24
echo "$each_row<BR>";
}

 

That will spit out all entries in the array

 

Or

 

$each_row = explode(",", $each_row);

 

$each_row[0] will give you the ID

$each_row[1] will give you the name

$each_row[2] will give you the age

 

Logical order. Think thats what you need? If not please explain

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.