pagination with the Angellist API - php

I am a little stuck on how to show the Angellist API records on the page with pagination.
I succesfully got the first 50 records of the Angellist API showing on the page. When i change the number of $page to 2 and print $data, i do get the 50 records of page 2.
But now i don't know how i can do that automatically so i can get the pagination going.
This is the code i have until now:
<?php
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$jsonurl = 'https://api.angel.co/1/jobs/?page=' . $page;
$url = file_get_contents($jsonurl);
$data = json_decode($url,true);
$total = $data['total'];
$per_page = $data['per_page'];
$current = $data['page'];
$last_page = $data['last_page'];
?>
<div class="col-md-12" style="padding:0;">
<div class="col-md-6">
<div class="pagination">
<?php
$i = 0;
while($page < $last_page) :?>
<?php echo "<a href=\"/jobs?page=".$page++."\"\>Next 50</a>";
endwhile;
?>
</div>
</div>
I hope this is sufficient information for some help, but if you need more info i'm happy to give it.

You could a ternary with $_GET -
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
and then your url would look like
yourpage.php?page=2
And you can create a next link -
echo "Next 50";

Related

php pdo pagination - how to hide the 'next page' button when you get to the last page

I'm trying to hide the "next page" button when I get to the last page, but I can't figure out how to do this. The other buttons work as expected.
I was following a tutorial and I came across the pagination, in the tutorial the person left the buttons always showing, but I don't think it's a good idea, so I hide some buttons depending on the situation on the page.
When there is no post on the page no button should appear - OK that worked
If there is at least one post the 'next page' button should appear alone - OK that worked
If you go to page 2 or beyond the 'Go to first page' and 'Back page' buttons should appear - OK that worked
The 'next page' button should disappear when I get to the last page, but it keeps going on and on
How i can do that?
Thanks
<?php
//pagination offset limit
$limit = 10;
$offset = ($PAGE['page_number'] - 1) * $limit;
$query = "select * from categories order by id desc limit $limit offset $offset";
?>
<!--my buttons-->
<div class="col-md-12 button-pos-x">
<?php if($offset>1):?>
<a href="<?=$PAGE['first_link']?>">
<button class="custom-btn btn-12"><span>Go to first page</span></button>
</a>
<a href="<?=$PAGE['prev_link']?>">
<button class="custom-btn btn-12 button2-pos-x"><span>Back page</span></button>
</a>
<?php endif;?>
<?php if(($offset>=1) || ($offset<$limit)):?>
<a href="<?=$PAGE['next_link']?>">
<button class="custom-btn btn-12 button2-pos-x"><span>Next page</span></button>
</a>
<?php endif;?>
</div>
//my pagination function
function get_pagination_vars()
{
/** set pagination vars **/
$page_number = $_GET['page'] ?? 1;
$page_number = empty($page_number) ? 1 : (int)$page_number;
$page_number = $page_number < 1 ? 1 : $page_number;
$current_link = $_GET['url'] ?? 'home';
$current_link = ROOT . "/" . $current_link;
$query_string = "";
foreach ($_GET as $key => $value)
{
if($key != 'url')
$query_string .= "&".$key."=".$value;
}
if(!strstr($query_string, "page="))
{
$query_string .= "&page=".$page_number;
}
$query_string = trim($query_string,"&");
$current_link .= "?".$query_string;
$current_link = preg_replace("/page=.*/", "page=".$page_number, $current_link);
$next_link = preg_replace("/page=.*/", "page=".($page_number+1), $current_link);
$first_link = preg_replace("/page=.*/", "page=1", $current_link);
$prev_page_number = $page_number < 2 ? 1 : $page_number - 1;
$prev_link = preg_replace("/page=.*/", "page=".$prev_page_number, $current_link);
$result = [
'current_link' =>$current_link,
'next_link' =>$next_link,
'prev_link' =>$prev_link,
'first_link' =>$first_link,
'page_number' =>$page_number,
];
return $result;
}
//my init function
$PAGE = get_pagination_vars();
I can't understand how to make the next page button hide when I get to the last page

reducing number of php pagination links to 10

