Numbering query results?

Discussion in 'PHP' started by anversli, Dec 28, 2007.

  1. #1
    $count++;
    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
    Code (markup):
    Hi,
    I'm using this code to number query results from mysql like:

    01
    02
    03
    04
    05

    But I would like to number results like:
    05
    04
    03
    02
    01

    Can someone help me with this please?
     
    anversli, Dec 28, 2007 IP
  2. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    hi,

    first get the total number of results :
    $count = mysql_num_rows($result);
    PHP:
    then show your results and place this at the bottom:
    $count--;
    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
    PHP:
    U'r welcome
     
    Dagon, Dec 28, 2007 IP
  3. anversli

    anversli Active Member

    Messages:
    350
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Hey Dagon, thanks for your reply

    I'm getting results like:
    02
    02
    02
    02
    02
    02
     
    anversli, Dec 28, 2007 IP
  4. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Make sure you place
    $count--;
    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
    PHP:
    within the loop, under the echo/print function calls.
     
    Dagon, Dec 28, 2007 IP
  5. anversli

    anversli Active Member

    Messages:
    350
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    This should do it?
    
    $getcomments = mysql_query("SELECT * FROM comments");
    $count = mysql_num_rows($getcomments);
    while($row = mysql_fetch_array($getcomments)) {
    $count--;
    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
    echo"
    <b>$count</b>
    </div>
    ";
    }
    
    PHP:
    Still not working
     
    anversli, Dec 28, 2007 IP
  6. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #6
    
    $getcomments = mysql_query("SELECT * FROM comments");
    $count = mysql_num_rows($getcomments);
    while($row = mysql_fetch_array($getcomments)) {
    $count = str_pad($count, 2, "0", STR_PAD_LEFT);
    echo"<b>$count</b>";
    $count--;
    }
    
    echo"</div>";
    
    PHP:
     
    Dagon, Dec 28, 2007 IP
  7. anversli

    anversli Active Member

    Messages:
    350
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #7
    Ok thanks alot Dagon, working now :)
     
    anversli, Dec 28, 2007 IP