开发者

PHP setting numbers automatically

开发者 https://www.devze.com 2023-01-25 04:42 出处:网络
<?php $result = mysql_query(\"SELECT * FROM articles WHERE tag=\'sports\'\"); $strss=$strss+1; while ($row = mysql_fetch_array($result))
<?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.

0

精彩评论

暂无评论...
验证码 换一张
取 消