Jump to content

Recommended Posts

Hey Guys & Girls,

 

Okay, so here's my little issue.  I'm running a WordPress site with a custom PHP script using JSON/CURL/API pulling info from an API.

 

All is good in the world in this respect.  However, the API listings that are returned are only returned in a 50 item block.  The API doesn't allow for more than 50 items per call.

 

So, what I'm looking for is a way to paginate my results so that if there are more than 50 items in the API call, it will allow the user to click to page 2, 3, 4 etc.

 

Here's my code:

<?php
//functions relating to wordpress go here:
//----------------------------------------
$bg_colors = array('green', 'orange', 'blue', 'yellow', 'red', 'black');
//----------------------------------------
//End functions relating to wordpress

// Start PetRescue PHP/API Code
//----------------------------------------
// Open CuRL/JSON Stuff

$ch = curl_init();
$category=$_GET['category'];
$url="http://www.myapilink.com.au/api/listings?token=f716909f5d644fe3702be5c7895aa34e&group_id=10046&species=".$category;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: f716909f5d644fe3702be5c7895aa34e',
));
$json = json_decode(curl_exec($ch), true);


// Functions relating to the Echo Code

foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://mysiteurl.com.au/beta/pet-info/?petid=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gallery_listing=$listing['photos'][5]['large_900'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
$unique_gallery_name="lightbox['.$inc.']";
    $inc++;
foreach($listing["photos"] as $photosthumb)
          {
            $photo_thumb_large=$photosthumb["large_340"];
			$photo_thumb_hidden=$photosthumb["small_80"];
		  }
$rand_background = $bg_colors[array_rand($bg_colors)];
	
// General IF/AND/ELSE Statements to refine the Echo Output
if($listing['photos'] == null) {
	$medium_photo="http://mysiteurl.com.au/beta/wp-content/themes/Archive/images/photo_coming_soon.png";
}
if($listing['desexed'] == "Yes") {
	$desexed="yes";
}
else {
	$desexed="no";
}
if($listing['vaccinated'] == "Yes") {
	$vaccinated="yes";
}
else {
	$vaccinated="no";
}
if($listing['wormed'] == "Yes") {
	$wormed="yes";
}
elseif($listing['wormed'] == "No") {
	$wormed="no";
}
else {
	$wormed="no";
}
if($listing['heart_worm_treated'] == "Yes") {
	$heart_worm_tested="yes";
}
elseif($listing['heart_worm_treated'] == "No") {
	$heart_worm_tested="no";
}
else {
	$heart_worm_tested="no";
}
if($listing['species'] == "Dog") {
	$adoption_enquiry_link="http://mysiteurl.com.au/beta/pre-adoption-form-dogs/?dog_name=$name";
	$hwt="list-$heart_worm_tested";
}
elseif($listing['species'] == "Cat") {
	$adoption_enquiry_link="http://mysiteurl.com.au/beta/pre-adoption-form-cats/?cat_name=$name";
	$hwt="list-hwt-hidden";
}

// Echo the output

echo'<div class="animal">
		<div class="animal-image">
<a class="size-thumbnail thickbox" rel="'.$unique_gallery_name.'" href="'.$medium_photo.'">
				<img src="'.$medium_photo.'" class="image-with-border" alt="">
				<div class="border" style="width: 340px; height: 340px;">
					<div class="open"></div>
				</div>
			</a>
		<div class="item-title-bg '.$rand_background.'">
				<h2 class="entry-title">'.$listing['name'].'</h2>
				<div class="animal-adopt-button">
	<a href="'.$adoption_enquiry_link.'" style="background-color: #575757; border-color: #494949; background-position:5px 0;" class="button medium">Enquire about '.$name.'</a>
				</div>		
			</div>		
		</div>		
		<div class="animal-thumbnail hidden">
<a class="lightbox" rel="'.$unique_gallery_name.'" href="'.$photo_thumb_large.'">
					<img class="animal-thumbnail" src="'.$photo_thumb_hidden.'" >
					</a>
		</div>		
<div class="animal-content">
	<div class="animal-left">
		<ul class="animal-list">
			<li class="list-sex-'.$gender_class.'">'.$listing['gender'].'</li>
			<li class="list-breed-'.$breed_class.'">'.$listing['breeds_display'].'</li>
			<li class="list-age">'.$listing['age'].'</li>
			<li class="list-fee">'.$listing['adoption_fee'].'</li>
		</ul>
	</div>
<div class="animal-right">
		<ul class="animal-list">
			<li class="list-'.$desexed.'">Desexed?</li>
			<li class="list-'.$vaccinated.'">Vaccinated?</li>
			<li class="list-'.$wormed.'">Wormed?</li>
			<li class="'.$hwt.'">Heart Worm Tested?</li>
		</ul>
</div>
<div class="animal-full">
		<ul class="animal-list">
			<li class="list-description">'.$listing['personality'].'</li>
		</ul>
</div></div>
<div class="clearer"></div>
</div>
<div class="delimiter"></div>';
// Close the CURL
}
curl_close($ch);
?>

Attached is some dummy data that represents the JSON data returned from the API.

 

As you can see in the file, the last line is where the paging details comes in.

 

Any help you guys could give would be appreciated.

 

Cheers,

 

Dave

listings.txt

Link to comment
https://forums.phpfreaks.com/topic/287836-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.