How to get a table number to auto-increment?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

How to get a table number to auto-increment?

Post by insight »

I'm creating a table for my menu. I have a number and ID. The ID is read from the database and shows the ID of the link. But I was wondering how I could get the table to also show a number which increments by 1 every time a new link is added and increments down by 1 every time a link is taken out.

Image
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: How to get a table number to auto-increment?

Post by Jammerious »

Hm, if I understood you correctly, you want to show a sequential number, not the ID from the database entry.
I would simply do that within the loop that you use to show all of the menu entries. As you explained it there's no need to have that in the DB, otherwise you could do that with database triggers, if not even some simpler way.
So just use a counter in the loop and echo that :)
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: How to get a table number to auto-increment?

Post by turbolemon »

You just want to display a table row number next to each of the results?

Code: Select all

 
 
$count = 1;
 
//in the loop which outputs each row of the table
 
echo $count++;
 
 
Obviously you come into problems if you use pagination, as you will have to offset the count variable by the number of items per page multiplied by the page number.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: How to get a table number to auto-increment?

Post by insight »

turbolemon wrote:You just want to display a table row number next to each of the results?

Code: Select all

 
 
$count = 1;
 
//in the loop which outputs each row of the table
 
echo $count++;
 
 
Obviously you come into problems if you use pagination, as you will have to offset the count variable by the number of items per page multiplied by the page number.
Cheers, will use that code now and see how it goes. And it's just for a menu so maybe a maximum of 20 links (even that's probably exaggerating to much) so no need to use pagination.

I'm learning a lot here, I only just seriously started getting into PHP and its amazing what you can do with it. I can practically show any page I want just by using the $_GET method :o. And there's still so much more to learn :mrgreen:
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: How to get a table number to auto-increment?

Post by insight »

Damn, not working :(, what am I doing wrong?

Code: Select all

while($fetchmenu = mysql_fetch_array($menu)) 
{
 
$id = $fetchmenu['id'];
$title = $fetchmenu['title'];
$link = $fetchmenu['weblink'];
$module = $fetchmenu['module'];
$level = $fetchmenu['level'];
$count = 1;
 
echo "<tr align=\"center\"><td class=\"tbl\">";
 
echo $count++; 
 
echo "</td><td class=\"tbl\"><input type=\"checkbox\" name=\"check_list\" value=\"1\"></td><td align=\"left\" class=\"tbl\" style=\"padding-left: 5px;\"><a href=\"index.php?file=Admin&page=Menu&function=Edit&id=" . $id . "\">" . $title . "</a></td><td class=\"tbl\">Position</td><td class=\"tbl\">URL</td><td class=\"tbl\">" . $level . "</td><td class=\"tbl\">" . $id . "</td><td class=\"tbl\"><a href=\"index.php?file=Admin&page=Menu&function=Edit&id=" . $id . "\"><img border=\"0\" src=\"themes/default/images/edit_f2.png\" alt=\"\" /></a></td><td class=\"tbl\"><a href=\"index.php?file=Admin&page=Menu&function=Delete&id=" . $id . "\"><img border=\"0\" src=\"themes/default/images/cancel_f2.png\" alt=\"\" /></a></td></tr>\n";
}
NVM, it works, just a small mistake. Thanks a heaps :mrgreen:

Code: Select all

$count = 1;
 
while($fetchmenu = mysql_fetch_array($menu)) 
{
 
$id = $fetchmenu['id'];
$title = $fetchmenu['title'];
$link = $fetchmenu['weblink'];
$module = $fetchmenu['module'];
$level = $fetchmenu['level'];
 
echo "<tr align=\"center\"><td class=\"tbl\">";
 
echo $count++; 
 
echo "</td><td class=\"tbl\"><input type=\"checkbox\" name=\"check_list\" value=\"1\"></td><td align=\"left\" class=\"tbl\" style=\"padding-left: 5px;\"><a href=\"index.php?file=Admin&page=Menu&function=Edit&id=" . $id . "\">" . $title . "</a></td><td class=\"tbl\">Position</td><td class=\"tbl\">URL</td><td class=\"tbl\">" . $level . "</td><td class=\"tbl\">" . $id . "</td><td class=\"tbl\"><a href=\"index.php?file=Admin&page=Menu&function=Edit&id=" . $id . "\"><img border=\"0\" src=\"themes/default/images/edit_f2.png\" alt=\"\" /></a></td><td class=\"tbl\"><a href=\"index.php?file=Admin&page=Menu&function=Delete&id=" . $id . "\"><img border=\"0\" src=\"themes/default/images/cancel_f2.png\" alt=\"\" /></a></td></tr>\n";
}
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: How to get a table number to auto-increment?

Post by Jammerious »

Try doing it like this:

Code: Select all

while($fetchmenu = mysql_fetch_array($menu))
{
 
$id = $fetchmenu['id'];
$title = $fetchmenu['title'];
$link = $fetchmenu['weblink'];
$module = $fetchmenu['module'];
$level = $fetchmenu['level'];
$count = 1;
 
echo "<tr align=\"center\"><td class=\"tbl\">";
 
echo $count++;
?>
  </td>
  <td class="tbl"><input type="checkbox" name="check_list" value="1"></td> 
  <td class="tbl"><?php echo $level ?></td>
</tr>
<?php
}
 
As you see there's no need to escape the double quotes and your code becomes more readable (I used just two lines to show you how you should do it).
Post Reply