Start counting from zero again

Discussion in 'PHP' started by red-x, Oct 5, 2008.

  1. #1
    Hi, I have something like a thread that has posts inside of it. I want to display what post number it is in each post. But in every thread the count should start from zero again like in this forums. How can I do that?

    Thanks in advance :)
     
    red-x, Oct 5, 2008 IP
  2. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just keep a seperate variable that you ++ each time you output the posts. You have to be reading the posts from a DB or something, so you would

    
    foreach ( post )
       postvar++
       output( postcontent )
    
    Code (markup):
     
    darkmessiah, Oct 5, 2008 IP
  3. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply, I don't really know how to use foreach();. I am using mysql to display the posts. Each post has a unique ID. How will I code that?


    $post_id = $row["pid"];
    
    foreach($post_id)
      $count_post++
      output($post_id)
    PHP:
    Am I close? and will this work inside while(); ?
     
    red-x, Oct 5, 2008 IP
  4. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just to a search on google for foreach() you can also do this in a while loop, fetching each row of the sql query
    
    $post_count=0;
    while($row = fetch_array()){
      echo $row['content'];
      echo $row['pid'];
      echo $post_count;
      $post_count++;
    }
    
    Code (markup):
     
    darkmessiah, Oct 6, 2008 IP
  5. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much that worked :)
     
    red-x, Oct 6, 2008 IP