I get this strange problem....
All the page has this code only. global $currentPage; is null and i dont know why...
<?php
$pager = $_PARAMS["this"];
$pages = 5;
$currentPage = 1;
$tst="ap";
$nearPages = 5;
//Prologic
?>
<div class="pager">
<?php
$nearPagesHalf = ($nearPages - 1) / 2;
drawNumbers(1, 1);
if ($currentPage - $nearPagesHalf <= 0) {
}
drawNumbers($pages, $pages);
?>
<?php
function drawNumbers($from, $to) {
global $currentPage;
for ($i = $from; $i <= $to; $i++) {
echo $currentPage;
if ($i == $currentPage) {
?> <span class="pageNumbers current"><?= $i ?></span>
<?php
} else {
?>
<a href="#">
<span class="pageNumbers"><?= $i ?></span>
</a>
<?php
}
}
?>
<?php
}
function drawDots($from, $to) {
}
?>
</div>
THE PROBLEM
echo $currentPage; prints 1
function drawNumbers($from, $to) {
global $currentPage;
echo $currentPage; prints nothing
I bet you're executing this code by including this file inside another function.
So you need to mark the first variable occurrence as global
too.
Btw, global variables are weird, the more simple and correct way to pass the data to the function is to use function parameters.
The $currentPage
defined at the top does not live in global
space. Why don't you just pass the $currentPage
as the first parameter to the drawNumbers function? It's much cleaner that way:
drawNumbers( $currentPage, 1, 1 );
function drawNumbers($currentPage, $from, $to) {
// no need define $currentPage here since it's passed
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With