Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Results per page problem with smarty - urgent please help

    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

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    /*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){
    		$rs1->setPos($np-1); 
    	}
    ?>
    Code:
    <?php 
    /** Count the number of rows
    / and break on the match
    */ 
    	$Counter++;
    	if($Counter == $commentsPerPage){
    		$np = $rs1->getPos();
    		break;
    	}//End if
    			
     }//End While Loop
    ?>
     <br><div align="center">	
    <?php
    $noOfPages = ceil($anzahl / $commentsPerPage);
    
    if ($page > 0){
       
    	$st = $HTTP_GET_VARS['np'] - $commentsPerPage;
    	$st = ceil($st);
    	if($st<0){
    	  $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> ";
    		 
    	   }else{   
    			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> ";
    	   }
    	   if($i%10==0){ 
    			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>"; 
    }
    ?>
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •