Jump to content

Recommended Posts

I am testing out an API which needs pagination and got a pagination script which partly does what I want. At the moment it's displaying just 10 results by default per page and worked it to display the number of pages e.g 150 results displays PREV 1 2 3 4 5 NEXT.

The problem is when I click on next or page 2 etc, it displays no result. Here is the code...need help please. Many thanks.

<?php
    include("class/class_api.php");
    include("class/class_api_doctor.php");

extract($_POST);
    extract($_GET);
    
    $doctorApi = new DoctorApi();
    $doctorApi->start_list = 1;
    $doctorApi->limit_list = 10;  //10 results per page max
    $doctorApi->first_name = $first_name;
    $doctorApi->last_name = $last_name;
    $doctorApi->state = $state;
    $doctorApi->region = $region;
    $doctorApi->specialty = $specialty;
    $doctorApi->insurance = $insurance;
    $doctorApi->zipcode = $zipcode;
    $doctorApi->radius = $radius;
    $doctorApi->gender = $gender; // M or F
    $doctorApi->language = $language; 
    
    $doctorsApi = $doctorApi->api_readContents($doctorApi->api_getContents());
$total_pages = $doctorApi->total;
$limit = $doctorApi->limit_list;
// How many adjacent pages should be shown on each side?
	$adjacents = 3;


/* Setup page vars for display. */
if ($page == 0) $page = 1;					//if no page var is given, default to 1.
$prev = $page - 1;							//previous page is page - 1
$next = $page + 1;							//next page is page + 1
$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;						//last page minus 1

if($doctorApi && $doctorApi->total > 0) echo "<br>We found ".$doctorApi->total." Doctor for your Search <br>";
    if($doctorsApi) {
        foreach($doctorsApi as $doctor) {
            echo "<div class=\"Doctor\">";
            echo "<a href=\"".$doctor->profilelinkurl."\">";
            echo "<span class=\"name\">".$doctor->prefix.$doctor->first_name.$doctor->last_name.$doctor->credential."</span>";
            echo "<span class=\"specialty\">".$doctor->speciality."</span>";
            echo "<span class=\"address\">".$doctor->streetaddress."</span>";
            echo "<span class=\"address\">".$doctor->city.", ".$doctor->state." ".substr($doctor->zipcode,0,5)."</span>";
            echo "</a>";
            echo "</div>";
        }
    }

/* 
	Now we apply our rules and draw the pagination object. 
	We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{	
	$pagination .= "<div class=\"pagination\">";
	//previous button
	if ($page > 1) 
		$pagination.= "<a href=\"search_results2.php?page=$prev\">« previous</a>";
	else
		$pagination.= "<span class=\"disabled\">« previous</span>";	

	//pages	
	if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
	{	
		for ($counter = 1; $counter <= $lastpage; $counter++)
		{
			if ($counter == $page)
				$pagination.= "<span class=\"current\">$counter</span>";
			else
				$pagination.= "<a href=\"search_results2.php?page=$counter\">$counter</a>";					
		}
	}
	elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
	{
		//close to beginning; only hide later pages
		if($page < 1 + ($adjacents * 2))		
		{
			for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"search_results2.php?page=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"search_results2.php?page=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"search_results2.php?page=$lastpage\">$lastpage</a>";		
		}
		//in middle; hide some front and some back
		elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
		{
			$pagination.= "<a href=\"search_results2.php?page=1\">1</a>";
			$pagination.= "<a href=\"search_results2.php?page=2\">2</a>";
			$pagination.= "...";
			for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"search_results2.php?page=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"search_results2.php?page=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"search_results2.php?page=$lastpage\">$lastpage</a>";		
		}
		//close to end; only hide early pages
		else
		{
			$pagination.= "<a href=\"search_results2.php?page=1\">1</a>";
			$pagination.= "<a href=\"search_results2.php?page=2\">2</a>";
			$pagination.= "...";
			for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"search_results2.php?page=$counter\">$counter</a>";					
			}
		}
	}

	//next button
	if ($page < $counter - 1) 
		$pagination.= "<a href=\"search_results2.php?page=$next\">next »</a>";
	else
		$pagination.= "<span class=\"disabled\">next »</span>";
	$pagination.= "</div>\n";		
}


    
echo "<br clear='both'><div>" .$pagination."</div>";
?>

Link to comment
https://forums.phpfreaks.com/topic/159296-php-pagination-help/
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.