Increment on button click and display image

Hey. I’ve been working on this the past few days and it hasn’t been doing what I want it to do. I’m trying to make a image viewer, that does First, Back, Next, and Last. Right now, I’m just trying to get the Back and Next buttons working.

I tried

session_start();
require_once 'imgviewer_fns.php';

$_SESSION['current_page']  = 0;

display_image($_SESSION['current_page']);
display_nav_bar($_SESSION['current_page']);


if (isset($_GET['Next'])){
++$_SESSION['current_page'];
display_image($_SESSION['current_page']);
display_nav_bar($_SESSION['current_page']);
}

and then on my Back and Next buttons I had a hidden input type that echo’s the $_SESSION[‘current_page’]. Doesn’t work, only time I got it to do anything was was when I incremented and decremented the $_SESSION[‘current_page’] inside the hidden input type value field. And when I did that, it only did it once, from 0 to 1 and back.

I’m guessing I’m doing a silly mistake, because I looked up similar questions, some on Sitepoint, and the answers were simple and similar to what I was doing.

It’s hard to tell where the code above sits - right at the top you set ‘current page’ to zero, but where does that appear in your page? If it does that every time that might cause a problem.

When you have the hidden variable with your button code, if you right-click and view code, is it showing the correct value? I must admit that if that value is output by your php, I’d probably be setting it to whatever value I needed rather than incrementing it after the button press - so I’d output the ‘next’ button code with a hidden current_page of current_page+1, the ‘first’ code would have current_page=0, previous would have ((current_page-1<0)?0:current_page-1) and so on. Either would work once you crack why the values are going wrong.

Like droopsnoot said, it’s better to already display the link to the good page on your next and previous button. Or else, if you have, say, 10 pages, what will happen if the user is on the last page and hit next?

Either you will get an error or have to display a message saying ‘Hey, this page doesn’t exists’ (which is annoying).
OR you’ll have to write code anyway to hide your ‘next’ and ‘previous’ button…

So, you probably want to calculate the value of the ‘next page’ and ‘previous page’ before the user clicks on a button.


$currentPage = $_GET['page'];
$nextPage = $currentPage + 1;
$previousPage = $currentPage - 1;

if ($nextPage > 10) {
  $nextPage = 0;  // if we are on the last page, set next page to 0 (so that we don't display the 'next' link)
}

if (previousPage < 0) {
  $previousPage = 0;  // if we're on the first page, let's set the previous page to 0 (so that we don't display the previous link)
}

And then, in your HTML:


<?php if ($previousPage !== 0): ?>
<a href='?page=<?php echo $previousPage ?>'>PREVIOUS</a>
<?php endif; ?>

<?php if ($nextPage !== 0): ?>
<a href='?page=<?php echo $nextPage ?>'>NEXT</a>
<?php endif; ?>

Using a SESSION here is, in my opinion, overkill and will use unnecessary server resources (not that it’s really intense, but still, it’s not necessary).

Thanks! $current_page = 0 is the first thing that appears, at the top of the page, and that’s the only time it shows up. Can you explain more about why it doesn’t work please, just so I don’t do it again?

and what’s the best way to track variables as they output? I tried echo, but it really doesn’t help that much.

Thanks! I’m going to try it.

Well, I’m not saying it won’t work, I’m just saying that if you reset the current_page to zero every time you draw the page, pressing the ‘next’ button as you have it coded is only ever going to increase to one. Next time you press the button, it will reopen the page, reset current_page to zero, then increment it to one again. That’s why I was asking about how the code is laid out - that might have been an excerpt from an initialisation page.

Well, echo and print_r are the normal ways, but if you’ve got a lot of HTML it can be hard to see where they appear on the page. So you could do something like


echo "<!-- *VARVALUE* page is currently $current_page -->";

which in theory will cause the value to be visible if you right-click and view source, but not upset the page layout because it’s just a HTML comment as long as you put it in a reasonable place. The VARVALUE is just a text string that makes it easier to find. (I haven’t tried this, by the way).

edited…still working on it before i ask another question

Variable is setting to 0, but not actually passing to to the hidden input type.

The index.php code is this

require_once 'imgviewer_fns.php';
 
$current_page = (isset($current_page)) ? $current_page = $_GET['currentpage'] : 0;

$next_page = $current_page + 1;
$prev_page = $current_page - 1;

echo $current_page;

display_image($current_page );
display_nav_bar($current_page );

and the HTML is this

<td><form action ="index.php" method="get">
<input type ="submit" value ="Next">
<input type ="hidden" name ="currentpage" value ="<?php echo $next_page; ?>"></form> </td>

<td><form action ="index.php" method="get">
<input type ="submit" value ="Prev">
<input type ="hidden" name ="currentpage" value ="<?php echo $prev_page; ?>">
</form> </td>

I know $current_page is zero because it echos out, and currentpage shows up in the query string, but it’s blank after the equal sign, and also when I click view pages source on my website.

Edit: Pretty sure it’s because I still have $current_page in the function paramater of display_nav_bar. Fixing it.

Here’s a little sample. Haven’t looked through all your code.

<?php
$current_page = (isset($_GET['currentpage']) ? $_GET['currentpage'] : 0);
//Set max pages
//$max = count($pages);  // or whatever
$max = 25;

$next_page = $current_page + 1;
$prev_page = $current_page - 1;	

if($current_page>0):	
	echo '<a href="?currentpage=' . $prev_page . '">Prev</a>&nbsp;&nbsp;&nbsp;';
endif;	
if($current_page<$max):	
	echo '<a href="?currentpage=' . $next_page . '">Next</a><br />';
endif;	
echo $current_page;
?>

And it works just as well with the forms

<?php
$current_page = (isset($_GET['currentpage']) ? $_GET['currentpage'] : 0);
//Set max pages
//$max = count($pages);  // or whatever
$max = 25;

$next_page = $current_page + 1;
$prev_page = $current_page - 1;	

if($current_page>0):
?>
<form action ="" method="get">
<input type ="submit" value ="Prev">
<input type ="hidden" name ="currentpage" value ="<?php echo $prev_page; ?>">
</form>
<?php
endif;	
if($current_page<$max):
?>
<form action ="" method="get">
<input type ="submit" value ="Next">
<input type ="hidden" name ="currentpage" value ="<?php echo $next_page; ?>">
</form>
<?php
endif;	
echo $current_page;
?>

If the HTML is output from within a function, but $next_page and $prev_page are created from outside that function, then they must be passed in as parameters. Or if you’re only passing in $current_page, you have to calculate the values of $next_page and $current_page inside the function. Or you can declare those variables as globals inside the function, which would make them accessible inside and out - depending on what else you do with them.