Jump to content

Pagination problems


HughbertD

Recommended Posts

At the moment when I get to the end of the results, a link appears that takes you to the previous page. However, at the moment the previous button takes you to $startrow= -25 so there are no records to display, I get two errors,

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\joStark\css\display\displayStudentOrderBy.php on line 283

 

and also

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\joStark\css\display\displayStudentOrderBy.php on line 310

 

My code at the moment looks like this

<?php

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  //we give the value of the starting row to 0 because nothing was found in URL
  $startrow = 0;
//otherwise we take the value from the URL
} else {
  $startrow = (int)$_GET['startrow'];
  //$startrow = $startrow+10;
// echo ("this way");
}


$SQL = "select * from student order by lastName limit $startrow, 25";

//echo ($startrow);

$retid = mysql_query($SQL, $cid);
if (!$retid && $startrow > 0) { echo( mysql_error()); }

$num_rows = mysql_num_rows($retid);

if ($num_rows == NULL && $startrow > 0)
{
	echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-25).'">Go back</a>';		
print ($startrow);
}


else {

//check if the starting row variable was passed in the URL or not

echo '<table width=60%><tr><td align=left><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-25).'"><< Prev</a></td>
<td align=right><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+25).'">Next >></a>

</tr></table>';

	echo ("<table cellpadding=4 style=font-size:1em>");
	?>
	<tr>
		<td align="center">Name</td>
		<td align="center">Email</td>
		<td align="center">Phone Number</td>
		<td align="center">Mobile Phone</td>
	</tr>
	<?php
	while ($row = mysql_fetch_array($retid)) {
		$firstName = $row["firstName"];
		$lastName = $row["lastName"];
		$mobilePhone = $row["mobilePhoneNum"];
		$phone = $row["phoneNum"];
		$email = $row["email"];
		$id = $row["studentID"];



		echo ("<tr>");


		echo ("<td>$firstName $lastName</TD>\n");
		?>
		<td align="center"> <?php echo ("$email")?></td><?php

		echo ("<td align=center>$phoneNum</td>\n");
		echo ("<td align=center>$mobilePhone</td>\n");
		echo ("<td><A HREF=\"../insertUpdateEdit/studentEdit.php?id=$id\">Edit</A></td>");
		echo ("<td><A HREF=\"displayStudent.php?id=$id&task=del\">Delete</A></td>");
		echo ("</tr>");
	 }
	echo ("</table>");
}

?>

 

If anyone could help I would REALLY appreciate

 

Cheers

Hugh

Link to comment
Share on other sites

Your problem is you cant limit when using the mysql_num_rows function, and even if it was possible, think of it logically, it would only return a miximum of 25.

$num_rows = mysql_num_rows(mysql_query("SELECT * FROM `student`"));

Link to comment
Share on other sites


Let me share you my page per page code

 

<?php

$rowsPerPage=10;
$table="table";

if ($_GET

==""){$pageNum = 1;}else{$pageNum = $_GET['page'];}

$offset = ($pageNum - 1) * $rowsPerPage;
$result  = mysql_query("SELECT COUNT(id) AS numrows FROM $table $limit_status") or die (mysql_error());
$row     = @mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$re_page = $pageNum*$rowsPerPage;
$ree_page = $re_page-$rowsPerPage+1;

// Table Start
print ("");

$result = mysql_query("SELECT * FROM $table $where") or die (mysql_error());
while($data = @mysql_fetch_array($result))
{

// Table Middle
// Use $data[name] to display stuff
print ("");

}
// Table end
print ("");

$maxPage = ceil($numrows/$rowsPerPage);

$self = "index.php?";
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
    if ($page == $pageNum)
    {
        $nav .= " $page ";
    }
    else
    {
        $nav .= " <a href=\"$self&page=$page\">$page</a> ";
    }
}

if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a href=\"$self&page=$page\">[back]</a> ";

    $first = " <a href=\"$self&page=1\">[First page]</a> ";
}
else
{
    $prev  = ' ';
    $first = ' ';
}

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 = ' ';
    $last = ' ';
}

