Page 1 of 1

How to get a table number to auto-increment?

Posted: Tue Jul 28, 2009 4:25 am
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

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

Posted: Tue Jul 28, 2009 4:43 am
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 :)

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

Posted: Tue Jul 28, 2009 4:45 am
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.

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

Posted: Tue Jul 28, 2009 5:18 am
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:

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

Posted: Tue Jul 28, 2009 5:24 am
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";
}

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

Posted: Tue Jul 28, 2009 7:02 am
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).