Results per page problem with smarty - urgent please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaurav_sting
Forum Newbie
Posts: 19
Joined: Sat Mar 27, 2004 3:45 am
Location: Delhi

Results per page problem with smarty - urgent please help

Post by gaurav_sting »

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
$smarty->assign("cards_array",$cards_array);
$smarty->display("show_cards.tpl");



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

{foreach key=key item=item from=$cards_array}
<tr>
<td>{$key}</td><td>{$item}</td>
</tr>
{/foreach}

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.
Thanx.
Gaurav
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

you need to change the sql statement to use the LIMIT and offset attributes to pull out the correct items

$cards = get_cards(); // thru function call----->select * from cards LIMIT $offset, 10

Then write a quick function to pull out the check the current offset and the current page to write out the appropriate links to navigate the recordset
Post Reply