SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: smarty question

  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    smarty question

    Hi,

    Im creating some pagination with smarty...ive got the page numbers spitting out fine.....like so:


    Code PHP:
     //If there are no results then print a message saying so
          if(mysql_num_rows($query) == 0){
            $tpl->assign("noresults", "1");
          }
     
     
          //If we are not on page 1 then output a previous link
          if($page != 1) {
            $pageprev = $page - '1';
            $tpl->assign('prev','1');
            $tpl->assign('cat', $category);
            $tpl->assign("pageprev", $pageprev);
            $tpl->assign("limit", $limit);
          }
     
          //print a next button if there are enough results
          //there wouldnt be if there were ten rows, the limit was 2 and we were on page 2
          // 10 - (2 x 5) = 0
          if(($totalrows - ($limit * $page)) > 0) {
             $pagenext = $page + '1';
             $tpl->assign('next','1');
            $tpl->assign('cat', $category);
            $tpl->assign("pagenext", $pagenext);
            $tpl->assign("limit", $limit);
          }
     
     
         //Now build the page numbers
     
         $numofpages = ceil($totalrows / $limit);
     
     
         for($i = 1; $i <= $numofpages; $i++) {
            $pagingArray = array();
            $pagingArray['pageNo'] = $i;
            $pagingArray['cat'] = $category;
            $tpl->append("pagingArray", $pagingArray);
            if($i == $page) {
            $tpl->assign("current", '1');   
            }



    and the assocaited template code :


    Code PHP:
    <ul class="paging">
    {if $prev}
    <li><a href="results.php?category={$cat}&page={$pageprev}">Previous</a></li>
    {/if}
    {foreach from=$pagingArray item=i}
    {if $current}
    <li>{$i.page}</li>
    {/if}   
    <li><a href="results.php?category={$i.cat}&page={$i.pageNo}">{$i.pageNo}</a></li>
    {/foreach}
    {if $next}
    <li><a href="results.php?category={$cat}&page={$pagenext}">Next</a></li>
    {/if}
     
    </ul>


    Now, this all works dandy but the rpoblem comes when i want to diplsay just the page number and not a link for when im on the corresponsing page.....so if im on page 2 i dont want the 2 to be a link in the page number list...i just want it to be static....

    Ive had a stab at it....the php code correct ( its the if $page = $i bit above)
    but i dont know how to get it outputting in the template ( ive also tried to put it in the template but it doesnt work....how do i do this????/

  2. #2
    Team SitePoint santouras's Avatar
    Join Date
    Jul 2006
    Location
    planet earth
    Posts
    276
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    I would modify your code as such
    Code PHP:
     //If there are no results then print a message saying so
          if(mysql_num_rows($query) == 0){
            $tpl->assign("noresults", "1");
          }
     
     
          //If we are not on page 1 then output a previous link
          if($page != 1) {
            $pageprev = $page - '1';
            $tpl->assign('prev','1');
            $tpl->assign('cat', $category);
            $tpl->assign("pageprev", $pageprev);
            $tpl->assign("limit", $limit);
          }
     
          //print a next button if there are enough results
          //there wouldnt be if there were ten rows, the limit was 2 and we were on page 2
          // 10 - (2 x 5) = 0
          if(($totalrows - ($limit * $page)) > 0) {
             $pagenext = $page + '1';
             $tpl->assign('next','1');
            $tpl->assign('cat', $category);
            $tpl->assign("pagenext", $pagenext);
            $tpl->assign("limit", $limit);
          }
     
     
         //Now build the page numbers
     
         $numofpages = ceil($totalrows / $limit);
     
     
         for($i = 1; $i <= $numofpages; $i++) {
            $pagingArray = array();
            $pagingArray['pageNo'] = $i;
            $pagingArray['cat'] = $category;
            $pagingArray['current'] = $i == $page;
            $tpl->append("pagingArray", $pagingArray);
     
         }



    Code PHP:
    <ul class="paging">
    {if $prev}
    <li><a href="results.php?category={$cat}&page={$pageprev}">Previous</a></li>
    {/if}
    {foreach from=$pagingArray item=i}
    {if $i.current}
    <li>{$i.pageNo}</li>
    {else}   
    <li><a href="results.php?category={$i.cat}&page={$i.pageNo}">{$i.pageNo}</a></li>
    {/if}
    {/foreach}
    {if $next}
    <li><a href="results.php?category={$cat}&page={$pagenext}">Next</a></li>
    {/if}
     
    </ul>
    my utility belt tells me its to the bar batman

    read the manual then google it then do a search THEN post....

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah santouras thankyou! I didnt know you could if and elses in a for each loop....im new to smarty...

    Thanks again

  4. #4
    Team SitePoint santouras's Avatar
    Join Date
    Jul 2006
    Location
    planet earth
    Posts
    276
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    no problems. Have a read through of the smarty manual, you can do some really cool stuff with foreach and especially section loops. I use them for paging all the time. Something like this

    PHP Code:

    {section name=paging loop=100 step=10}
    <
    a href="/search/start/{$smarty.section.paging.index}">{$smarty.section.paging.iteration}</a>
    {/
    section
    But of course with statements to check for the current page and the like
    my utility belt tells me its to the bar batman

    read the manual then google it then do a search THEN post....


Bookmarks

Posting Permissions

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