Forum Moderators: open
PS - there wont ever be more than 100 or so rows returned, so if it helps, it doesnt need to be the fastest solution.
$sql = "select news_placements.id, news_placements.title, news_placements.short_text, news_placements.artist_id_fk, news_placements.placement, news_placements.date, images.path_small, images.news_id_fk, artists.name, artists.id as aId
from news_placements
left join images on images.news_id_fk = news_placements.id
inner join artists on news_placements.artist_id_fk = artists.id
where news_placements.placement = 1 and
images.path_small is not null
order by news_placements.date desc
";
this give me an error: Query failed: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
so your select should be: select count(*) as nbrRows from news_placements ... etc
you will get one row/column returned for that query that contains the count.
then you do another select (your original query) to get all (nbrRows) of the data.
In reference to your original question about pagination, you want to do something like this (simplified:)
$sql = "select count(*) from news_placements [joins] [where];
Store this in something like $tot_records. Then using $tot_records, you decide where to place your limit. For the first page, it's always going to be zero:
$per_page = 25; ## or whatever
$start = 0;
$sql = "select [fields] [joins] [where] limit $start, $per_page order by news_placements.date desc;
You then store "first in page range" and "last in page range" to $tot_records based on the $per_page value in your programming. Create a little routine something like this:
sub buildPaginationLinks {
$per = ($per_page)?$per_page:25; ## Sets a default, in case $per_page is not defined
if ($per >= $tot_records) { return; } ## no need for links
for ($i=0; $i<=$tot_records; $i++){
$start_link = ($i * $per) - $per;
$start_display = $start_link + 1;
$nextLink = (($start_link + $per) < $tot_records)?($start_link + $per):$tot_records;
$linkText .= "<a href=\"yourscript&lim_start=$start_link_
&lim_ceil=$per&\">$start_display</a>";
# Previous ALL ON ONE LINE at the underscore
if ($i < $tot_records) { $linkText .= ' ¦ ' ; }
}
return $linkText;
}
$linkText is the "chunk" representing your pagination links.
This code is extremely oversimplified and won't run on it's own, but this should show you the concept and is one way to do it. Also if you have more than a thousand records, you have to do something else or it will bog the page load with links.
Note the generation of lim_start and lim_ceil in these links. When sent to your script, you use them to set limit and ceil. A modification of the original select:
## If lim_start and lim_ceil are present, set the variables
## to those values; if not, use 0 and $per_page:
$lim_start = ($qs{'lim_start'})?$qs{'lim_start'}:0;
$lim_ceil = ($qs{'lim_ceil'})?$qs{'lim_ceil'}:$per_page;
$sql = "select [fields] [joins] [where] limit $lim_start, $lim_ceil order by news_placements.date desc;