Smarty PHP Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Smarty PHP Question

    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.

    [PHP]
    // required connect
    SmartyPaginate: :connect();
    // set items per page
    SmartyPaginate: :setLimit(20);

    $sql = mysql_query("se lect * from users");
    while($row = mysql_fetch_arr ay($sql)){
    $array[] = $row;
    }

    // now we get the total number of records from the table
    $sql = mysql_query("SE LECT FOUND_ROWS() as total");
    $row = mysql_fetch_arr ay($sql, MYSQL_ASSOC);

    SmartyPaginate: :setTotal($row['total']);

    $smarty->assign("array" , $array);

    // assign {$paginate} var
    SmartyPaginate: :assign($smarty );

    // Assign New Members
    $cols_per_row = 4;
    $smarty->assign('cols_p er_row', $cols_per_row);
    [/PHP]


    Can someone help me out?
    Cheers,
    Adam
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    This link is pretty good, and it saves me writting out code!!

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      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

      Comment

      • adamjblakey
        New Member
        • Jan 2008
        • 133

        #4
        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.

        [PHP] // required connect
        SmartyPaginate: :connect();

        if(!isset($_GET['next'])){
        $page = 1;
        } else {
        $page = $_GET['next'];
        }

        $max_results = 24;
        $from = (($page * $max_results) - $max_results);

        $sql = mysql_query("SE LECT * FROM users LIMIT $from, $max_results");

        while($row = mysql_fetch_arr ay($sql)){
        $array[] = $row;
        }

        // now we get the total number of records from the table
        $sql = mysql_query("SE LECT FOUND_ROWS() as total");
        $row = mysql_fetch_arr ay($sql, MYSQL_ASSOC);

        SmartyPaginate: :setTotal($row['total']);

        $smarty->assign("array" , $array);

        // assign {$paginate} var
        SmartyPaginate: :assign($smarty );

        // Assign New Members
        $cols_per_row = 4;
        $smarty->assign('cols_p er_row', $cols_per_row);[/PHP]

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

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          My suggestion is to go to the Smarty support people, because the problems almost certainly lies in the Smarty classes/functions.

          Ronald

          Comment

          • thefox149
            New Member
            • Mar 2008
            • 6

            #6
            I only started using paginate the other day I followed this

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

            and worked first time..

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by thefox149
              I only started using paginate the other day I followed this

              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

              Comment

              • adamjblakey
                New Member
                • Jan 2008
                • 133

                #8
                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

                Comment

                • thefox149
                  New Member
                  • Mar 2008
                  • 6

                  #9
                  [php]// required connect
                  SmartyPaginate: :connect();
                  // set items per page
                  SmartyPaginate: :setLimit(15);
                  $sql = "Your Sql goes here";
                  // assign your db results to the template
                  $smarty->assign('make ', get_db_results( $sql));
                  // assign {$paginate} var
                  SmartyPaginate: :assign($smarty );
                  // display results
                  // display results
                  $smarty->display('you r tpl');
                  function get_db_results( $sql) {
                  $_data = func_query($sql );//my own littel sql could easily use the mysql
                  SmartyPaginate: :setTotal(count ($_data));
                  return array_slice($_d ata, SmartyPaginate: :getCurrentInde x(),
                  SmartyPaginate: :getLimit());
                  }[/php]

                  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

                  Comment

                  • adamjblakey
                    New Member
                    • Jan 2008
                    • 133

                    #10
                    Hi,

                    I have tried this but does not seem to work.

                    [PHP] // required connect
                    SmartyPaginate: :connect();

                    // set items per page
                    SmartyPaginate: :setLimit(15);

                    $sql = "SELECT * FROM users";

                    // assign your db results to the template
                    $smarty->assign('array' , get_db_results( $sql));

                    // assign {$paginate} var
                    SmartyPaginate: :assign($smarty );


                    function get_db_results( $sql) {
                    $_data = mysql_query($sq l); //my own littel sql could easily use the mysql
                    SmartyPaginate: :setTotal(count ($_data));
                    return array_slice($_d ata, SmartyPaginate: :getCurrentInde x(),
                    SmartyPaginate: :getLimit());

                    }[/PHP]

                    It is not displaying any results.

                    Comment

                    Working...