PDA

View Full Version : Getting a counter to increment when using mysql_fetch_*




Malfoy
Apr 28, 2008, 01:35 AM
Ok this is my result:
http://neveroutgunned.net/s2s/table.png

Where you see the red arrows is where I'd like the numbers 1-x where x is the number that comment is. Using the 'while (mysql_fetch_array) ' doesn't allow me to increment the counter. The way it handles the loop it seems to disregard anything else but the output and doesn't increment a counter if I put it at the end of the loop (counter is commented out)
http://neveroutgunned.net/s2s/testcode.png
I tried using a for loop (which i commented out at the top) but that failed miserably and broke a whole lot of things.

How would you guys go about getting an incremental counter there while keeping everything else functional? If it requires changing my loop style I'm fine with that. Know that the page is only taking in one argument, an integer, and from that pulls the item that has multiple comments. Any guidance would be appreciated. Thanks in advance.

EDIT: The code as its shown here isn't designed to implement a counter.



elppa
Apr 28, 2008, 03:13 AM
$result = mysql_query("SELECT title FROM table");

$row_count = mysql_num_rows($result);
for($i = 1; $i <= $row_count; $i++) {
$row = mysql_fetch_array($result);
echo("$i. {$row['title']}\n");
}

// move result pointer back to beginning
mysql_data_seek($result,0);

$i = 1;
while($row = mysql_fetch_array($result)) {
echo("$i. {$row['title']}\n");
$i++;
}


These loops will print out exactly the same data.

bhess
Apr 28, 2008, 03:15 AM
So if before the while loop, you put $i = 1, add $i into the output where you want it, and uncomment the $i++ at the end of the while loop, does that not work?

elppa
Apr 28, 2008, 03:23 AM
Your code is hard to read with all those black lines through it.

Hopefully you can learn from the examples above.

Those are two ways to achieve what you have described.

Malfoy
Apr 28, 2008, 03:30 AM
So if before the while loop, you put $i = 1, add $i into the output where you want it, and uncomment the $i++ at the end of the while loop, does that not work?

no it just shows 1 for each spot on the comments.


I'm trying what elppa suggested now. Will be back in a bit with an update.

elppa
Apr 28, 2008, 03:33 AM
no it just shows 1 for each spot on the comments.


I'm trying what elppa suggested now. Will be back in a bit with an update.

Paste your code up, preferably in code or php tags and we will get it working. Promise. :)

An image with loads of black lines is hard to work with.

Malfoy
Apr 28, 2008, 03:41 AM
$Wait_Comments = mysql_query($Waiter_Com) or die(mysql_error());
$Counter = mysql_num_rows($Wait_Comments); // establishing counter for numbering the comments

//for ($i=0; $Wait_Com_Result = mysql_fetch_array($Wait_Comments); $i++){
while ($Wait_Com_Result = mysql_fetch_array( $Wait_Comments)){


Is what I currently use that generates what I want but can't get a counter to work.


I just tried to use $Counter = mysql_num_rows($Wait_Comments); // establishing counter for numbering the comments

for ($i=1; i <= $Counter; $i++){
$Wait_Com_Result = mysql_fetch_array($Wait_Comments);



But the page goes into an infinite load(as in IE just freezes cause the timer just spins) and never displays anything in the table. :(

NOTE: I'm writing the code on a mac, testing on my windows machine at work. That's why I said IE. :)

elppa
Apr 28, 2008, 03:54 AM
Put a dollar sign in front of the i in your for loop condition.

change

($i=1; i <= $Counter; $i++)

to

($i=1; $i <= $Counter; $i++)



If you're happier with the while loop:


$Wait_Comments = mysql_query($Waiter_Com) or die(mysql_error());

$Counter = 1; // counter equal to 1

while ($Wait_Com_Result = mysql_fetch_array($Wait_Comments)){
echo $Counter; // output is 1
$Counter++; // counter now equal to 2
}

Malfoy
Apr 28, 2008, 04:03 AM
1. I CAN'T believe I forgot a $. I kept looking right at it but it didn't stick out.


2. HUG

Thanks soo much! It's the little things :)

elppa
Apr 28, 2008, 04:05 AM
No problem, at least you'll know to look out for it in the future.

I take it that is is working now?

Malfoy
Apr 28, 2008, 04:09 AM
Yep! :)