Forum Moderators: coopster
pls not that this is a jst a snippet from my coding.so ignore unclosed tags n all.
$db_host = 'localhost';
$db_user = '';
$db_pwd = '';
$database = 'mydb';
$table = 'user_groups';
$rowsPerPage = 3;
$pageNum = 1;
$offset = ($pageNum - 1) * $rowsPerPage;
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result1 = mysql_query(" SELECT ug_name FROM user_groups "." LIMIT $offset, $rowsPerPage");
if (!$result1) {
die("Query to show fields from table failed");
}
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
?>
<table width="348" border="1" cellpadding="1" cellspacing="1" bordercolor="#1648AB" bgcolor="#EBE9ED">
<tr>
<td width="159" class="right"><strong class="right">User Group</strong></td>
<td width="63" class="right"><strong>Edit</strong></td>
<td width="108" class="right"><strong>Remove</strong></td>
</tr>
<?php while($row1 = mysql_fetch_row($result1)) { ?>
<tr><?php foreach($row1 as $cell){?>
<td><?php echo "$cell"; ?></td>
<td><a href="editusergroups.php?x=<?php echo"$cell";?>">Edit</a></td>
<td><a href="deleteusergroups.php?x=<?php echo"$cell";?>">Remove</a></td>
</tr>
<?php } }
$query = "SELECT COUNT(ug_name) AS numrows FROM user_groups";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
$nav = '';
$fields_num = mysql_num_fields($result1);
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
mysql_free_result($result1);
?>
</table>
</div>
<br class="clearfloat" />
<?php @include_once("ui_footer.php");?>
</div>
</body>
</html>
[edited by: eelixduppy at 7:46 am (utc) on July 18, 2008]
[edit reason] removed db specifics [/edit]
$rowsPerPage = 3;
[b]$pageNum = isset($_GET['page']) ? (int)$_GET['page'] : 1;[/b]
$offset = ($pageNum - 1) * $rowsPerPage;
#
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
#
if (!mysql_select_db($database))
die("Can't select database");
#
$result1 = mysql_query(" SELECT ug_name FROM user_groups "." LIMIT $offset, $rowsPerPage");
if (!$result1) {
die("Query to show fields from table failed");
}
You need to defined the variable pageNum BEFORE you calculate the offset, otherwise it's not going to work. Try that and see where it gets you.
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
before:
$offset = ($pageNum - 1) * $rowsPerPage;
and also:
while($row = mysql_fetch_assoc($result))
instead of:
while($row1 = mysql_fetch_row($result1))
and put:
<tr>
<td><?php echo $row['ug_name'] ?></td>
<td><a href="editusergroups.php?x=<?php echo $row['ug_name'];?>&ug_id=<?php echo $row['ug_id'];?>">Edit</a></td>
<td><a href="deleteusergroups.php?x=<?php echo $row['ug_name'];?>&ug_id=<?php echo $row1['ug_id']?>">Remove</a></td>
</tr>
instead of:
<tr><?php foreach($row1 as $cell){?>
<td><?php echo "$cell"; ?></td>
<td><a href="editusergroups.php?x=<?php echo"$cell";?>">Edit</a></td>
<td><a href="deleteusergroups.php?x=<?php echo"$cell";?>">Remove</a></td>
</tr>
it worked! thanks