<?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?
Related
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
}
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);
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";
I have the following PHP script that will add a class of .Active to the current open page - this bit works but I am also trying to also add the page name to the body tag as an ID "#", but it does not seem to be working how I do it. Can anyone please advice me?
<!--add class .active to current page-->
<?php
$directoryURL = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURL, PHP_URL_PATH);
$components = explode('/', $path);
$currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));
if ($currentPage == "") {
$currentPage = "index";
}
function href($url) {
global $currentPage;
$path = explode('/', $url);
$page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
echo 'href="' . $url . '" ';
if ($page == $currentPage) {
echo 'class="active"';
}
}
?>
Here is the menu:
<li><a <?php href('index.php'); ?>>Home</a></li>
<li><a <?php href('about.php'); ?>>About</a></li>
<li><a <?php href('treatments.php'); ?>>Treatments</a></li>
And the HTML code:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
It is very simple add that variable $page in echo
echo "class='active' id='$page'";
Basically, if you don't want to modify the template, you can't.
The solution would be to do this in your template, but it must be obvious to you:
<body <?php echo $id; ?>>
What seems less obvious to you, is just that you can't do this without touching the template.
Also, you should avoid global variables.
I'm using Mick Sears' php breadcrumb script - found here:
http://www.roscripts.com/PHP_breadcrumbs-118.html
I've used this script several times with no problems. But with this one site I'm having the weirdest problem... Home page - fine. Level 1 page - fine. But every time I move to a level2 page, the correct level1 crumb is replaced by "Help". The link on the crumb is the correct one for the help page. This happens even if I clear all browser caches and don't go to the Help section of the site at all.
The site is http://www.fastexas.org. The script is there, but I gave the breadcrumb div display:none; until I can figure this out.
This script seems to have been around awhile and I'm wondering if anyone else has seen this problem.
The Breadcrumb Script:
<?php
class Breadcrumb{
var $output;
var $crumbs = array();
var $location;
function Breadcrumb(){
if ($_SESSION['breadcrumb'] != null){
$this->crumbs = $_SESSION['breadcrumb'];} }
function add($label, $url, $level){
$crumb = array();
$crumb['label'] = $label;
$crumb['url'] = $url;
if ($crumb['label'] != null && $crumb['url'] != null && isset($level)){
while(count($this->crumbs) > $level){
array_pop($this->crumbs); }
if (!isset($this->crumbs[0]) && $level > 0){
$this->crumbs[0]['url'] = "/index.php";
$this->crumbs[0]['label'] = "Home";}
$this->crumbs[$level] = $crumb;}
$_SESSION['breadcrumb'] = $this->crumbs;
$this->crumbs[$level]['url'] = null;}
function output(){
echo "<ul>";
foreach ($this->crumbs as $crumb){
if ($crumb['url'] != null){
echo "<li> <a href='".$crumb['url']."' title='".$crumb['label']."'>".$crumb['label']."</a></li> ";} else {
echo "<li class='last'>".$crumb['label']."</li> ";}}
echo "</ul>";}}
?>
Each page begins with something like:
<?php session_start();
$level= '1';
$label= 'Honors Circle';
$url= '/honors/'; include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level); ?>
or
<?php
session_start();
$level= '2';
$label= 'Districts';
$url= '/honors/district.php';
include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level);
?>
And to print the breadcrumb trail:
<div id="breadcrumb"><?php $trail->output(); ?></div>