Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Malfoy

macrumors 6502a
Original poster
Nov 1, 2005
688
2
Ok this is my result:
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)
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.
 
PHP:
$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.
 
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?
 
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.
 
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.
 
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.
 
PHP:
  $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
PHP:
 $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. :)
 
Put a dollar sign in front of the i in your for loop condition.

change

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

to

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



If you're happier with the while loop:

PHP:
$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
}
 
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 :)
 
No problem, at least you'll know to look out for it in the future.

I take it that is is working now?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.