Hi all.
I've worked out how to show records over multiple pages but unfortunately it requires two queries to be executed, one to get the total count and one to add the limit values.
Since my my queries are always unique on every page (and there's lots), is there anyway to use the one query yet still get the total count of records and also display the information 10 records at a time?
See code below:
I've worked out how to show records over multiple pages but unfortunately it requires two queries to be executed, one to get the total count and one to add the limit values.
Since my my queries are always unique on every page (and there's lots), is there anyway to use the one query yet still get the total count of records and also display the information 10 records at a time?
See code below:
Code:
$page_name="view_active.php"; // If you use this code with a different page ( or file ) name then change this
if(!isset($start)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start - 0);
$limit = 10; // No of records to be shown per page.
$this = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
///////////// Now let us start executing the query with variables $eu and $limit set at the top of the page///////////
$query=" SELECT * FROM leads LIMIT $eu, $limit ";
$querycount = "SELECT * FROM leads";
#Any way to execute the query just once.
$result=mysql_query($query);
$num_rows=mysql_num_rows($result);
echo mysql_error();
//////////////// Now we will display the returned records in side the rows of the table/////////
#Here I am displaying the content?
////////////////////////////// End of displaying the table with records ////////////////////////
/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=$querycount;
$result2=mysql_query($query2);
echo mysql_error();
$num_rows=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) {
print "<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>";
}
//////////////// Let us display the page links at center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $num_rows;$i=$i+$limit)
{
if($i <> $eu)
{
echo " <a href='$page_name?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
}
else
{
echo "<font face='Verdana' size='4' color=red>$l</font>";
} /// Current page is not displayed as link and given font color red
$l=$l+1;
}
echo "</td><td align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this < $num_rows) {
print "<a href='$page_name?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td></tr></table>";