Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Smarty PHP Question

Question posted by: adamjblakey (Member) on March 26th, 2008 01:57 PM
Hi,

I am trying to implement pagination into my site but am having a few problems.

This is my code, but when i load the page all the results show and when i click to the next page 2 it shows the same results. It does not seem to be splitting the results between 2 pages.

Code: ( text )
  1. // required connect
  2.     SmartyPaginate::connect();
  3.     // set items per page
  4.     SmartyPaginate::setLimit(20);
  5.  
  6.    $sql = mysql_query("select * from users");   
  7.    while($row = mysql_fetch_array($sql)){
  8.    $array[] = $row;
  9.    }
  10.    
  11.     // now we get the total number of records from the table
  12.         $sql = mysql_query("SELECT FOUND_ROWS() as total");
  13.         $row = mysql_fetch_array($sql, MYSQL_ASSOC);
  14.        
  15.         SmartyPaginate::setTotal($row['total']);
  16.    
  17.    $smarty->assign("array", $array);
  18.    
  19.     // assign {$paginate} var
  20.     SmartyPaginate::assign($smarty);
  21.    
  22.    // Assign New Members
  23.    $cols_per_row = 4;
  24.    $smarty->assign('cols_per_row', $cols_per_row);



Can someone help me out?
Cheers,
Adam
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
harshmaul's Avatar
harshmaul
Expert
481 Posts
March 26th, 2008
04:47 PM
#2

Re: Smarty PHP Question
This link is pretty good, and it saves me writting out code!!

http://www.tonymarston.net/php-mysql/pagination.html

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
March 27th, 2008
12:48 AM
#3

Re: Smarty PHP Question
Quote:
Originally Posted by harshmaul
This link is pretty good, and it saves me writting out code!!

http://www.tonymarston.net/php-mysql/pagination.html

I am afraid that Tony Marston article is about writing the pagination code yourself.

It does not address using the Smarty pagination plug-in.

Ronald
__________________
RTFM is an almost extinct art form.

Reply
adamjblakey's Avatar
adamjblakey
Member
110 Posts
March 27th, 2008
08:56 AM
#4

Re: Smarty PHP Question
Quote:
Originally Posted by ronverdonk
I am afraid that Tony Marston article is about writing the pagination code yourself.

It does not address using the Smarty pagination plug-in.

Ronald


Yes this is what i was going to say,

I have changed the code a bit to try and get it to work but does not seem to function correctly.

Code: ( text )
  1. // required connect
  2.     SmartyPaginate::connect();
  3.    
  4.     if(!isset($_GET['next'])){
  5.     $page = 1;
  6.     } else {
  7.         $page = $_GET['next'];
  8.     }
  9.    
  10.     $max_results = 24;
  11.     $from = (($page * $max_results) - $max_results);
  12.  
  13.     $sql = mysql_query("SELECT * FROM users LIMIT $from, $max_results");   
  14.  
  15.     while($row = mysql_fetch_array($sql)){
  16.     $array[] = $row;
  17.     }
  18.    
  19.      // now we get the total number of records from the table
  20.         $sql = mysql_query("SELECT FOUND_ROWS() as total");
  21.         $row = mysql_fetch_array($sql, MYSQL_ASSOC);
  22.        
  23.         SmartyPaginate::setTotal($row['total']);
  24.    
  25.     $smarty->assign("array", $array);
  26.    
  27.     // assign {$paginate} var
  28.     SmartyPaginate::assign($smarty);
  29.    
  30.     // Assign New Members
  31.     $cols_per_row = 4;
  32.     $smarty->assign('cols_per_row', $cols_per_row);


It just shows Items 1-24 out of 24 displayed and no other next or pages links but there is 60+ in the table.

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
March 27th, 2008
04:36 PM
#5

Re: Smarty PHP Question
My suggestion is to go to the Smarty support people, because the problems almost certainly lies in the Smarty classes/functions.

Ronald

Reply
thefox149's Avatar
thefox149
Newbie
6 Posts
March 27th, 2008
11:40 PM
#6

Re: Smarty PHP Question
I only started using paginate the other day I followed this

http://www.phpinsider.com/php/code/SmartyPaginate/

and worked first time..

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
March 27th, 2008
11:45 PM
#7

Re: Smarty PHP Question
Quote:
Originally Posted by thefox149
I only started using paginate the other day I followed this
http://www.phpinsider.com/php/code/SmartyPaginate/
and worked first time..

thefox149: welcome to The Scripts. It is good to hear that you have found some way of pointer OP in the right direction of this Smarty problem. I was losing hope that we could solve this in this forum.

Thanks.

Ronald
__________________
RTFM is an almost extinct art form.

Reply
adamjblakey's Avatar
adamjblakey
Member
110 Posts
March 28th, 2008
08:42 AM
#8

Re: Smarty PHP Question
Hi,

Thank you for your replies, i have tried directing this to specific smarty programmers but they don't seem to want to help.

As for the link this is what i followed and code is based on but i cannot get it to work correctly. The problem with that script is it uses an array and only briefly focuses on the MySQL solution.

Cheers,
Adam

Reply
thefox149's Avatar
thefox149
Newbie
6 Posts
March 28th, 2008
06:45 PM
#9

Re: Smarty PHP Question
Code: ( text )
  1. // required connect
  2.     SmartyPaginate::connect();
  3.     // set items per page
  4.     SmartyPaginate::setLimit(15);
  5.     $sql = "Your Sql goes here";
  6.     // assign your db results to the template
  7.     $smarty->assign('make', get_db_results($sql));
  8.     // assign {$paginate} var
  9.     SmartyPaginate::assign($smarty);
  10.     // display results
  11.     // display results
  12.     $smarty->display('your tpl');
  13. function get_db_results($sql) {
  14.         $_data = func_query($sql);//my own littel sql could easily use the mysql
  15.         SmartyPaginate::setTotal(count($_data));
  16.         return array_slice($_data, SmartyPaginate::getCurrentIndex(),
  17.             SmartyPaginate::getLimit());
  18.     }


Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR

Reply
adamjblakey's Avatar
adamjblakey
Member
110 Posts
March 31st, 2008
08:38 AM
#10

Re: Smarty PHP Question
Hi,

I have tried this but does not seem to work.

Code: ( text )
  1. // required connect
  2.       SmartyPaginate::connect();
  3.  
  4.       // set items per page
  5.       SmartyPaginate::setLimit(15);
  6.       
  7.       $sql = "SELECT * FROM users"; 
  8.  
  9.       // assign your db results to the template
  10.       $smarty->assign('array', get_db_results($sql));
  11.     
  12.      // assign {$paginate} var
  13.      SmartyPaginate::assign($smarty);
  14.    
  15.  
  16.     function get_db_results($sql) {
  17.         $_data = mysql_query($sql); //my own littel sql could easily use the mysql
  18.         SmartyPaginate::setTotal(count($_data));
  19.         return array_slice($_data, SmartyPaginate::getCurrentIndex(),
  20.                   SmartyPaginate::getLimit());
  21.  
  22.           }


It is not displaying any results.

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,939 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors