Jump to content

Global in a Function Inside a Function Does Not Work?


chaseman

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.