Hi,
I have a page that queries two databases in order to display all properties, the tables are comm_prop and res_prop.
My page listing is a combination of the two results and can get rather long. Now i would like to add pagenation to limit the page to display 10 with next and previous etc.
But i have only used pagenation with 1 query result set before and i have tried adapting it to suit 2 but the page numbers get all over the place.
This is the pagenation script i have been using
$per_page = 5;
//
if(!isset($_GET['page'])){
$page1 = 1;
}else{
$page1 = $_GET['page'];
}
//set up the variables for the prev and next links
$prev_page1 = $page1 - 1;
$next_page1 = $page1 + 1;
//get the data from the database
$result = mysql_query($sql);
/*set up specified page, so we have set $per_page to 5 we need to figure out the starting page based on the amount of data recieved from the database. mysql_num_rows() will get the no. of rows in the result from our mysql_query*/
$page_start1 = ($per_page * $page1) - $per_page;
$num_rows1 = mysql_num_rows($result);
/*once we determine the amount of data based on the number of rows in our database, we will check it against the per-page limit and display the appropiate number of pages. For example say we have 15 entries in our dtabase and our $per_page is set to 5 we would have to render 3 pages of stats. We do this by using hte $num_pages variable*/
if ($num_rows1 <= $per_page){
$num_pages1 = 1;
}elseif (($num_rows1 % $per_page1) == 0) {
$num_pages1 = ($num_rows1 / $per_page);
}else{
$num_pages1 = ($num_rows1 / $per_page) + 1;
}
$num_pages1 = (int) $num_pages1;
if ($page1 > $num_pages1 || $page1 < 0) {
echo 'You have specified an invalid page number'
;}
//now we will use the LIMIT clause to tell mysql to limit the amount of records returned
$sql = $sql." LIMIT $page_start1, $per_page";
I then repeat this for the next query but change the variable names ie. $num_pages instead of $num_pages1 and so on.
Below is what i out put in the body of my page
$prev_page2 = ($prev_page1 + $prev_page);
$page2 = ($page1 + $page);
$num_pages2 = ($num_pages1 + $num_pages);
$next_page2 = ($next_page1 + $next_page);
$first_num2 = ($first_num1 + $first_num);
$last_num2 = ($last_num1 + $last_num);
$num_rows2 = ($num_rows1 + $num_rows);
// previous link. we need a if statement to determine if prev page is not equal to 0, then echo the url for the link
if ($prev_page2 != 0){
echo '<a href="buy_property.php?page='.$prev_page2.'">Previous</a> | ';
}
//displays the next link
if ($page2 != $num_pages2) {
echo ' <a href="buy_property.php?page='.$next_page2.'">Next</a> ';
}
$first_num2=($prev_page2+1);
$last_num2=($next_page2);
Basically i add the two value together to give me a new variable which i use to determine the page lenght .
Does this make sense or is there a better script out there that can handle this better.
Thanks