Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

I'm a little bit confused about pagination with php

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 &rsaquo;&rsaquo;</a></li>";
} ?>

Whenever I click on next page, it shows a blank index.php?page_no=2 with no content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...