Jump to content

Recommended Posts

Hello,

 

I am a newbie regarding php, I created a website with a MySql database, I have added pagination. Everything is workign fine. One function I dont get working properly.

 

I want to have the max of pages between the <<prev and the next>> button limited to 10, So like google.. <<prev 1,2,3,4,5,6,7,8,9,10 next>> now its like <<prev 1,2,3,4,5,6,7,8,9,10,11,12,13,14 next>>  etc etc.  Ill hope someone can help me out.

 

<?php
require_once('./include/connect.php'); 


$page = $_REQUEST["page"];
$amount = 10;
$start = $page * $amount;
$currentpage = $page + 1;


$queryamount = $amount + 1;
$query = "SELECT date, id1, id2, id3, id4, id5, id6, id7 FROM member LIMIT $start, $queryamount";
$response = mysqli_query($link, $query);


$query2 = "SELECT COUNT(*) as total FROM wotd_id";
$response2 = mysqli_query($link, $query2);
$row2 = mysqli_fetch_assoc($response2);
$totalpages = ceil($row2['total'] / $amount);


if($response){


echo "<center>(You are on Page <b>$currentpage</b> of <b>$totalpages </b>) </center>";


    $result = mysqli_query ($link,"select count(1) FROM member");
    if($result) {
            $row = mysqli_fetch_array($result);
            $total = $row[0];
            echo "<center>Currently there are <b>$total</b> entry's in the Database </center>";
    } 


$i = $amount;
while($row = mysqli_fetch_array($response)){
$i--;
if($i < 0)
break; 


}

if($page > 0) {
    
    echo "<a href= '" . $_SERVER["PHP_SELF"] . "?page=" . ($page-1) . "' class='button'><b> << Prev</b></a>&nbsp";
}

for($i = 1; $i <= $totalpages; $i++) {
    //echo "<a href= '" . $_SERVER['PHP_SELF'] . "?page=" . ($i-1) . "'>";
    if($i == $currentpage) {




    echo "<span class=active_page>[" . $i . "]</span>";
        
    } else 
        {
        
        //echo "<span class = page_nonactive>" $i "</span>";
        
echo "<a href= '" . $_SERVER['PHP_SELF'] . "?page=" . ($i-1) . "'>";


        echo "<span class=nonactive>" .$i . "</span>";


    } 
    echo "</a> ";    
}
 for($i = $currentpage + 1; $i <= min($currentpage + 11, $total_pages); $i++)
    if($currentpage < $totalpages) {
        echo "<a href='" . $_SERVER["PHP_SELF"] . "?page=" . ($page+1) . "' class='button'><b>Next >> </b></a>&nbsp";




    }


    
} else {
    
    echo "Couldn't issue database query";
    
    echo mysqli_error($link);
}
    
mysqli_close($link);


?>

 

An easier way is to set a page link offest. E.g. the pages in the pagination list will be +/- 5 pages from the current page. That means if you are on either end of the total pages one side will be less than the offset.

 

There are quite a few problems and inefficiencies with your code. Here is a quick rewrite - have not tested so there may be some minor errors. Read the comments to understand what is happening. There are a lot of other improvements I would make to this, but did not want to invest the time.

<?php
 
//------ Configuration variables --------
$recordsPerPage = 10;  //Define the records per page
$pageLinkOffset = 5;   //Offset for displayed pages
//---------------------------------------
 
//Connect to DB
require_once('./include/connect.php'); 
 
//Get the total number of records and pages
$query = "SELECT COUNT(*) as record_count FROM member";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_assoc($result);
$totalRecords = $row['record_count'];
$totalPages = ceil($totalRecords / $recordsPerPage);
 
//Get the requested page
$page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
//If requested page is not within the range of available pages, set to page 1
if($page<1 || $page>$totalPages) {
    $page = 1;
}
 
//set limit start value and run query to get records
$limitStart = ($page-1) * $recordsPerPage;
$query = "SELECT date, id1, id2, id3, id4, id5, id6, id7
          FROM member LIMIT {$limitStart}, {$recordsPerPage}";
$result = mysqli_query($link, $query);
 
//Create the output
$output = "";
$output .= "<center>(You are on Page <b>{$page}</b> of <b>{$totalPages}</b>)</center>\n";
$output .= "<center>Currently there are <b>{$totalRecords}</b> entry's in the Database</center>";
 
//Iterate through the results to create the output
while($row = mysqli_fetch_assoc($result)) {
    //Do something with $row to create output for each record
    $output .= "";
}
 
 
//Create pagination links
$paginationLinks = ""; //Ouput variable
//Define start/end pages to show in links based on
//  offset and current page
$pageLinkStart = max($page-$pageLinkOffset, 1);
$pageLinkEnd   = min($page+$pageLinkOffset, $totalPages);
 
//Make the root url for action attribute safe
$pageURL = htmlentities($_SERVER['PHP_SELF']);
 
//Create prev page link
if($page > 1) {
    $prevPage = ($page-1);
    $paginationLinks .= "<a href='{$pageURL}?page={$prevPage}' class='button'><b> << Prev</b></a>&nbsp\n";
}
//Create links for pages
for($pageNo = $pageLinkStart; $pageNo <= $pageLinkEnd; $pageNo++) {
    if($page == $pageNo) {
        $paginationLinks .= "<span class=active_page>[{$pageNo}]</span>&nbsp\n";
    } else {
        $paginationLinks .= "<a href='{$pageURL}?page={$pageNo}'></a>&nbsp\n";
    }
}
//Create next page link
if($page < $totalPages) {
    $nextPage = ($page+1);
    $paginationLinks .= "<a href= '{$pageURL}?page={$nextPage}' class='button'><b>Next >></b></a>&nbsp\n";
}
 
mysqli_close($link);
 
?>
<html>
<body>
 
<?php echo $output; ?>
<br><br>
<?php echo $paginationLinks; ?>
 
</body>
</html>
 
Edited by Psycho
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.