Jump to content

PHP data pagination issue


stakinloot

Recommended Posts

<?php
error_reporting(0);
$servername = "localhost";
$username = "user";
$password = "pw";
$dbname = "news";
$datatable = "ttrss_user_labels2"; // MySQL table name
$datatable2 = "ttrss_entries";
$results_per_page = 10; // number of results per page
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
?>
<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $results_per_page;
$sql = "SELECT * FROM ".$datatable2.", ".$datatable." WHERE article_id = id ORDER BY date_entered DESC LIMIT $start_from, ".$results_per_page;
$rs_result = $conn->query($sql); 

?> 
<?php 
 while($row = $rs_result->fetch_assoc()) {
?>
<?php
$title =  preg_replace('/[^(\x20-\x7F)]*/','', $row['title']);
$content = preg_replace('/[^(\x20-\x7F)]*/','', $row['content']);
?>
     <table id="news" border="1" cellpadding="1" class="mx-auto col-xs-12 col-sm-12 col-md-12 col-lg-12">
           <tr class="wrap"><td class="rounded-lg shadow curved">
	   <?php echo '<div charset="UTF-16" class ="text-center" style ="font:14px/18px Arial,tahoma,sans-serif top:5px">', $title, '<hr class=mx-auto></div>'; ?>
           <?php echo '<div charset="UTF-16" class ="text-center" style ="font:11px/21px Arial,tahoma,sans-serif">', $content, '&nbsp;', '<a href= "' . $row['link'] . '" target=_blank>Read More</a>', '</div>'; ?>
	   <div style ="font:9px/15px Arial,tahoma,sans-serif" class="text-right"><? echo date('F j, Y g:i a', strtotime($row["date_entered"])) ?><br></div>
	  
           
<? echo '</td></tr></table><br><br>'; ?>
<?php 
}; 
?>

<!--Grid row-->
<div class="row d-flex justify-content-center">

  <!--Grid column-->
  <div class="top_menu col-xs-12 col-sm-12 col-md-12 col-lg-12">
	<div class="container">
					<div class="row"><div class="col-md-9">
<ul class="pagination pagination-sm pagination-centered">
<?php 
$sql = "SELECT COUNT(ID) AS total FROM ".$datatable2;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
$firstPage = 0; // or 1, depending on your implementation
$currentPage = (int)$_GET['page'];
if ($currentpage != $total_pages) {
$nextpage = $currentPage + 1;
$prevpage = $currentPage - 1;
   echo "<li><a href='index.php?page=$prevpage'><<</a></li>&nbsp;";


// for ($i=1; $i<=$total_pages; $i++) {  // print links for all pages
for($i = $page + 1; $i <= min($page + 4, $total_pages); $i++) {
            echo "<li><a href='index.php?page=".$i."'";
            if ($i==$page)  echo " class='curPage'";
            echo ">".$i."</a></li> "; 



}; 

   echo "<li><a href='index.php?page=$nextpage'>>></a></li>&nbsp;";
  // echo "<li><a href='index.php?page=$total_pages'>last</a></li></u><br><br>";
}
?>
</div></div></div><div class="clearfix">

 

Hi fellow PHP coders, I have gotten stuck with this code a bit as I was trying to build my first pagination script.

I am getting only the 10 latest results from the database on all result pages.  I am not sure what exactly I did wrong in this code to make it show all the results database has to offer spanning across pages at a rate of 10 per page from most current first to the oldest.  Anyone have any ideas what I am doing wrong here?

Any suggestions are much appreciated. 

 

 

Link to comment
Share on other sites

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.