0

Is possibile to create PHP pagination system?

With next - prev pagination link? Max 5 news per page.

This is my php code:

<?php
include('config.php');

$query1=mysql_query("select id, name, email , age from addd");
echo "<table><tr><td>Testo</td><td>Nome</td><td>Anni</td></tr>";

function truncate_string($str, $length) {
if (!(strlen($query2['name']) <= $length)) {
$query2['name'] = substr($query2['name'], 0, strpos($query2['name'], '        ', $length)) . '...';
}

return $query2['name'];
}

while($query2=mysql_fetch_array($query1))
{
$number= $query2['name'];
echo "<tr><td>".substr($query2['name'], 0, 500).".....</td>";
echo "<td>".$query2['email']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td>".str_word_count($number)."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Mod</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."' onclick=\"return     confirm('Sei sicuro di volerlo eliminare?');\");'>Canc</a></td><tr>";
echo "<td><a href='singletwo.php?id=".$query2['id']."');'>vedi</a></td><     tr>"; }?>

I tried it in different ways , but I failed. I read other answers , but I have been of help.

  • Please note that `mysql` is not supported anymore. Use `mysqli` or `pdo` instead. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Nytrix Jul 28 '15 at 23:59

1 Answers1

0

You can do most of it in your query, for example:

if( isset( $_GET["page"]) ) $PAGE=$_GET["page"]; else $PAGE=1;
$query1=mysqli_query($db,"select id, name, email , age from add LIMIT ". ($PAGE * 5) - 5) .",5");

this will give you pagination. You can then point next and previous links to ?page=n (where n is the next page number).

Hope this helps!

SteJ
  • 1,491
  • 11
  • 13
  • $query1=mysqli_query($db,"select id, name, email , age from add LIMIT ". ($PAGE * 5) - 5) .",5"); Make an error. –  Jul 29 '15 at 09:17
  • $query1=mysql_query("select id, name, email , age from addd LIMIT ". (($PAGE * 5) - 5) .",5"); Now work! –  Jul 29 '15 at 09:34
  • Yes, I was using the mysqli version because the old mysql library (mysql_connect, mysql_query, etc) is depreciated. Consider reading up on mysqli (it's procedure interface isn't very different) as you will need to use this in future versions of php – SteJ Jul 29 '15 at 19:14