Hiya-
Longtime reader, first time poster.
I am using PHP to generate <option> tags inside a <select> input in a form. I am fetching a complete list of categories from a database. There needs to be an <option> for each category, and if the variable $mylcat matches one of those, then the <option> needs a 'selected' attribute stuck in there.
I thought this would be easy to do by running a while loop of the categories, then sticking in an IF to compare the current category with $mylcat. Much to my surprise, this works only when the comparison expression in that IF is returning false! If the expression I look for returnst true, then a variable gets reassigned against my will.
So, this works:
$sql = "SELECT DISTINCT lcat FROM links ORDER BY lcat";
$result = mysql_query($sql);
echo "CATEGORY:<br><select name=\"mylcat\">";
while (list($cat) = mysql_fetch_row($result)) {
if ($cat != $mylcat) {
$tag ="";
} else {
$tag ="selected";
}
echo "<option $tag value=$cat>$cat";
}
echo "</select><p>";
BUT THIS DOESN'T:
$sql = "SELECT DISTINCT lcat FROM links ORDER BY lcat";
$result = mysql_query($sql);
echo "CATEGORY:<br><select name=\"mylcat\">";
while (list($cat) = mysql_fetch_row($result)) {
if ($cat = $mylcat) {
$tag ="selected";
} else {
$tag ="";
}
echo "<option $tag value=$cat>$cat";
}
echo "</select><p>";
In the latter case, $cat is reassigned the value of $mylcat for all iterations of the loop! What am I missing?
-p