View Full Version : 10 items per page (MySQL & PHP)
queerfm
03-28-2008, 03:47 PM
Hi i would like to show a max of 10 items per page and i was wondering how is this done.
If you can help that would be great,
$news = "SELECT * FROM news WHERE `group1` LIKE '%".$_GET['g']."%' ORDER BY RAND()";
$result = mysql_query($news);
while($row = mysql_fetch_array($result))
{
echo "<div class='newslist'><a href='http://www.twitoosearch.com/?p=News&g=view&q={$row['NB']}&tags={$row['group1']}&group={$row['group1']}'><img class='podimg' border='0' alt='{$row['newsname']}' src='{$row['image']}' width='100' height='100' /></a></div>";
}
Any help welcomed
JShor
03-28-2008, 06:56 PM
Hi, you would need to put a limit on your MySQL query
$rowsPerPage = 10; //number of results you want to display
$num = 1; //set the offset to start w/the num. of results (good for paging)
$offset = ($num - 1) * $rowsPerPage; // to offset the limit count
$news = "SELECT * FROM news WHERE `group1` LIKE '%".$_GET['g']."%' ORDER BY RAND() LIMIT $rowsPerPage, $offset"; // notice I added the limit
$result = mysql_query($news);
while($row = mysql_fetch_array($result))
{
echo "<div class='newslist'><a href='http://www.twitoosearch.com/?p=News&g=view&q={$row['NB']}&tags={$row['group1']}&group={$row['group1']}'><img class='podimg' border='0' alt='{$row['newsname']}' src='{$row['image']}' width='100' height='100' /></a></div>";
}
and the limit can be changed to whatever you want
HTH
-Josh
queerfm
03-28-2008, 07:27 PM
Thank for the help,
Just a few questions,
Will this show a next button if there are news items that they have not seen, and a back button.
Like i don't want to show a next button if there is nothing to next to.
queerfm
03-28-2008, 07:38 PM
Tryed the code and seem not to of worked very well, as far as it did not show the limit and when i said 12 it showed 10 and when i said 10 it show 3
jackbenimble4
03-29-2008, 02:56 PM
Tryed the code and seem not to of worked very well, as far as it did not show the limit and when i said 12 it showed 10 and when i said 10 it show 3
What your looking for is known around the developing community as pagination. There are countless articles on how to achieve pagination using PHP and MySQL. I'd recommend you check out a couple of these articles:
PHP Pagination (http://php.about.com/od/phpwithmysql/ss/php_pagination.htm)
Pagination through a database (http://www.wellho.net/resources/ex.php4?item=h113/mqchunks.php)
Pagination - what it is and how to do it (http://www.tonymarston.net/php-mysql/pagination.html)
Pagination with PHP (http://www.allsyntax.com/tutorials/PHP/23/Pagination-with-PHP/1.php)
How to add paging (Pagination) with PHP and MySQL (http://www.weberdev.com/ViewArticle/How-To-add-paging-(Pagination)-with-PHP-and-MySQL)
PHP pagination script (http://www.roscripts.com/PHP_pagination-70.html)
PHP Pagination Script (http://phpsense.com/php/php-pagination-script.html)
Perfect PHP Pagination (http://www.sitepoint.com/article/perfect-php-pagination)
Surely one of those can help you.
Note: Is your query fetching random rows? If it is, pagination is impossible. It's just common sense.
queerfm
03-31-2008, 12:02 PM
No its not well it should not be.
I would like it to be by last entry
queerfm
03-31-2008, 12:48 PM
Thanks for the help, i have it working kinda
http://www.twitoosearch.com/?p=Podcasts&g=Technology
(http://www.twitoosearch.com/?p=Podcasts&g=Technology)
however as you can see it seems to be showing the next and back button next to the logos when it should be showing it at the bottom.
here is some of the code
while($row = mysql_fetch_array($SearchResult))
{
echo "<div class='podcastlist'><a href='http://www.twitoosearch.com/?p=Podcasts&g=view&q={$row['1']}&tags={$row['2']}&group={$row['3']}'><img class='podimg' border='0' alt='{$row['4']}' src='{$row['image']}' width='100' height='100' /></a></div>";
}
$Nav="";
If($page > 1) {
$Nav .= "<br /><A HREF=\"?p=Podcasts&g=" .urlencode($g) . "&page=" . ($page-1) . "\"><< Prev</A>";
}
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= "<B>$i</B>";
}Else{
$Nav .= "<A HREF=\"?p=Podcasts&g=" .urlencode($g) . "&page=" . $i . "\">$i</A>";
}
}
If($page < $NumberOfPages) {
$Nav .= "<A HREF=\"/?p=Podcasts&g=" .urlencode($g) . "&page=" . ($page+1) . "\">Next >></A>";
}
Echo "<div class='searchr'>" . $Nav;
Echo "</div>";
?>
Part of the CSS
div.podcastlist{
background-image:URL(http://www.twitoosearch.com/images/div.png);
width:120px;
float:left;
position:inherit;
height:120px;
margin-right:0.5em;
margin-bottom:0.5em;
}
div.podcastlist:hover{
background-image:URL(http://www.twitoosearch.com/images/div_hover.png);
width:120px;
height:120px;
}
div.searchr{
width:100%;
}
Thx
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.