Ive been building a data driven website displaying general info about countries in the world. Its been made so that 1 country is displayed per page and you can move the next country by clicking a pagination link. Only problem that I am having is I cannot limit the amount of visible links. I did try this for loop with the first line as this: for ($i = $Page; $i <= min($Page + 9, $TotalRecords); $i++) { which does reduce it to the 10 records however this does result in the website breaking when I test the web address by entering index.php?page=aa.
<nav class="mt-5">
<ul class="pagination pagination-lg justify-content-center">
<?php
if( isset($Page) ) {
if ($Page > 1 ) {
?>
<li class="page-item">
«
</li>
<?php
}
}
?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM countriesinfo";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalRecords = array_shift($RowPagination);
$RecordPagination = $TotalRecords / 1;
$RecordPagination = ceil($RecordPagination);
for ($i = $Page; $i <= $RecordPagination; $i++) {
if( isset($Page) ) {
if ($i == $Page)
{
?>
<li class="page-item active">
<?php echo $i; ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php
}
}
}
?>
<?php if (isset($Page) && !empty($Page) ) {
if ($Page+1 <= $RecordPagination) {
?>
<li class="page-item">
»
</li>
<?php
}
}
?>
</ul>
</nav>
I test the web address by entering index.php?page=aa.
You can check to make sure the get variable is a number by using php is_numeric() bulit in function
and if it isn't force it to number 1.
This will stop the website from breaking when someone manipulate the page varible.
Here is a code to help you out, put at the beginning of the page.
if(is_numeric($page)){
$page = $page; // if $page is a number do nothing
}else{
$page = 1; //else set $page to 1 which will fetch data from beginning
}

Glob pagination rewrite url

Update
Tested my script outside Wordpress and it works. So the problem with this pagination is inside Wordpress. I did some research and I added this rewrite code to my functions file, but still it doesn't work
function my_rewrite_globe_pagination()
{
add_rewrite_rule('^newspaper/page/([0-9]{1,})/?', 'index.php?paged=$matches[1]', 'top');
}
add_action('init', 'my_rewrite_globe_pagination');
The next button url looks like this: domain/page-name/?page=2
URL in browser after clicking next: domain/page-name/page/2/
Before update
first of all I would like to say I read all the similar questions on stackoverflow but I din't find the solution fitting to my case.
What I'm trying to achieve is a globe numeric pagination at my wordpress page. In my page template I have the code listed below and I have with this code two problems:
one is that I don't know how to change the prev and next buttons to numeric pagination
second whenever I click on the next button I see the same two images as on first page, despite the fact that my url changes to domain/page-name/2/
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$base_dir = trailingslashit(wp_upload_dir()['basedir']);
$base_url = wp_upload_dir()['baseurl'];
$dir_jpg = '/newspaper/jpg/';
$dir_pdf = '/newspaper/pdf/';
$images = glob($base_dir.$dir_jpg.'*.*');
$limit = 2;
$total =count($images);
$total_pages = ceil($total/$limit);
$offset = ($page-1) * $limit;
$images = array_slice($images, $limit);
foreach($images as $image) {
$url = $base_url.$dir_jpg.basename($image);
$filename = substr(basename($image), 0, -4);
$pdfs = $base_url.$dir_pdf.$filename.".pdf";
printf('<img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div>', esc_url($url));
}
?>
<ul class="pagination">
<li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
</ul>
For the first problem you simply have to print $page - 1 and $page + 1 instead "Prev" and "Next" (and check if the current page is the first or the last):
<ul class="pagination">
<li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
<?php echo ($page <= 1) ? "X" : ($page - 1)?>
</li>
<li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
<?php echo ($page >= $total_pages) ? "X" : ($page + 1)?>
</li>
</ul>
The second problem is caused by your incorrect call to array_slice.
According to the documentation, the second parameter must be the start offset of the array ($offset in your case) and the third the length of the array you want ($limit), so this should fix it:
$images = array_slice($images, $offset, $limit);

Pagination on the API Indeed.com xml feed 25 results per page, How to Paginate?

I have set up the Indeed.com xml feed on my site. Their API only allows 25 results per query. How can I paginate the results if there are more than 25?
I have not found a satisfactory or thorough enough answer anywhere online. I've searched for weeks on this.
Here is what I have in my code:
PHP:
// Indeed.com API URL parameters
$url = 'http://api.indeed.com/ads/apisearch'.'?';
$publisher = 'xxxxxxxxxxxxxxxx';
$q = $query;
$location = '';
if (isset($_POST['location'])) {
$location = $_POST['location'];
} else {
$geo = geoCheckIP($_SERVER['REMOTE_ADDR']);
if (isset($geo) && ($geo != "not found, not found")) {
$location = $geo;
}
}
$sort = 'date';
$radius = '20';
$st = '';
$jt = '';
$start = '0';
$limit = '25';
$fromage = '';
$highlight = '0';
$filter = '1';
$latlong = '0';
$co = 'us';
$chnl = '';
$userip = $_SERVER['REMOTE_ADDR'];
$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? ($_SERVER['HTTP_USER_AGENT']) : 'unknown';
$v = '2';
$xml = simplexml_load_file($url."publisher=".$publisher."&q=".$q."&l=".$location."&sort=".$sort."&radius=".$radius."&st=".$st."&jt=".$jt."&start=".$start."&limit=".$limit."&fromage=".$fromage."&highlight=".$highlight."&filter=".$filter."&latlong=".$latlong."&co=".$co."&chnl=".$chnl."&userip=".$userip."&useragent=".$useragent."&v=".$v);
HTML BODY
<div class="paradiv">
<h1><?php echo $xml->totalresults . " " . $jobroll_title . " Near " . $location ?></h1>
<!-- BEGIN INDEED ORDERED LIST-->
<ol class="jobs">
<?php
foreach($xml->results->result as $result) { ?>
<li class="job <?php echo (++$liBgColor%2 ? 'odd' : 'even'); ?>">
<div class="title_wrapper">
<div id="jobtitle"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank"><?php echo $result->jobtitle;?></a></strong></div>
<div id="company"><?php echo $result->company;?></div>
</div>
<div id="snippet">
<?php $result->snippet = str_replace(" ", ". ", $result->snippet); echo $result->snippet;?>
</div>
<div id="location"><strong>Location:</strong> <?php echo $result->formattedLocationFull;?></div>
<div id="date"><span class="posted <?php echo (++$locationBgColor%2 ? 'even' : 'odd'); ?>">Posted <?php echo $result->formattedRelativeTime;?></span></div>
<div id="details-2"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank">Details</a></strong></div>
</li>
<?php } ?>
</ol>
<!-- END INDEED ORDERED LIST -->
<!-- THIS IS WHERE THE PAGINATION WILL DISPLAY -->
<div class="pagenumber"><?php echo "Page Number " . "" . $xml->pageNumber . "" ?></div>
</div>
This is how it works. A user arrives on the web page, then the page loads with the job results based on the users location. If less than 25 results are found for their zip code then there is no problem and pagination isn't needed.
But if the xml feed has more than 25 results, it will show 25 and that's it. If I want to display the rest, I have to paginate. This is what I need help with.
Here is how their API url works.
http://api.indeed.com/ads/apisearch?publisher=xxxxxxxxxxxxxxx&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=0&limit=25&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2
The part that says &start=0&limit=25 is how to display the results based on page number of the xml.
So for example: &start=0&limit=25 would be page 0 showing 25 results, &start=25&limit=25 would be page 1 showing the next 25 results and &start=50&limit=25 would be page 2 showing the remaining 25 results. This example is based on if there are a total of 75 results in the xml feed.
And in my // Indeed.com API URL parameters above I have it set to start on page 0 and limit to 25. They do not allow beyond 25 in limit. If set higher it will default to 25.
$start = '0';
$limit = '25';
I need some help on implementing a way to paginate using my current PHP code above. How can I add on to what I have in my PHP code?
has_more function returns true if there are 25 results in the xml
$start = 0;
do {
$xml = simplexml_load_file(...$start...);
// process $xml
$start += 25;
} while(has_more($xml));

My pagination isn't working properly. Help!

<?php
// Code not directly relevant omitted (including lots of vars)
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "First";
$prev = "Prev";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "Next";
$last = "Last";
}
?>
<html>
<title></title>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $slots; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
EDIT/UPDATE:
I just realized I had a file called test.php a while back. I deleted it but I guess it's still in my site somehow... Nevertheless, I changed the word test with works. Now when I click next, it brings me to mydomain.com/works.php?page=2. But it shows a 404 error :/
Can somebody please tell me where I screwed up? Thanks!
The code certainly looks good; are you sure the problem is in the links being generated? Maybe you are being redirected from the "correct" page for some other reason?

Categories