Something wrong with my code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shags_j
Forum Newbie
Posts: 9
Joined: Sun Aug 22, 2010 9:55 pm

Something wrong with my code

Post 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?
ScOrPi
Forum Commoner
Posts: 44
Joined: Fri Jul 09, 2010 8:01 am
Location: BAGHDAD - IRAQ

Re: Something wrong with my code

Post 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 :?
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Something wrong with my code

Post 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
shags_j
Forum Newbie
Posts: 9
Joined: Sun Aug 22, 2010 9:55 pm

Re: Something wrong with my code

Post 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 :oops:



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
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Something wrong with my code

Post 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
Post Reply