...

View Full Version : Resolved PHP MySQL multiple resuls array.



mrkfc
09-03-2011, 12:38 AM
I am trying to make a PHP MySQL script to get multiple results from a database and then display them in a HTML form list.
Here is my current code:

<?php
include_once "connect_to_mysql.php";
$fetchmatch = mysql_query("SELECT * FROM matches WHERE hteam='$team' and played='no'"); //get matches for your team
//get the varialbes into an array
$matchcount = mysql_num_rows($fetchmatch);
while($row2 = mysql_fetch_assoc($fetchmatch)){
$matchid = $row2["id"];
$ateam = $row2["ateam"];
}
$matchname = "$team vs $ateam";
?>


<select name="matchid" id="matchid">
<option value="<?php echo"$matchid"; ?>" selected="selected"><?php echo"$matchname"; ?></option>
</select>


Any help would be appreciated, thanks :)

Chris Hick
09-03-2011, 01:54 AM
Ok, what exactly are you trying to do? A drop down list of all the matches or a drop down list of all the teams??

mrkfc
09-03-2011, 02:34 AM
I am trying to get a dropdown list of all matches.

ASTP001
09-03-2011, 03:17 AM
I am trying to make a PHP MySQL script to get multiple results from a database and then display them in a HTML form list.
Here is my current code:

<?php
include_once "connect_to_mysql.php";
$fetchmatch = mysql_query("SELECT * FROM matches WHERE hteam='$team' and played='no'"); //get matches for your team
//get the varialbes into an array
$matchcount = mysql_num_rows($fetchmatch);
while($row2 = mysql_fetch_assoc($fetchmatch)){
$matchid = $row2["id"];
$ateam = $row2["ateam"];
}
$matchname = "$team vs $ateam";
?>


<select name="matchid" id="matchid">
<option value="<?php echo"$matchid"; ?>" selected="selected"><?php echo"$matchname"; ?></option>
</select>


Any help would be appreciated, thanks :)

ok, what you need to do is store match id and ateam as an array... so to do that do


$matchid[] = $row2["id"];
$ateam[] = $row2["ateam"];

and so all the entries will be stored in there.
and then to make a list of all the options find out the number of entries in the array by doing


$indexCount = count($matchid);


and run a loop for the options

for($index=0; $index < $indexCount; $index++) {
echo '<option value="' . $matchid . '" selected="selected">"' . $matchname . '</option>';
}


also, when you're setting

$matchname = "$team vs $ateam";
it should be
$matchname = $team . " vs " . $ateam;

mrkfc
09-03-2011, 03:58 AM
Thank you very much :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum