I have been trying to get my PHP script return two different pages namely: get2015 and get2016. The aim is to have these two pages have pagination links below them.
To achieve this i have written two scripts in two different files that look like this:
file 1 = functions.php
<?php
include "data.php";
////get results for the year 2015
function get2015(){
$con = dbConnect();
$query = "select * from manuscript where year = 2015";
$sql=$con->prepare($query);
$sql->execute();
$total = $sql->rowCount();
$refs_per_page = 20;
$pages = ceil($total/$refs_per_page);
echo "$total <br>";
//
if (isset($_GET['page']) && is_numeric($_GET["page"])){
$page = (int) $_GET['page'];
}
else {
$page = 1;
} //
if ($page > $total) {
$page = $total;
} //
if ($page < 1) {
$page = 1;
} //
$offset = ($page - 1) * $refs_per_page;
//
$query = "select * from manuscript where year = 2015 limit $offset, $refs_per_page";
$sql=$con->prepare($query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
while ($row=$sql->fetch()){
$title = $row['title'];
$authors = $row['authors'];
echo "<b>$title</b>" . "<br>" . $authors . "<p>";
}
//
for($x=1; $x<=$pages; $x++){
?><a href = "index.php?page= <?php echo $x;?>" style="text-decoration:none"> <?php echo $x;?> </a> <?php
}
}
//get results for the year 2016
function get2016(){
$con = dbConnect();
$query = "select * from manuscript where year = 2016";
$sql=$con->prepare($query);
$sql->execute();
$total = $sql->rowCount();
$refs_per_page = 20;
$pages = ceil($total/$refs_per_page);
echo "$total <br>";
//
if (isset($_GET['page']) && is_numeric($_GET["page"])){
$page = (int) $_GET['page'];
}
else {
$page = 1;
} //
if ($page > $total) {
$page = $total;
} //
if ($page < 1) {
$page = 1;
} //
$offset = ($page - 1) * $refs_per_page;
//
$query = "select * from manuscript where year = 2016 limit $offset, $refs_per_page";
$sql=$con->prepare($query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
while ($row=$sql->fetch()){
$title = $row['title'];
$authors = $row['authors'];
echo "<b>$title</b>" . "<br>" . $authors . "<p>";
}
//
for($x=1; $x<=$pages; $x++){
?><a href = "index.php?page= <?php echo $x;?>" style="text-decoration:none"> <?php echo $x;?> </a> <?php
}
}
?>
file 2: Index.php looks like this
<?php
include "functions.php";
$page = $_GET["page"];
if($page){
if($page=="get2015"){
get2015();
}
if($page=="get2016"){
get2016();
}
}
?>
<html>
Archive<br>
<a href="index.php?page=get2015"> 2015</a><br>
<a href="index.php?page=get2016"> 2016</a><br>
</html>
<?php ?>
How do iget both funcctions to return paginated pages that work?