PHP setting numbers automatically
<?php
$result = mysql_query("SELECT * FROM articles WHERE tag='sports'");
$strss=$strss+1;
while ($row = mysql_fetch_array($result))
开发者_开发技巧 {
echo '<li id="nav'.$strss.'"><a href="#" >'.$row['title'].'</li>';
}
?>
Why can't I set numbers automatically? the number always is 1
You need to move your counter inside the while loop like the following:
<?php
$result = mysql_query("SELECT * FROM articles WHERE tag='sports'");
$strss=1;
while ($row = mysql_fetch_array($result))
{
echo '<li id="nav'.$strss.'"><a href="#" >'.$row['title'].'</li>';
$strss++;
}
?>
Because the increment is outside the loop.
精彩评论