PDA

View Full Version : array within an array


SkaFreaks
09-03-2004, 12:30 PM
Hey
ok, I'm not sure if you can do this or how to, so I'll just explain what I want to do .

I have 2 tables in a database (contests, contestswin) Contests contains (conid, conname, condescription) Contestswin contains (winid, conid, conwinner)

Some of the contests have multiple winners, so I want it to list the contest name then all the winners for that contest and then show the next contest and continue on.

this is what I tried but it didn't work, it may help explain more of what I want.


$db2 = mysql_connect("localhost", "name", "pass");
mysql_select_db("skafreak_nuke1",$db2);
$result = mysql_query("SELECT * FROM contests, contestswin WHERE contests.conid=contestswin.conid ORDER BY condeadline",$db2);
while ($myrow = mysql_fetch_array($result)) {
printf("%s<br>\n", $myrow["conname"]);
while ($myrow = mysql_fetch_array($result)) {
printf("<li>%s\n", $myrow["conwinner"]);
} }


Thanks for any help!

Horus_Kol
09-03-2004, 01:29 PM
sorry - is this a database question or a PHP question ???

SkaFreaks
09-03-2004, 02:06 PM
well both i guess. it has to do with how to code the php to get the data from the database a certain way

Horus_Kol
09-03-2004, 02:43 PM
sorry.

you aren't accessing any indexes within the arrays...

actually, I don't think that would work at all...


try this:

$db2 = mysql_connect("localhost", "name", "pass");
mysql_select_db("skafreak_nuke1",$db2);

$result = mysql_query("SELECT conid,conname FROM contests ORDER BY condeadline;",$db2);


while ($row = mysql_fetch_array($result)
{
echo $row['conname'] ."<br>\r\n";

$wins = mysql_query("SELECT conwinner FROM contestswin WHERE conid=". $row['conid'] ." ORDER BY conwinner;", $db2);

while ($r2 = mysql_fetch_array($wins))
{
echo $r2['conwinner'] ."<br>\r\n";
}
}


That ought to do it.

SkaFreaks
09-03-2004, 03:04 PM
That did it. Thanks.

Just incase anyone else uses this there was one mistake in it.

The first while statement is missing a ) at the end of it.

Horus_Kol
09-03-2004, 03:21 PM
I keep missing those. Fixed version.:


$db2 = mysql_connect("localhost", "name", "pass");
mysql_select_db("skafreak_nuke1",$db2);

$result = mysql_query("SELECT conid,conname FROM contests ORDER BY condeadline;",$db2);


while ($row = mysql_fetch_array($result))
{
echo $row['conname'] ."<br>\r\n";

$wins = mysql_query("SELECT conwinner FROM contestswin WHERE conid=". $row['conid'] ." ORDER BY conwinner;", $db2);

while ($r2 = mysql_fetch_array($wins))
{
echo $r2['conwinner'] ."<br>\r\n";
}
}



Glad it worked out for you.