Note that there are some explanatory texts on larger screens.

plurals
  1. POPaginator Class
    primarykey
    data
    text
    <p>everyone. I am working on a site with smarty templates using php and a mysql database. This is a more specific question than my first one which asked how to pass methods to a class. I thought it would be easier to repackage the question than edit the old one.</p> <p>I have written a paginator script for my image gallery which displays images on the page. If a user has selected a category then only images in a particular category are shown and the results are always paginated. The script is shown below.</p> <pre><code>$page_num = (isset($_GET['page_num']))?$_GET['page_num']:1; //if $_GET['page_num'] is set then assign to var $page_num. Otherwise set $page_num = 1 for first page $category = (isset($_GET['req1']))?$_GET['req1']:'null'; //if $_GET['req1'] is set assign to $category other set $category to null $items_pp = 5; $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++; } if(!empty($images)){ $smarty-&gt;assign('images',$images);}else{ $smarty-&gt;assign('message',"There are no images to display in the ".ucwords(str_replace('_',' ',$category))." category");} 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>The script calls on two methods from my database class: $db->num_images_gallery which looks like this:</p> <pre><code>function num_images_gallery($cat='null'){ $query = ($cat == 'null')? "SELECT COUNT(*) AS images FROM images LEFT JOIN image_categories ON (images.image_categories_id = image_categories.id) WHERE images.gallery='1' AND image_categories.gallery = '1'"//no images should be shown in a category which is not intended to be shown at all : "SELECT COUNT(*) AS images FROM images LEFT JOIN image_categories ON (images.image_categories_id = image_categories.id) WHERE category = '{$cat}' AND images.gallery='1' AND image_categories.gallery = '1'"; $result = $this-&gt;connection-&gt;query('SELECT COUNT(*) AS images FROM (?)',$x); $row = $result-&gt;fetch_assoc(); $row_count = $row['images']; echo $row_count; return $row_count; } </code></pre> <p>and the method $db::get_images_gallery() which looks like this:</p> <pre><code>function get_images_gallery($category,$limit){ $query = ($category=='null')? "SELECT `file`,title,images.description,sizes,images.gallery,category FROM images LEFT JOIN image_categories ON (images.image_categories_id = image_categories.id) WHERE images.gallery='1' AND image_categories.gallery = '1' {$limit}" : "SELECT `file`,title,images.description,sizes,images.gallery,category FROM images LEFT JOIN image_categories ON (images.image_categories_id = image_categories.id) WHERE category = '{$category}' AND images.gallery='1' AND image_categories.gallery = '1' {$limit}"; $result = $this-&gt;connection-&gt;query($query); return $result; } </code></pre> <p>I now want to create a class called paginate and put this script in it so i can display my site products paginated. The main problem is that i need to use different functions to get the num of prodducts in my product table and then return the paginated results. How do i turn the script above into a class where i can change the functions which are used. I almost got an answer on my <a href="https://stackoverflow.com/questions/1733582/pass-functions-to-a-class">previous question</a>, but the question was not specific enough. Thanks andrew</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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