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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    Sunshine State
    Posts
    80
    Thanks
    18
    Thanked 0 Times in 0 Posts

    How to use query from another function within same class

    this is a question that I know has been asked multiple times and either I'm just not understanding the answer or I'm not doing it right...

    I have a public function paginate($table,$display,$orderby) that performs a pagination query and returns the $page_list which is working perfectly
    PHP Code:
      public function paginate($table,$display,$orderby) {
          
    $getCountQuery $this->pdo->query("SELECT * FROM $table");
          
    $getCountResult count($getCountQuery->fetchAll());
          
    $pages = ($getCountResult $display ceil($getCountResult/$display) : 1);
          
    $page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;
          
    $s = (isset($_GET['s']) ? (int) $_GET['s'] : 0);
          
    $start = ($page 1) * $display;
          
    $getListQuery $this->pdo->query("SELECT * FROM (SELECT row_number() OVER (ORDER BY $orderby) AS rownum FROM $table) AS A WHERE A.rownum BETWEEN $start AND ($s+$display)");
          if (
    $pages 1) {
              
    $current_page = ($start/$display)+1;
              
    $previous = ($page != '<a href="?page='.($page-1).'&s='.($start-$display).'&np'.$pages.'">Previous</a>' 'Previous ');
              
    $page_list $previous.'';
              for (
    $i 1;$i<=$pages;$i++) {
                  
    $page_list .= ($i != $page '<a href="?page='.($i).'&s='.(($display * ($i-1))).'&np='.$pages.'">&nbsp;&nbsp;'.$i.'&nbsp;&nbsp;</a>' '&nbsp;&nbsp;'.$i.'&nbsp;&nbsp;');
              }
              
    $next = ($page != $pages '<a href="?page='.($page+1).'&s='.($start+$display).'&np='.$pages.'">Next</a>' ' Next');
              
    $page_list .= ''.$next;
              return 
    $page_list;
          }
      } 
    what I also need from this function is the records result of $getListQuery - do I have to create a whole new function to get those results? if so, how do I use the existing query within this function? or can someone help me to understand how to return both $page_list AND the results?

  • #2
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    Seeing as it's a class, you could define a public variable outside the function and store $getListQuery in there.

    E.g, you could do the following:

    PHP Code:
    // In function paginate:
    $getListQuery $this->pdo->query("SELECT * FROM (SELECT row_number() OVER (ORDER BY $orderby) AS rownum FROM $table) AS A WHERE A.rownum BETWEEN $start AND ($s+$display)");
    // Add this line:
    $this->listQuery $getListQuery;

    // In your script
    $var = new Class;
    $page_list $var->paginate();
    $list_query $var->listQuery
    That way you'll have both variables accessible in your script.

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Not relevant with your question, but this is not the "right" way to get the number of all records in a table.
    PHP Code:
    $getCountQuery $this->pdo->query("SELECT * FROM $table"); 
    $getCountResult count($getCountQuery->fetchAll()); 
    You should replace your query with this one.
    PHP Code:
    SELECT COUNT(*) FROM $table 

  • #4
    New Coder
    Join Date
    Jul 2011
    Location
    Sunshine State
    Posts
    80
    Thanks
    18
    Thanked 0 Times in 0 Posts
    as it turns out - I don't think the query within this function is going to provide the data I need anyway...I will ask a follow up question in a different post


  •  

    Posting Permissions

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