Page 1 of 1
Something wrong with my code
Posted: Mon Aug 23, 2010 7:14 pm
by shags_j
Allright,
I'm new to this php thing and can't work out where I am going wrong:
Code: Select all
//loop and populate select box
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['gamesId']."\" ";
if($row['gamesID']== $gamesystemid){
echo "selected=\"selected\" ";
}
echo ">".$row['gamesNames']."\n ";
}
?>
The above doesn't do the selected bit (ie. can't get the if statement to trigger. Is there some basic screwup I have done?
Re: Something wrong with my code
Posted: Mon Aug 23, 2010 7:21 pm
by ScOrPi
Code: Select all
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['gamesId'];
if($row['gamesID']== $gamesystemid){
echo "selected=selected";
}
echo ">".$row['gamesNames']." \n ";
}
Try it now

Re: Something wrong with my code
Posted: Mon Aug 23, 2010 7:35 pm
by shawngoldw
@Scorpi: That will get you something which looks like:
<option value=valselected=selected>
That's not going to work.
You're almost right here, I think you're only missing the ending of the option. Also there is an unnecessary space after selected, and I would move the space after value into the selected line so that it isn't there when it doesn't need to be.
Code: Select all
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['gamesId']."\"";
if($row['gamesID']== $gamesystemid){
echo " selected=\"selected\"";
}
echo ">".$row['gamesNames']."</option>\n ";
}
I would also use just SELECT rather than selected="selected" like this:
Code: Select all
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['gamesId']."\"";
if($row['gamesID']== $gamesystemid){
echo " SELECTED";
}
echo ">".$row['gamesNames']."</option>\n ";
}
Shawn
Re: Something wrong with my code
Posted: Mon Aug 23, 2010 7:58 pm
by shags_j
EDIT: Never mind. Spot the idiot (that would be me). Had the wrong column identifier in the $row['gamesID'] instead of gamesId.
What a tool
Thanks guys. It's still not seelcting the right one.
Here is the html output after runnign the script:
<HTML>
<body>
<select name="gamesystem">
<option value="1" >Game 1
<option value="2" >Game 2
<option value="3" >Game 3
</select>
</body>
</html>
The if statement doesn't seem to be triggering.
Earlier in the script I set $gamesystemid = 2 so expected Game 2 to show.
Cheers,
J
Re: Something wrong with my code
Posted: Mon Aug 23, 2010 9:53 pm
by shawngoldw
You forgot something that I mentioned in my post.
note: It will not fix the problem you're talking about. But it will make html syntax proper.
The syntax for your if statement and select looks correct. Maybe you have a problem with the capitalization or scope of the variable
Shawn