Note that there are some explanatory texts on larger screens.

plurals
  1. POpagination - 10 pages per page
    primarykey
    data
    text
    <p>I have a pagination script that displays a list of all pages like so:<br> <code>prev [1][2][3][4][5][6][7][8][9][10][11][12][13][14] next</code><br> But I would like to only show ten of the numbers at a time:<br> <code>prev [3][4][5][6][7][8][9][10][11][12] next</code></p> <p>How can I accomplish this? Here is my code so far:</p> <pre><code>&lt;?php /* Set current, prev and next page */ $page = (!isset($_GET['page']))? 1 : $_GET['page']; $prev = ($page - 1); $next = ($page + 1); /* Max results per page */ $max_results = 2; /* Calculate the offset */ $from = (($page * $max_results) - $max_results); /* Query the db for total results. You need to edit the sql to fit your needs */ $result = mysql_query("select title from topics"); $total_results = mysql_num_rows($result); $total_pages = ceil($total_results / $max_results); $pagination = ''; /* Create a PREV link if there is one */ if($page &gt; 1) { $pagination .= '&lt; a href="?page='.$prev.'"&gt;Previous&lt;/a&gt; '; } /* Loop through the total pages */ for($i = 1; $i &lt;= $total_pages; $i++) { if(($page) == $i) { $pagination .= $i; } else { $pagination .= '&lt; a href="index.php?page='.$i.'"&gt;'.$i.'&lt;/a&gt;'; } } /* Print NEXT link if there is one */ if($page &lt; $total_pages) { $pagination .= '&lt; a hr_ef="?page='.$next.'"&gt; Next&lt;/a&gt;'; } /* Now we have our pagination links in a variable($pagination) ready to print to the page. I pu it in a variable because you may want to show them at the top and bottom of the page */ /* Below is how you query the db for ONLY the results for the current page */ $result=mysql_query("select * from topics LIMIT $from, $max_results "); while ($i = mysql_fetch_array($result)) { echo $i['title'].'&lt;br /&gt;'; } echo $pagination; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload