chaseman Posted February 28, 2011 Share Posted February 28, 2011 When I put this chunk of code into it's own function: function fetch_all ($dbc, $query) { include ('knuffix_list_func.php'); pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; echo "test query: " . $query; knuffix_list ($query, $dbc); pagination_end ($pag_array); } And when I echo out the query as you can see in the example, then I notice that the variables $offset and $rows_per_page never get appended. I set the variable $pag_array to a global inside the function pagination_start(). It usually works when I DON'T wrap a function around this chunk of code, but if I do wrap a function around everything then the global suddenly won't work anymore. Btw, this also won't work if I wrap a function around the function DECLARATIONS. Any ideas, how I could make it work? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 28, 2011 Share Posted February 28, 2011 Don't use globals, pass the needed variables to the function. I don't see any globals being declared in the function you posted, unless they are in the included file, which you didn't post. Ken Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 28, 2011 Author Share Posted February 28, 2011 as I wrote in my post the global is inside the pagination_start() function CALL. How would I pass it through? I'm able to in-ject variables into a function, but I'm never able to out-ject variables outside of the function if I don't use globals. How would it work in this example? This is the DECLARATION of pagination_start(): function pagination_start ($dbc, $query) { // find out how many rows are in the table: $query_run = mysqli_query ($dbc, $query); $num_rows = mysqli_num_rows($query_run); echo "Number: " . $num_rows; // number of rows to show per page $rows_per_page = 2; // find total pages -> ceil for rounding up $total_pages = ceil($num_rows / $rows_per_page); // get the current page or set a default if (isset($_GET['current_page']) && is_numeric($_GET['current_page'])) { // make it an INT if it isn't $current_page = (int) $_GET['current_page']; } else { // default page number $current_page = 1; } // if current page is greater than total pages then set current page to last page if ($current_page > $total_pages) { $current_page = $total_pages; } // if current page is less than first page then set current page to first page if ($current_page < 1) { $current_page = 1; } // the offset of the list, based on current page $offset = (($current_page - 1) * $rows_per_page); global $pag_array; $pag_array = array ($offset, $rows_per_page, $current_page, $total_pages); } // END FUNCTION Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 28, 2011 Share Posted February 28, 2011 In this case you want to return the array back to the calling function. In the fetch_all function, do <?php $page_ary = pagination_start ($dbc, $query); ?> In the pagination_start function, do <?php return (array ($offset, $rows_per_page, $current_page, $total_pages)); } ?> and delete the line <?php global $pag_array; ?> Ken Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 28, 2011 Author Share Posted February 28, 2011 Wow that worked, thank you a lot, I looked in books and couldn't find these type of informations. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.