echo "<p align='center'>($ree_page/$re_page from $numrows)  Page:<b>$first $prev $nav $next $last</b></p>";

?>

Link to comment
Share on other sites


Hello,

 

Thanks for the code, the pagination seems to calculate properly and works out the number of pages and displays the direction hyperlinks, however when outputting to the screen it displays all the records rather than limiting it to just 10.

 

I think the example above maybe missing a variable declaration - i.e $limit_status

 

Anyone see what the problem is?

 

 

<?php
require "connect.php";
$rowsPerPage=10;


if ($_GET

==""){$pageNum = 1;}else{$pageNum = $_GET['page'];}

$offset = ($pageNum - 1) * $rowsPerPage;
$result  = mysql_query("SELECT COUNT(JobID) AS numrows FROM Job $limit_status") or die (mysql_error());
$row     = @mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$re_page = $pageNum*$rowsPerPage;
$ree_page = $re_page-$rowsPerPage+1;
?><title>Page</title>
<table border="1" align="center" cellspacing="0" cellpadding="4">
  <tr>
    <th><a href="jobs.php?sort=JobID">Job ID</a></th>
    <th><a href="jobs.php">Start Date</a></th>
    <th><a href="jobs.php?sort=EndDate">End Date</a></th>
    <th><a href="jobs.php?sort=JobAddress1">Job Address 1</a></th>
    <th><a href="jobs.php?sort=JobAddress2">Job Address 2</a></th>
<th><a href="jobs.php?sort=JobTown">Town</a></th>
<th><a href="jobs.php?sort=JobCounty">County</a></th>
    <th><a href="jobs.php?sort=JobPostcode">Postcode</a></th>
    <th><a href="jobs.php?sort=Price">Price</a></th>
<th><a href="jobs.php?sort=Decision">Paid</a></th>
<th><a href="jobs.php?sort=InvoiceDate">Invoice Date</a></th>
<th><a href="jobs.php?sort=first_name">Customer Name</a></th>
<th><a href="jobs.php?sort=address1"> Customer Address</a></th>
  </tr>
<?php
print ("");

$result = mysql_query("SELECT * FROM Job $limit_status") or die (mysql_error());
while($row= mysql_fetch_array($result))
	{ ?>
<tr>
	<td align="center"><a href="jobadmin.php?JobID=<?= $row['JobID'] ?>"><?=$row['JobID']?></a></td>
	<td><?=$data['StartDate']?></td> 
	<td><?=$row['EndDate']?></td>
	<td><?=$row['JobAddress1']?></td>
	<td><?=$row['JobAddress2']?></td>
	<td><?=$row['JobTown']?></td>
	<td><?=$row['JobCounty']?></td>
	<td><?=$row['JobPostcode']?></td>
	<td><?=$row['Price']?></td>
	<td><?=$row['Decision']?></td>
	<td><?=$row['InvoiceDate']?> </td>
	<td><?=$row['first_name']?> <?=$row['surname']?> </td>
	<td><?=$row['address1']?> <?=$row['address2']?> <?=$row['town']?> <?=$row['county']?> <?=$row['postcode'] ?></td>

</tr>



<?php
print ("");

}?>
// Table end
</table>
<?php
print ("");

$maxPage = ceil($numrows/$rowsPerPage);

$self = "page.php?";
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
    if ($page == $pageNum)
    {
        $nav .= " $page ";
    }
    else
    {
        $nav .= " <a href=\"$self&page=$page\">$page</a> ";
    }
}

if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a href=\"$self&page=$page\">[back]</a> ";

    $first = " <a href=\"$self&page=1\">[First page]</a> ";
}
else
{
    $prev  = ' ';
    $first = ' ';
}

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 = ' ';
    $last = ' ';
}

echo "<p align='center'>($ree_page/$re_page from $numrows)  Page:<b>$first $prev $nav $next $last</b></p>";

?>

 

 

Many thanks

 

Link to comment
Share on other sites

I've been following this problem from the side lines, and can't see where limit_status is defined.

 

Should it be limit $currentRecordNumber, $number_of_records_per_page ?

 

I see $rowsPerPage, but where does it find out the number of the last record viewed?

 

Any help would be appreciated

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.