PHP loop gives the same results for every item in the loop
I have a loop in a custom CMS that creates a drop down and allows me to select and change the author of of a post.
Unfortunately it doesn't work and every single option have the syntax 'selected' when it should just be one of them.
What have I done?
while ($row = mysql_fetch_array($rows)) {
printf ('<option value=%d',$auth_id);
if ($auth_id == $current_auth) {
开发者_JAVA技巧 echo ' selected';
}
printf (">%s", $name);
}
$auth_id
isn't changing anywhere. Your script could look like this
$auth_id = 0;
$current_auth = 1;
while ($row = mysql_fetch_array($rows)) {
$auth_id++; // ?? change here
printf ('<option value=%d',$auth_id);
if ($auth_id == $current_auth) {
echo ' selected';
}
printf (">%s", $row['name']);
}
You need to replace $auth_id and $name with the data from the row, e.g. $row['auth_id'] or whatever your fields are named.
It looks like you're not using $row at all in that code, is that correct? Naturally $auth_id and $current_auth will not change.
精彩评论