Jump to content

Archived

This topic is now archived and is closed to further replies.

DrFishNips

[SOLVED] PHP increasing numbers

Recommended Posts

This is a simple question but I couldn't think of what to put into google to get info on it. I need to dynamically number database results starting from 1. For example lets say I have a database with 3 entries "monkey", "badger", "armadillo" when I display the contents I need it to be like

1.) Armadillo

2.) Badger

3.) Monkey

Share this post


Link to post
Share on other sites

What do you mean dynamically?  You should already have some sort of id or auto-increment values when inserting in the database.

Share this post


Link to post
Share on other sites

Or just add a counter to the loop $i++

Yeah thats what I'm talking about. I tried that and it works but the number starts at 0 for some reason. Do you know how to make it start at 1?

Share this post


Link to post
Share on other sites

Or just add a counter to the loop $i++

Yeah thats what I'm talking about. I tried that and it works but the number starts at 0 for some reason. Do you know how to make it start at 1?

 

Put $i=1 before the loop.  If you post the relevant code it will be easier to help.

Share this post


Link to post
Share on other sites

Sorry heres the code

  $query = "SELECT * FROM potg_memb_pm WHERE memb_username = '$pm_login' ORDER BY 'msg_date' LIMIT 10";

  $result = mysql_query($query);

  $num = mysql_num_rows($result);



  $i=0;

  while ($i < $num) {



    $msg_id = mysql_result($result, $i, "id");

    $memb_id = mysql_result($result, $i, "memb_id");

    $msg_sender = mysql_result($result, $i, "msg_sender");

    $msg_subject = mysql_result($result, $i, "msg_subject");

    $msg_body = mysql_result($result, $i, "msg_body");

    $msg_date = mysql_result($result, $i, "msg_date");

    $msg_time = mysql_result($result, $i, "msg_time");





    echo "  <tr>\n";

    echo "    <td><a class=\"gb_link\" href=\"?get=members&page=pm&action=show&msg=$msg_id\">";  echo $i; echo "</a></td>\n";

    echo "    <td><a class=\"gb_link\" href=\"?get=members&page=pm&action=show&msg=$msg_id\">$msg_subject</a></td>\n";

    echo "    <td><a class=\"gb_link\" href=\"?get=members&member=$memb_id\">$msg_sender</a></td>\n";

    echo "    <td>$msg_date</td>\n";

    echo "    <td>$msg_time</td>\n";

    echo "    <td><input type=\"checkbox\" name=\"1\" value=\"1\"></td>\n";

    echo "  </tr>\n";

    echo "        <br>\n";



    ++$i;

  

 

It lists members PM's and I'm trying to number the PM's from 1 up.

Share this post


Link to post
Share on other sites

I got it to work. I just typed echo $i + 1 and now it starts from 1 instead of 0. Thanks for the help.

Share this post


Link to post
Share on other sites

Or you could have changed $i=0 to $i=1.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.