Simple Counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ankurdave
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 12:35 am
Location: INDIA

Simple Counter

Post by ankurdave »

My problem is very simple
In the result i got the all values in tables but my counter always shows value 1 in all the fields
My code is as follows

Code: Select all

<?php
while ($row = mysql_fetch_object($result))
{
    $counter = 1;
    $value = $row -> innovatorid;
    $result1 = mysql_query("SELECT * FROM innovator WHERE innovatorid="$value"") or die("Query 2 Wrong");
    $total1 = mysql_num_rows($result1); 
    while ($row1 = mysql_fetch_object($result1))
   {
        print("<tr>"); 
        print("<td align="center">" . $counter++ . "</td>"); 
        print("<td align="center"><a href="innovators.php?         innovatorid=" . $row1 -> innovatorid . "">" . $row1 -> fname . "</a></td>");
        print("<td align="center"><a target="New Window" href="http:\cases" . $row1 -> linkpage . "">" . $row1 -> linkpage . "</a></td>");
        print("</tr>");
    } 
    $counter = $counter + 1;
	// 	echo "$counter";
} 

?>
The problem is in the line

Code: Select all

<?php
        print("<td align="center">" . $counter++ . "</td>"); 

?>
[/b]

It always shows 1 in each column
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

($counter++) maybe..
User avatar
ankurdave
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 12:35 am
Location: INDIA

Post by ankurdave »

I do it in this way then it show 2 in all column

Code: Select all

<?php

 $counter = $counter+1;
 print("<td align="center">" . $counter . "</td>"); 

?>
it show 2 in all column
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. looking at your script more closely.. you seem to be only returning 1 object for the second query.

Since you have $counter = 1, inside the loop.. it gets reset for each one found.

so.. pull the $counter=1 bit out of the loop.
User avatar
ankurdave
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 12:35 am
Location: INDIA

Post by ankurdave »

thnx
I done it
Post Reply