Search code examples
phphtmlmysqlpagination

PHP MySQL Paging


I tried so many different solutions. Im new to php since 1 week, used ASP 12 years ago so I hope I can get some help.

Everything down here works fine. But there are around 1000 rows in the db and I need to split them up in pages.

<?php
$con = mysqli_connect("localhost","test","test","test")or die('could not connect to database');

// Check connection
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo "<table border='0'>
 <tr>
  <th>Img:</th>
  <th>Text:</th>
 </tr>";

$result = mysqli_query($con,"SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText FROM Jokes LEFT JOIN Categories ON Jokes.CategoryID = Categories.ID ORDER BY  Jokes.JokeText");

while($row = mysqli_fetch_array($result))
  {

   echo "<tr>";
   echo "<td align='center'><img src='webimg/" . $row['CategoryName'] . ".png' height='35' width='35'></td>";
   echo "<td align='left'  width='80%'>" . $row['JokeText'] . "</td>";
   echo "</tr>";

  }
echo "</table>";

mysql_close($con);
?>

Kind Regards.


Solution

  • Thx for all the answers. I came over this tutorial and it worked: http://www.developphp.com/view.php?tid=1349

    <?php
    include_once("mysqli_connection.php");
    
    $sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    
    $rows = $row[0];
    
    $page_rows = 10;
    
    $last = ceil($rows/$page_rows);
    
    if($last < 1){
        $last = 1;
    }
    
    $pagenum = 1;
    
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
    
    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    }
    
    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    
    $sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
    $query = mysqli_query($db_conx, $sql);
    
    $textline1 = "Testimonials (<b>$rows</b>)";
    $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
    
    $paginationCtrls = '';
    
    if($last != 1){
    
        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
    
            for($i = $pagenum-4; $i < $pagenum; $i++){
                if($i > 0){
                    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
                }
            }
        }
    
        $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    
        for($i = $pagenum+1; $i <= $last; $i++){
            $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            if($i >= $pagenum+4){
                break;
            }
        }
    
        if ($pagenum != $last) {
            $next = $pagenum + 1;
            $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
        }
    }
    $list = '';
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        $id = $row["id"];
        $firstname = $row["firstname"];
        $lastname = $row["lastname"];
        $datemade = $row["datemade"];
        $datemade = strftime("%b %d, %Y", strtotime($datemade));
        $list .= '<p><a href="testimonial.php?id='.$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>';
    }
    
    mysqli_close($db_conx);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
    div#pagination_controls{font-size:21px;}
    div#pagination_controls > a{ color:#06F; }
    div#pagination_controls > a:visited{ color:#06F; }
    </style>
    </head>
    <body>
    <div>
      <h2><?php echo $textline1; ?> Paged</h2>
      <p><?php echo $textline2; ?></p>
      <p><?php echo $list; ?></p>
      <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
    </div>
    </body>
    </html>