Log in

View Full Version : Some PHP help!



jc_gmk
07-31-2007, 11:29 AM
Hopefully this is a simple problem:
I have a while statement in my php code that prints out the rows from a mysql database. This works fine.



while($info = mysql_fetch_array( $result ))
{
print "<div>" . $info['tablerow'] . "</div>";
}


My question is: can I use an IF statement within the while statement that doesn't effect all of the results. e.g.



while($info = mysql_fetch_array( $result ))
{
if ($info['anothertablerow'] == true)
{
print "This text is printed only for rows with a value";
}
else
{
print " ";
}
print "<div>" . $info['tablerow'] . "</div>";
}

jc_gmk
07-31-2007, 04:01 PM
Ok, to elaborate on what i'm trying to acheive:

I am creating a list of shops for a 'find a dealer' page

I have a list of UK counties in a MySQL database and from that I produce a form like this:

<select name="county">

<option value="all" <?php if ($submitted == "all") { print "selected=\"selected\""; } else { print ""; } ?> >Please Select...</option>

<?php while ($countyinfo = mysql_fetch_array($countyresult)) { ?>
<option value="<?php print $countyinfo['county']; "><?php print $countyinfo['county']; ?></option>
<?php } ?>

</select>

The reason I want to put an IF Statement in is because - if the form is submitted I want that value to remain selected.

e.g.
(to be placed within the while)

if ($selected = $countyinfo['county'])
{
print "selected=\"selected\"";
}
else
{
print "";
}

Hope this makes sense...

Twey
07-31-2007, 04:38 PM
Yes. You could have just tried it and seen for yourself :)

jc_gmk
08-01-2007, 07:44 AM
Got it to work now, it was because i was putting select="selected" in with php AND html, should have looked abit closer, my bad.

alexjewell
08-01-2007, 02:16 PM
If I were you, I'd use some single quotes...prevents all the backslashes, and single quotes are parsed faster. It's all preference, though, I guess.