Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a Smarty Add-On for pagination.</p> <p>You can find it here: <a href="http://www.phpinsider.com/php/code/SmartyPaginate/" rel="nofollow noreferrer">http://www.phpinsider.com/php/code/SmartyPaginate/</a></p> <p>For a quick example, extracted from the linked page:</p> <h3>index.php</h3> <pre><code>session_start(); require('Smarty.class.php'); require('SmartyPaginate.class.php'); $smarty =&amp; new Smarty; // required connect SmartyPaginate::connect(); // set items per page SmartyPaginate::setLimit(25); // assign your db results to the template $smarty-&gt;assign('results', get_db_results()); // assign {$paginate} var SmartyPaginate::assign($smarty); // display results $smarty-&gt;display('index.tpl'); function get_db_results() { // normally you would have an SQL query here, // for this example we fabricate a 100 item array // (emulating a table with 100 records) // and slice out our pagination range // (emulating a LIMIT X,Y MySQL clause) $_data = range(1,100); SmartyPaginate::setTotal(count($_data)); return array_slice($_data, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()); } </code></pre> <h3>index.tpl</h3> <pre><code>{* display pagination header *} Items {$paginate.first}-{$paginate.last} out of {$paginate.total} displayed. {* display results *} {section name=res loop=$results} {$results[res]} {/section} {* display pagination info *} {paginate_prev} {paginate_middle} {paginate_next} </code></pre> <p>Regarding your question about mixing the DB class and the Paginator class, it's all ok:<br> Your DB class will handle fetching data from DB<br> The SmartyPaginate class will handle the pagination<br> And your index.php just make the calls to each one where appropriate to set things out.</p> <p>The idea is to keep responsibilities isolated.<br> Your DB class won't handle pagination, nor will your pagination class contain DB code.</p> <p>From your other question, I think you were trying to do something too much convoluted for the problem at hand.</p> <p>I'd suggest you to move all code that is DB-related inside your DB handling class and outside your <code>index.php</code></p> <p>This, for example:</p> <pre><code>$limit = "LIMIT " .($page_num - 1)*$items_pp . "," . $items_pp . ""; //if 5 results pre page and we on page 3 then LIMIT 10,5 that is record 10,11,12,13 and 14 </code></pre> <p>This is DB logic, it generates (part of) an SQL string, so move it around.<br> It depends on 2 parameters, so find a way to get them available.<br> In this case, I'd suggest just passing both as parameters.</p> <p>Instead of:</p> <pre><code>$result = $db-&gt;get_images_gallery($category,$limit); </code></pre> <p>Use:</p> <pre><code>$result = $db-&gt;get_images_gallery($category,$no_items, $page); </code></pre> <p>Also, your pager navigation rule should be inside your paginator class..</p> <pre><code>if($total &gt; 0 &amp;&amp; $pages_required &gt;= 1){//only display this navigation if there are images to display and more than one page $smarty-&gt;assign('page_scroll',$page_num . ' of ' . $pages_required); $page_scroll_first = "&lt;a href='".$_SERVER['REDIRECT_URL'] . "?page_num=1"."' &gt;FIRST&lt;/a&gt; &lt;a href='".$_SERVER['REDIRECT_URL'] . "?page_num=" . ($page_num-1)."' &gt;&amp;lt;&amp;lt;PREVIOUS&lt;/a&gt;"; $page_scroll_last = " &lt;a href='".$_SERVER['REDIRECT_URL'] . "?page_num=". ($page_num+1) . "'&gt;NEXT&amp;gt;&amp;gt;&lt;/a&gt; &lt;a href='" . $_SERVER['REDIRECT_URL'] . "?page_num=".$pages_required."'&gt;LAST&lt;/a&gt;"; if($page_num == 1){$page_scroll_first = "FIRST &amp;lt;&amp;lt;PREVIOUS";} if($page_num == $pages_required){$page_scroll_last = "NEXT&amp;gt;&amp;gt; LAST";} $smarty-&gt;assign('page_scroll_first',$page_scroll_first);//just use if statements to set the values for page scroll first and page scroll last and then assign them here $smarty-&gt;assign('page_scroll_last',$page_scroll_last); $smarty-&gt;assign('page_num',$page_num); } </code></pre> <p>In this case, I hope the Add-On will handle it automatically for you.</p> <p>You could then move this whole block, which does all your logic for fetching and preparing images data to a function (inside your ImageGalery class if you have one)</p> <pre><code>$total = $db-&gt;num_images_gallery($category); //returns the number of records in total('null') or in a particular category('category_name') $pages_required = ceil($total/$items_pp); //total records / records to display per page rounded up if($page_num &gt; $pages_required){//in case the current page number is greater that the pages required then set it to the amount of pages required $page_num = $pages_required; } if($page_num &lt; 1){//in case the current page num is set to less that one set it back to 1 $page_num = 1; } $limit = "LIMIT " .($page_num - 1)*$items_pp . "," . $items_pp . ""; //if 5 results pre page and we on page 3 then LIMIT 10,5 that is record 10,11,12,13 and 14 $result = $db-&gt;get_images_gallery($category,$limit); $i = 0; while($row = $result-&gt;fetch_assoc()){ $images[$i]['file'] =$row['file']; $images[$i]['file_thumb'] = str_replace('.','_thumbnail.',$row['file']);//show the thumbnail version of the image on the page $images[$i]['title'] = $row['title']; $images[$i]['description'] = $row['description']; $i++; } </code></pre> <p>Finally, on your index.php, all you have to do is:<br> Validate the parameters you received<br> Call your ImageGalery class to fetch the galery data (pass the parameters it needs)<br> Call your Pagination class to do the pagination (setting up navigation links, etc)<br> Set the Smarty template variables you need<br> And display it. </p> <p>There is still lots of room for improvement, but I hope those few steps will help get your Image Galery code more clear.</p>
 

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