I have a while loop and I am trying to associate a number with each line that increments by 1. I am sure this is probably something simple, I am not sure why I am struggling with it. Here is an example of that I have:

<?php	
$counter = 1;
if($total_pages > 0){
while($row = mysql_fetch_array($query)){?>
<tr>
<td><?php echo $counter++ ;?></td>
<td>column2</td>
<td>column3</td>
<td>column4</td>
</tr>
<?php
}
}
?>

But it doesn't output anything. I have also tried several other combinations but can't get it to work for some reason.

Recommended Answers

All 12 Replies

Did you try the following?

<?php	
$counter = 1;
if($total_pages > 0) {
     while($row = mysql_fetch_array($query)) {
         $counter++;
         echo "<tr><td>".$counter."</td><td>column2</td><td>column3</td><td>column4</td></tr>";
     }
}
?>

you dont want to echo counter++. you should have

$counter ++;
echo $counter;

If that doesnt work, just move your html into the php tags and echo then out

you dont want to echo counter++. you should have

$counter ++;
echo $counter;

If that doesnt work, just move your html into the php tags and echo then out

Thanks for repeating my answer.

lol there werent any when i was looking at it.

not done intentionally

I know what you mean. Did happen to me sometimes as well.

you dont want to echo counter++. you should have

$counter ++;
echo $counter;

If that doesnt work, just move your html into the php tags and echo then out

<?php	
$counter = 1;
if($total_pages > 0) {
     while($row = mysql_fetch_array($query)) {
         $counter++;
         echo "<tr><td>".$counter."</td><td>column2</td><td>column3</td><td>column4</td></tr>";
     }
}
?>

Unfortunately still no output using this method. Do I need to define the variable in another place maybe? With no output it seems the variable is unset for some reason.

<?php	
$counter = 1;
if($total_pages > 0) {
     while($row = mysql_fetch_array($query)) {
         $counter++;
         echo "<tr><td>".$counter."</td><td>column2</td><td>column3</td><td>column4</td></tr>";
     }
}
?>

Unfortunately still no output using this method. Do I need to define the variable in another place maybe? With no output it seems the variable is unset for some reason.

The only reason I can think of is the while loop is not executed. Does the query have any results? And what is the value of $total_pages?

The only reason I can think of is the while loop is not executed. Does the query have any results? And what is the value of $total_pages?

The while loop definitely executes, as the rest of the code populates fine. I believe the issue is that the $counter variable needs to be within the while condition, as every example I find has it this way. I am just not sure how to implement it because the data is obviously coming from an array, not the $counter variable.

A miss-understanding maybe. Are you using the counter for the array? If yes, you need to have the counter inside the while loop. Something like this:

while($row = mysql_fetch_row($query))  {
   echo " <tr> ";
   for($x=0; $x <= count($row); $x++)  {
      echo "<td>myrow[$x]</td>";
   }
   echo "</tr>";
}

A miss-understanding maybe. Are you using the counter for the array? If yes, you need to have the counter inside the while loop. Something like this:

while($row = mysql_fetch_row($query))  {
   echo " <tr> ";
   for($x=0; $x <= count($row); $x++)  {
      echo "<td>myrow[$x]</td>";
   }
   echo "</tr>";
}

Yes, $counter should increment based on the elements in the array. I tried your solution but unfortunately it echoes multiple <td> tags, I don't want it to echo a new row every time, I just want it to increment the variable. What about this, the data is coming from a mysql_query. After that query, how would I add a new element to the array to represent the $counter variable? Then I believe it will work. I am not very knowledgable with arrays. something like array_push maybe?

In the following code you don't need a counter. Tested it on a real database and it works. I do remember now why I never use mysql functions but alway the mysqli ones. Things are so much easier with mysqli.
But anyway, I didn't use mysql_fetch_array but _assoc to make the foreach possible.Hope it will solve your problem.

<?php
// The while loop is looping through the database rows,
// the for loop is looping through the current database row.

while ($row = mysql_fetch_assoc ($query)) {
	echo "<tr>";
	foreach ($row as $var => $value) {
		echo "<td>".$value."</td>";
	}
	echo "</tr>";
}
?>

Thanks for helping colweb. I was right, it was something simple, and frankly embarrasing lol. The code suggested worked, but I wasn't seeing the numbers in the HTML because of the color. Once I changed the color the numbers were there as expected. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.