So right now, I've got a "gallery" system on my homepage of my site. Take a look:
<?php
$objConnect = mysql_connect("mydb.db","hello","mypass") or die(mysql_error());
$objDB = mysql_select_db("mydb");
$pic2 = "SELECT * FROM gallery";
if (!isset($_GET['Page'])) $_GET['Page']='0';
$pic1 = mysql_query($pic2);
$Num_Rows = mysql_num_rows($pic1);
$Per_Page = 16; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{$Page=1;}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{$Num_Pages =1;}
else if(($Num_Rows % $Per_Page)==0)
{$Num_Pages =($Num_Rows/$Per_Page) ;}
else
{$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;}
$pic2 .=" order by GalleryID ASC LIMIT $Page_Start , $Per_Page";
$pic1 = mysql_query($pic2);
$cell = 0;
$link2 = "SELECT * FROM gallery";
$link1 = mysql_query($link2);
$link = mysql_fetch_array($link1);
$alt2 = "SELECT * FROM gallery";
$alt1 = mysql_query($alt2);
$alt = mysql_fetch_array($alt1);
echo '<div id="tablediv"><table border="0" cellpadding="17" cellspacing="0" class="table"><tr>';
while($pic = mysql_fetch_array($pic1))
{if($cell % 4 == 0) {
echo '</tr><tr>';}
if($cell == 2) {
echo '<td>reserved cell, ignore this</td>';
} elseif ($cell == 3) {
echo '<td>reserved cell, ignore this</td>';
} else {
echo '
<td><div class="image"><img src="https://s3.amazonaws.com/images/' . $pic["pic"] . '" alt="' . $alt["alt"] . ' /></div></td>'; }
$cell++;
}
echo '</tr></table></div>';
?>
Anyhow... as you can see, with this system, whenever I insert a new record, it automatically updates my gallery. Now my question is how can I make it so when I insert a new record, it doesn't just affect my homepage gallery, it affects the galleries on the other subsections of my website as well.
Say my site is called site.com . I also have a site.com/nature . My site.com/nature is only for nature photos, but I don't want to manually update /nature by creating a whole new set of table and update that manually. Rather I'd rather take an easier route, so in my gallery table, I can specify whether or not I want it in /nature.
I presume I would need another column (obviously) for specifying what other folders do I want my record to appear in, or maybe some conditional statements to determine which subfolder should my record also appear in and not just my homepage. Unfortunately, I'm a nooblet, so I'm asking if fellow stackers can help me with this. Thanks!
I'm not sure of the exact schema you're using but if your gallery is sub divided into categories like nature, animation. its best to have one extra column ("category") probably a varchar or string (sorry not familiar with exact types in mysql) that specifies the category, so in the nature category you would go like:
select *
from gallery g
where g.category = 'nature'
So if you have another category like pokemon you would go:
select *
from gallery g
where g.category = 'pokemon'
This way you can have many different categories for you site.
but in the main page where you want all the pictures just have:
select *
from gallery g
Also, its best that instead of returning * you return only the fields you actually need. And possible use select DISTINCT filndName so you're not getting repeats the same tuple (entry) when grabbing it from sql
There are a couple of different ways you can go about this. Since you don't want to create another table, one way you can approach it is to use a column of the SET data type. This would allow you to create a set of all the subsections you have (up to 64) with one gallery item being possibly in multiple subsections.
The possible problems are the limit of 64 subsections, of course, and the fact that adding a new subsection requires an ALTER statement which, depending on how you go about adding a subsection, could cause permission issues among other things.
In my opinion, a better way is to add two new tables. One table will be a subsection table which only needs an id and a subsection path ('nature'). The other table will be a gallery-subsection table that has the gallery id and subsection id so that one gallery item can be in multiple subsections. This is not only easier to add a new subsection, but it also allows for many more than 64 subsections.
Related
I'm creating a basic main menu for a stock market simulator where the price of a company will be updated periodically. For testing purposes, I need to make a loop to display the price of a share on the website five times (with the website automatically updating without refreshing) and to update the database at the same time.
I have successfully wrote some code which will both update the database with the current share price and will also update the website as well. However, when I have tried to include a loop I have come to a problem. I have included a loop to iterate five times but the problem that I am having is that the code continues to iterate even after five tries.
PHP:
<?php
$conn = mysqli_connect("localhost", "root", "", "prices");
if ($conn->connect_error)
{
die("Connection error: ". $conn->connect_error);
}
$result = $conn->query("SELECT `price` FROM `priceTable` WHERE `company` = 'Bawden'");
$x = 0;
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
echo $row['price'];
echo '<br><br>';
echo $x;
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}
}
}
?>
The above code will be displayed in a separate document with Javascript code and I can post this if required in the original post however I originally chose not to as I believe this is a PHP only problem. I have chosen to display $x to see if the value will increment. However, when running, the value of $x will stay at 0.
My expected result is that, on the website, there will only be five different updates and in the database, the database will only be updated five times.
However, my actual result is that the website and database are both continuously being updated, not stopping after five times.
I'm trying to limit the update command to only 5 updates yes. At the
moment, for testing purposes, there is only one company in my database
with one price only. So I'm updating this one company's price five
times
If you need to do the update 5 times for each row returned from the database, change your if statement to a for loop. Change this :-
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}
to this
for ($x = 0, $x < 5, $x++)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
}
This will repeat the process exactly 5 times and not rely on a separate counter (remove the other references to $x). Not sure why you would want to update the same record 5 times with different random values though.
The else will break the first loop, the second one will stop on the first while loop.
while ($row = $result->fetch_assoc())
{
echo $row['price'];
echo '<br><br>';
echo $x;
if ($x < 5)
{
$random = (rand(3300, 3700) / 100);
$sql = $conn->query("UPDATE `priceTABLE` SET `price` = '$random' WHERE `company` = 'Bawden'");
$x++;
}else{
break;
}
break;
}
What makes you think the loop should stop after 5 iterations?
You need to add the condition $x<5 in the while ($row = $result->fetch_assoc())
Edit following your comment
What you initially wrote is something like loop hundreds of times if need be and do something in the first 5 occurrences (starting loop 6, keep looping but do nothing).
Now for the 2nd half of your comment, I'm not sure what you mean.
What I see in your code is:
Select all prices for company = 'Bawden'
Update all the prices for company = 'Bawden' 5 times (loop) with a random value, the same one, on all the records.
Not enough information to tell for sure but I don't think it makes sense: on one hand, you except to have several records under company = 'Bawden (= reason why you created a loop), on the other hand, your update feels like it is written under the assumption there would be 1 record only...
Are you missing something like a price date from your table? What is the primary key of priceTable?
Try to post more technical details about your table (definition, sample of data) or it will be complicated to help further.
Hi to all my friends out there,im facing a problem when i need to show all the purchase list from more than one table.let said 5 table named as "tablet","syrup","capsul","injection","flask",each purchase from user have to be shown in one page,lets said got 20 record from each table so total will be 100 row to be shown.the question is how to show only 10 row from each table but providing next and prev button,while each of next button wont influence the other table.here's the code.The problems is i got more than 1 mill users and there's a case when almost all of them view their purchase at the same time and my server gone.Thanks for all your help.heres the provided source.
$rs = mysql_query("SELECT * FROM tablet WHERE zomname ='".$_SESSION['username']."';",$conn) or die("Couldn't fetch records from stud");
$rs2 = mysql_query("SELECT * FROM syrup WHERE zomname ='".$_SESSION['username']."';",$conn) or die("Couldn't fetch records from stud");
$rs3 = mysql_query("SELECT * FROM capsul WHERE zomname ='".$_SESSION['username']."' LIMIT 10;",$conn) or die("Couldn't fetch records from stud");
$rs4 = mysql_query("SELECT * FROM injection WHERE zomname ='".$_SESSION['username']."';",$conn) or die("Couldn't fetch records from stud");
$rs5 = mysql_query("SELECT * FROM flask WHERE zomname ='".$_SESSION['username']."';",$conn) or die("Couldn't fetch records from stud");
$count=mysql_num_rows($rs);
$count2=mysql_num_rows($rs2);
$count3=mysql_num_rows($rs3);
$count4=mysql_num_rows($rs4);
$count5=mysql_num_rows($rs5);
if ($count>0 || $count2>0 || $count3>0 || $count4>0 || $count5>0)
{
echo "<CENTER>";
echo "<BR><B><U>Purchase</U></B><BR><br/>";
echo "<TABLE align='center' border='1' cellspacing='0' cellpadding='5'>";
echo "<TR><TH>Nama</TH><TH colspan='4'>batch</TH><TH>Hprice</TH><TH>Discount</TH><TH>Paid</TH><TH>Product</TH><TH>Branch</TH><TH>Regional</TH><TH>Time</TH></TR>";
//print Tablet data
$i=0;
while($i<$count)
{
$name=mysql_result($rs, $i, "zomname");
$product=mysql_result($rs, $i, "tproduct");
$bnum1=mysql_result($rs, $i, "batch1");
$bnum2=mysql_result($rs, $i, "batch2");
$bnum3=mysql_result($rs, $i, "batch3");
$bnum4=mysql_result($rs, $i, "batch4");
$bprice=mysql_result($rs, $i, "price");
$bprice=number_format($bprice, 0, ',', '.');
$disc=mysql_result($rs, $i, "Tdiscount");
$disc = $disc." persen";
$paid=mysql_result($rs, $i, "Tpaid");
$paid=number_format($paid, 0, ',', '.');
$country=mysql_result($rs, $i, "Tbr");
$Regin=mysql_result($rs, $i, "Treg");
$date=mysql_result($rs, $i, "zomdate");
$month=mysql_result($rs, $i, "zomonth");
$time=mysql_result($rs, $i, "zomtime");
$actualtime =$date."-".$month.",".$time;
echo "<TR><TD>$name</TD><TD>$bnum1</TD><TD>$bnum2</TD><TD>$bnum3</TD><TD>$bnum4</TD><TD>$bprice</TD><TD>$disc</TD><TD>$paid</TD><TD>$product</TD><TD>$country</TD><TD>$table</TD><TD>$actualtime</TD></TR>";
$i++;
}
echo "</TABLE></CENTER>";
echo "<br/>";
//print Syrup data and so on....all of the 5 product type have to be in one page.
//but i can limit to show each 10 item from each product type by providing next n previous button for each product
Do you think pulling your query from the database in blocks of 10 at a time will reduce your net database load? Assuming you have a large number of users executing a large number of small requests, your net load will increase. Not to mention the robustness issues of being sure you have displayed all the data while live transactions may be occuring during the paging operations.
mysql_* is depricated, look at mysqli
As Pixel Maker's comment says - use a table to track the types, hard coding the types into your php is a recipe for future pain.
My recommendations:
1) Give the user plenty of filter options - e.g. let them specify a purchase date range and purchase type, plus any other useful filters you can think of. This makes the returned data more managable for you to display and the user to find what they are looking for.
2) Grab everything the user requests in one query. Ajax the pages.
I feel this is a more logic problem than anything. A database has pictures saved via a source reference and booleans for tags e.g. isLandscape=1. I had made a system to traverse pages of results based on types asked. The following is an example of what I'm facing. I only see the same 12 pictures from page 0 -> page 22. Then I start to see new ones. I think I have just been overlooking this bug since I had not noticed it until now. One thing I noticed was page22*12pictures = 264 which is the same as the first new picture id that is seen. You can see the error here (just change the p to different pages).
<?php
$pictureid = -1;
$startpage = 0;
$viewsection = -1;
$uid = -1; //user id
$amntperrow = 4; //how many pictures per row, must correlate with doThumb()'s switch case amounts
$maxrows = 3; //how many rows of pictures to drop
if(isset($_GET['pid']) && is_int(intval($_GET['pid']))) $pictureid = clean($_GET['pid']);
if(isset($_GET['sec']) && is_int(intval($_GET['sec']))) $viewsection = clean($_GET['sec']);
if(isset($_GET['p']) && is_int(intval($_GET['p']))) $startpage = clean($_GET['p']);
$result = generateResult(array("isFlowers"), $startpage);
//**snip** -- drawing thumbnails would happen here
function generateResult($types, $page) {
global $amntperrow;
global $maxrows;
$sqlWheres = "";
$idAmnt = ($amntperrow*$maxrows)*$page;
if(isset($types) && !empty($types)) {
if(count($types) >= 1) {
for($i = 0; $i<count($types); $i++) {
$sqlWheres .= $types[$i] . "='1'";
if($i < count($types)-1) $sqlWheres .= " AND ";
}
}
}
$result = "SELECT * FROM pictures WHERE ";
if(!empty($sqlWheres)) $result .= $sqlWheres . " AND " ;
$result .= " private='0' AND id >='" . $idAmnt . "' LIMIT " . ($amntperrow*$maxrows);
return $result;
}
?>
This seems like a glaring bug that I am overlooking. Thanks for the help.
What is the difference between these two queries?
SELECT *
FROM pictures
WHERE private = '0' AND id >= '24'
LIMIT 12;
and
SELECT *
FROM pictures
WHERE private = '0' AND id >= '36'
LIMIT 12;
Answer: potentially no difference at all. The database engine can decide in either case that it wants to return pictures with ids 100 through 111 - that result set meets all of the conditions of either query.
Try a query like this instead:
"SELECT *
FROM pictures
WHERE private = '0'
ORDER BY id
LIMIT " . $idAmnt . ", " . ($amntperrow * $maxrows)
The ORDER BY id is really the key. Paging through database results is generally done with a combination of ORDER BY and LIMIT.
I have 2 tables in my database, table A and table B. I need to populate a visual grid of 20x20 squares, 400 squares in total. I just need to read the data, not insert or update.
What my script does is does FOR loop through each grid id, querys table A to see if data exists, and if it does, it does another mysql query to pulls data from table B.
As you can imagine this is using a lot of resources (400 requests). I'm not the best at PHP, but wondered if anyone knew of a better way to do this?
Note: I can't combine the 2 tables, it has to be separate.
<table width="500" height="500" background="images/map.png"><?
$plot = 1;
for ($plot_y = 1; $plot_y <= 20; $plot_y++) {
echo "<tr>";
for ($plot_x = 1; $plot_x <= 20; $plot_x++) {
$sql_result5 = mysql_query("SELECT * FROM turfs WHERE plot ='$plot'", $db);
if (mysql_num_rows($sql_result5) != 0) {
$rs5 = mysql_fetch_array($sql_result5);
$nogo = 0; // SET COLOR TO rs5[color]
} else {
$nogo = 1;
}
?><td width="23" height="23"><?php
if ($nogo == 0) {
$sql_result = mysql_query("SELECT * FROM properties WHERE plot = '$plot'", $db);
if (mysql_num_rows($sql_result) > 0) {
$rs = mysql_fetch_array($sql_result);
echo "<img src=$rs[image]>";
}
}
echo "</td>";
$plot = $plot + 1;
}
echo "</tr>";
}
?></table>
Obviously that much queries will take a lot of memory. There is a "trick" to optimize multiple queries that are alike - prepared statements.
You have 2 tables thah sounds like you need a join (if you are not using such).
Then you can use some caching if the data is not updating very often.
There are a lot of thing that could be optimized ...
Without knowing your table structure the best we can do is guess at it but following might give you some fresh ideas to work with.
The gist of it would be to
select the GridID's and the related data with one select statement
loop over the results of this statement to fill your grid
SQL Statement (edited)
SELECT t.plot AS plot
, t.plot MOD 20 AS plot_x
, FLOOR(t.plot / 20) AS plot_y
, CASE WHEN p.plot IS NULL THEN 'free' ELSE 'used' END
FROM turfs AS t
LEFT OUTER JOIN properties AS p ON p.plot = t.plot
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.
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.
$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->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 > $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 < 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->get_images_gallery($category,$limit);
$i = 0;
while($row = $result->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->assign('images',$images);}else{
$smarty->assign('message',"There are no images to display in the ".ucwords(str_replace('_',' ',$category))." category");}
if($total > 0 && $pages_required >= 1){//only display this navigation if there are images to display and more than one page
$smarty->assign('page_scroll',$page_num . ' of ' . $pages_required);
$page_scroll_first = "<a href='".$_SERVER['REDIRECT_URL'] . "?page_num=1"."' >FIRST</a> <a href='".$_SERVER['REDIRECT_URL'] . "?page_num=" . ($page_num-1)."' ><<PREVIOUS</a>";
$page_scroll_last = " <a href='".$_SERVER['REDIRECT_URL'] . "?page_num=". ($page_num+1) . "'>NEXT>></a> <a href='" . $_SERVER['REDIRECT_URL'] . "?page_num=".$pages_required."'>LAST</a>";
if($page_num == 1){$page_scroll_first = "FIRST <<PREVIOUS";}
if($page_num == $pages_required){$page_scroll_last = "NEXT>> LAST";}
$smarty->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->assign('page_scroll_last',$page_scroll_last);
$smarty->assign('page_num',$page_num);
}
The script calls on two methods from my database class:
$db->num_images_gallery which looks like this:
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->connection->query('SELECT COUNT(*) AS images FROM (?)',$x);
$row = $result->fetch_assoc();
$row_count = $row['images'];
echo $row_count;
return $row_count;
}
and the method $db::get_images_gallery() which looks like this:
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->connection->query($query);
return $result;
}
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 previous question, but the question was not specific enough.
Thanks
andrew
There's a Smarty Add-On for pagination.
You can find it here: http://www.phpinsider.com/php/code/SmartyPaginate/
For a quick example, extracted from the linked page:
index.php
session_start();
require('Smarty.class.php');
require('SmartyPaginate.class.php');
$smarty =& new Smarty;
// required connect
SmartyPaginate::connect();
// set items per page
SmartyPaginate::setLimit(25);
// assign your db results to the template
$smarty->assign('results', get_db_results());
// assign {$paginate} var
SmartyPaginate::assign($smarty);
// display results
$smarty->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());
}
index.tpl
{* 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}
Regarding your question about mixing the DB class and the Paginator class, it's all ok:
Your DB class will handle fetching data from DB
The SmartyPaginate class will handle the pagination
And your index.php just make the calls to each one where appropriate to set things out.
The idea is to keep responsibilities isolated.
Your DB class won't handle pagination, nor will your pagination class contain DB code.
From your other question, I think you were trying to do something too much convoluted for the problem at hand.
I'd suggest you to move all code that is DB-related inside your DB handling class and outside your index.php
This, for example:
$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
This is DB logic, it generates (part of) an SQL string, so move it around.
It depends on 2 parameters, so find a way to get them available.
In this case, I'd suggest just passing both as parameters.
Instead of:
$result = $db->get_images_gallery($category,$limit);
Use:
$result = $db->get_images_gallery($category,$no_items, $page);
Also, your pager navigation rule should be inside your paginator class..
if($total > 0 && $pages_required >= 1){//only display this navigation if there are images to display and more than one page
$smarty->assign('page_scroll',$page_num . ' of ' . $pages_required);
$page_scroll_first = "<a href='".$_SERVER['REDIRECT_URL'] . "?page_num=1"."' >FIRST</a> <a href='".$_SERVER['REDIRECT_URL'] . "?page_num=" . ($page_num-1)."' ><<PREVIOUS</a>";
$page_scroll_last = " <a href='".$_SERVER['REDIRECT_URL'] . "?page_num=". ($page_num+1) . "'>NEXT>></a> <a href='" . $_SERVER['REDIRECT_URL'] . "?page_num=".$pages_required."'>LAST</a>";
if($page_num == 1){$page_scroll_first = "FIRST <<PREVIOUS";}
if($page_num == $pages_required){$page_scroll_last = "NEXT>> LAST";}
$smarty->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->assign('page_scroll_last',$page_scroll_last);
$smarty->assign('page_num',$page_num);
}
In this case, I hope the Add-On will handle it automatically for you.
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)
$total = $db->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 > $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 < 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->get_images_gallery($category,$limit);
$i = 0;
while($row = $result->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++;
}
Finally, on your index.php, all you have to do is:
Validate the parameters you received
Call your ImageGalery class to fetch the galery data (pass the parameters it needs)
Call your Pagination class to do the pagination (setting up navigation links, etc)
Set the Smarty template variables you need
And display it.
There is still lots of room for improvement, but I hope those few steps will help get your Image Galery code more clear.