Reputation: 109
I have an index.php
page from where I have included other pages with if statements as shown below.
<?php
if(isset($_GET['dashboard'])) {
include("dashboard.php");
}
if(isset($_GET['view_staffs'])) {
include("view_staffs.php");
}
if(isset($_GET['delete_staff'])){
include("delete_staff.php");
}
if(isset($_GET['edit_staff'])){
include("edit_staff.php");
}
if(isset($_GET['group_companies'])){
include("group_companies.php");
}
?>
I added paginations to the view_staff
page with the url loking like index.php?view_staff
so on adding the pagination code,
<?php
if($page_no > 1){
echo "<li><a href='?page_no=1'>First Page</a></li>";
} ?>
<li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
<a <?php if($page_no > 1){
echo "href='?page_no=$previous_page'";
} ?>>Previous</a>
</li>
<li <?php if($page_no >= $total_no_of_pages){
echo "class='disabled'";
} ?>>
<a <?php if($page_no < $total_no_of_pages) {
echo "href='?page_no=$next_page'";
} ?>>Next</a>
</li>
<?php if($page_no < $total_no_of_pages){
echo "<li><a href='?page_no=$total_no_of_pages'>Last ››</a></li>";
} ?>
Whenever I click on next page, it shows a blank index.php?page_no=2
with no content
Upvotes: 1
Views: 132
Reputation: 33823
Essentially you need to do two things to ensure your code works as you want it to. You need to obtain a reference the the current page ( page_no
) from your querystring to enable basic calculations to deduce first
,previous
,next
and last
pages and also to combine that with any pre-existing querystring values.
A simple PHP function that could be of use ( not tested extensively ) takes any existing variables from the GET array and combines them with a user-supplied array of parameter/value pairs. If there is a parameter supplied to the function that already exists in the querystring it will overwrite the previous version and use the newly supplied version. It returns a formatted string for use in the hyperlinks in the menu structure.
function createquerystring( $args=array() ){
$req=array_merge( $_GET, $args );
return http_build_query( $req );
}
One thing I would say - your code sets various querystring parameters such as view_staffs
or delete_staff
whereas I'd suggest setting a single parameter, such as task
and then assigning view_staffs
as the value etc. ie ?page_no=2&task=view_staffs
~ it would make it easier to keep track of things and besides - the parameter really needs a value. A word of warning though - including files based upon querystring can be inherently dangerous so you should put in place a mechanism to check what is being passed in as a parameter to be included!
An example that grabs a page_no
parameter if present and assigns to the $page
variable which is then used to compare against 1 or $total_no_of_pages
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
<style>
body *{
box-sizing:border-box;
transition: all 250ms ease-in-out;
}
ul > li {
display: inline-block;
zoom:1;
margin:0 0.5rem;
border:1px dotted grey;
padding:0.5rem;
min-width:80px;
text-align:center;
}
ul > li:hover{
background:whitesmoke;
cursor:pointer;
}
.disabled{
background:rgba(100,0,0,0.1);cursor:not-allowed!important;
}
</style>
</head>
<body>
<ul>
<?php
function createquerystring( $args=array() ){
$req=array_merge( $_GET, $args );
return http_build_query( $req );
}
$total_no_of_pages=10;
# Obtain a reference to the page_no parameter from the querystring or set as 1 if it is not present
$page=isset( $_GET['page_no'] ) ? $_GET['page_no'] : 1;
# The menu will be within a simple UL element - a basic pattern using placeholders
$pttn='<ul>%s %s %s %s</ul>';
$first=$page==1 ? '<li class="disabled">First</li>' : sprintf( '<li><a href="?%s">First</a></li>', createquerystring( array( 'page_no' => 1 ) ) );
$prev=$page > 1 ? sprintf( '<li><a href="?%s">Previous</a></li>', createquerystring( array( 'page_no' => $page - 1 ) ) ) : '<li class="disabled">Previous</li>';
$next=$page < $total_no_of_pages ? sprintf( '<li><a href="?%s">Next</a></li>', createquerystring( array( 'page_no' => $page + 1 ) ) ) : '<li class="disabled">Next</li>';
$last=$page >= $total_no_of_pages ? '<li class="disabled">Last</li>' : sprintf( '<li><a href="?%s">Last</a></li>', createquerystring( array( 'page_no' => $total_no_of_pages ) ) );
# print the menu - substitute the placeholders for the named variables
printf( $pttn, $first, $prev, $next, $last );
?>
</ul>
</body>
</html>
Touching on the comment of including files and needing to ensure they are bonafide what you could do to aid the inclusion process would be to create an array containing the querystring parameter and the associated file and test if the parameter exists as a key...
<?php
if( !empty( $_GET['task'] ) ){
$task=filter_input( INPUT_GET, 'task', FILTER_SANITIZE_STRING );
$allowed=array(
'dashboard' => 'dashboard.php',
'view_staffs' => 'view_staffs.php',
'delete_staff' => 'delete_staff.php',
'edit_staff' => 'edit_staff.php',
'group_companies' => 'group_companies.php'
);
if( in_array( $task, array_keys( $allowed ) ) ){
$file=$allowed[ $task ];
require $file;
}
}
?>
Upvotes: 1
Reputation: 753
whenever i click on next page, it shows a blank index.php?page_no=2 with no content
yes, this is exactly what you have coded
echo "href='?page_no=$next_page'";
you have none of your paths like "dashboard" included in the URL - so how should the script know what to include?
All
if(isset($_GET['xxxxx'])) {
include("xxxxxxx.php");
wil therefore fail.
Use chrome devtools to see the GET and POST variables or dump the $_GET array if you are not sure what is available to your script.
As a start you will need to conserve the current path/url by using e.g. $_SERVER['PHP_SELF'] and use it as additional argument for the pagination
echo "<a href='{$_SERVER['PHP_SELF']}?page_no={$next_page}'>NEXT PAGE</a>";
Upvotes: 1