Getting a counter to increment when using mysql_fetch_*

Discussion in 'Web Design and Development' started by Malfoy, Apr 27, 2008.

  1. Malfoy macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005
    #1
    Ok this is my result:
    [​IMG]

    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)
    [​IMG]
    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.
     
  2. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #2
    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.
     
  3. bhess macrumors newbie

    Joined:
    Nov 8, 2007
    #3
    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?
     
  4. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #4
    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.
     
  5. Malfoy thread starter macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005
    #5
    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.
     
  6. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #6
    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.
     
  7. Malfoy thread starter macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005
    #7
    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<= $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. :)
     
  8. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #8
    Put a dollar sign in front of the i in your for loop condition.

    change

    PHP:
    ($i=1<= $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
    }

     
  9. Malfoy thread starter macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005
    #9
    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 :)
     
  10. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #10
    No problem, at least you'll know to look out for it in the future.

    I take it that is is working now?
     
  11. Malfoy thread starter macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005

Share This Page