View Full Version : Results per page problem with smarty - urgent please help

05-25-2004, 12:55 PM
hi everyone,
i am stuck in a very difficult situation. i am making a site in php, mysql using smarty.

now i am not able to implement results per page (showing results per page say 10 on one page and then 10 on others and so on....).

i have a page show_cards.php which contains the code which retrieves books from the database.

$cards = array();
$cards = get_cards(); // thru function call----->select * from cards

Now the cards returns an associative array, which i am am able to fetch and store in another array variable to show in the template file:

foreach($cards as $card)
$card_id = $card['card_id'];
$card_title = $card['card_title'];

$cards_array[$card_id] = $card_title;

//assign card_array to a variable to use in template

Now in the template file (show_cards.tpl), i have written following code:

{foreach key=key item=item from=$cards_array}

Here $key is the card_id and item is card_title.
Now using this code, i am getting all the cards on the same page, i am not able to figure out how to get limited number of cards on one page and rest on other (results per page), so that the user can click next or back to browse other cards.

Plz help.

05-25-2004, 01:22 PM
This is know as 'recordset paging'.

Run a search here or at google or at www.php.net or at www.hotscripts.com and you'll get lotts of links to tutorials and examplecode

05-25-2004, 07:28 PM
This code is not very generic but gets the job done. It's part of an app that Ii have not cleaned up yet. It pages the results which is not the best way if you are expecting a 1000+ records returned. PHP slows considerably when scanning large arrays. The better way is to grab recordset chunks (MySQL coding) when large quantities are involved.

/*Set up paging of records and grab the start and stop positions
$sn = "pageid=2";
$commentsPerPage = 2;

if (isset($HTTP_GET_VARS['page'])){
$page = $HTTP_GET_VARS['page'];
} else {
$page = 0;
if (isset($HTTP_GET_VARS['np'])){
$np = $HTTP_GET_VARS['np'];

if (isset($HTTP_GET_VARS['category'])){
$rs1=$db->executeQuery("SELECT * FROM ".$HTTP_GET_VARS["lang"]."_butik WHERE product_category='".$HTTP_GET_VARS['category']."'; ");
} else{
$rs1=$db->executeQuery("SELECT * FROM ".$HTTP_GET_VARS["lang"]."_butik; ");

$anzahl = 0;

if ($rs1) {
$anzahl = $rs1->getRowCount();
/* Set the first record position according to the querystring
$Counter = 0;
if ($page > 0){

/** Count the number of rows
/ and break on the match
if($Counter == $commentsPerPage){
$np = $rs1->getPos();
}//End if

}//End While Loop
<br><div align="center">
$noOfPages = ceil($anzahl / $commentsPerPage);

if ($page > 0){

$st = $HTTP_GET_VARS['np'] - $commentsPerPage;
$st = ceil($st);
$st = 0;
echo "<a href=\"index.php?lang=".$HTTP_GET_VARS['lang']."&go=kwiki_shop&gopage=kwiki_shop_admin_view.php&$sn&np=$st&page=".($page-1)."\" class=\"gblink\">&lt;&lt;</a>&nbsp;";
for ($i=1; $i<=$noOfPages; $i++) {
$np = (($commentsPerPage * $i)) - $commentsPerPage;

if ($page+1 == $i){
echo "<a name=\"mark\" class=\"gbmark\">$i</a> ";

echo "<a href=\"index.php?lang=".$HTTP_GET_VARS['lang']."&go=kwiki_shop&gopage=kwiki_shop_admin_view.php&$sn&np=".$np."&page=".($i-1)."\" class=\"gblink\">$i</a> ";
print "<br>";

if ($page+1 < $noOfPages){
//$en = ceil(($np + $commentsPerPage) * ($page + 1) / $noOfPages);
$en = $HTTP_GET_VARS['np'] + $commentsPerPage;

echo "<a href=\"index.php?lang=".$HTTP_GET_VARS['lang']."&go=kwiki_shop&gopage=kwiki_shop_admin_view.php&$sn&np=$en&page=".($page+1)."\" class=\"gblink\">&gt;&gt;</a>";