
How to get a table number to auto-increment?
Moderator: General Moderators
- 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?
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
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
- 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?
You just want to display a table row number next to each of the results?
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.
Code: Select all
$count = 1;
//in the loop which outputs each row of the table
echo $count++;
Re: How to get a table number to auto-increment?
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.turbolemon wrote:You just want to display a table row number next to each of the results?
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.Code: Select all
$count = 1; //in the loop which outputs each row of the table echo $count++;
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
Re: How to get a table number to auto-increment?
Damn, not working
, what am I doing wrong?
NVM, it works, just a small mistake. Thanks a heaps
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";
}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";
}- 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?
Try doing it like this:
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).
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
}