yuli chika
yuli chika

Reputation: 9221

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

Upvotes: 0

Views: 75

Answers (2)

AndreKR
AndreKR

Reputation: 33678

Because the increment is outside the loop.

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

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++;
             }
?>

Upvotes: 6

Related Questions