[solved] Switching an Include page w/ the press of a button

Hello everyone and thank you in advance for any help you can provide. To start with I have a project that has help to reduce the amount of hair on my head. It involves a login in which I have and several pages that the end user will view. I am trying to make a base php page to open the rest of the pages. So I have set up a string called $pages = “step_”.$num.".php" this was the easy part, then I access the the page with an include ( “$pages” ), so my code looks like.

<?php
	include ("header.php");
	
	$num = 1;
	$plues = $num++;
	$minus = $num--;
	$pages = "step_".$num.".php";
	$base = "base.php"."?IS=".$logic;
	$logic = 1;
	
	include ( "$pages" );
	
	
echo $num;
echo $pages;
	
if ($num == 1) {
	?>
    
    <TABLE align=right>
          <TR>
            <TD>

         <a href="base.php" onclick="<?php $plues; ?>">
         <img src="http://www.innerlinks.com/IS_Images/continue.gif" />
		 </a>

<!--			<INPUT type="image" name="submit" src="http://www.innerlinks.com/IS_Images/continue.gif" width="74" height="20" />-->
            
			</TD>
		  </TR>
		</TABLE>
	  <br />
	  <br />
	  <br />


    <?php

} else if ($num == 16) {
	?>
    
	  <TABLE align=right>
          <TR>
            <TD>
            <a href="base.php">
            <img src="http://www.innerlinks.com/IS_Images/back.gif" />
			</a>
<!--			<INPUT type="image" name="submit" src="http://www.innerlinks.com/IS_Images/back.gif" width="74" height="20" />
-->           
			</TD>
		  </TR>
		</TABLE>
	  <br />
	  <br />
	  <br />
	  <br />


<?php
} else {
?>

    	  <TABLE align=right>
          <TR>
            <TD>
            <a href="base.php">
            <img src="http://www.innerlinks.com/IS_Images/continue.gif" />
			</a>
<!--			<INPUT type="image" name="submit" src="http://www.innerlinks.com/IS_Images/continue.gif" width="74" height="20" />
-->            
			</TD>
		  </TR>
		</TABLE>
	  <br />
	  <br />
	  <br />
	  <TABLE align=right>
          <TR>
            <TD>
            <a href="base.php">
            <img src="http://www.innerlinks.com/IS_Images/back.gif" />
			</a>
<!--			<INPUT type="image" name="submit" src="http://www.innerlinks.com/IS_Images/back.gif" width="74" height="20" />
-->			
            </TD>
		  </TR>
		</TABLE>
	  <br />
	  <br />
	  <br />
	  <br />

<?php
}
	include ("footer.php");
?>

I have tried different ways to add 1 to my var $num which should reload the include file and change the page but I don’t know were I am going wrong.

I am not asking for anyone to do my work for my please let me just say that now, all I am asking for is a fresh set of eyes and maybe a new perspective on how I might handle going about making my button add 1 to my var.

Thank you again for any and all help on this topic.

Well first of all

$num++ and $num-- both will increment or decrement (respectively) the $num variable.

So your statements will be evaluated as follows:
[php]
//
// Num gets initialized with 1
$num = 1;

// Num gets incremented to 2 and plues gets initialized with 2
$plues = $num++;
// Num gets decremented back to 1 and minus gets initialized with 1
$minus = $num–;
// Pages = step_1.php
$pages = “step_”.$num.".php";

//
[/php]

Since there is nothing (that I saw) that does anything else to change the value of $num, num will always pull up page 1

[php]
//
// Num gets initialized with 1
$num = 1;

// Num gets incremented to 2 and plues gets initialized with 2
$plues = $num++;
// Num gets decremented back to 1 and minus gets initialized with 1
$minus = $num–;
// Pages = step_1.php
$pages = “step_”.$num.".php";

//
[/php]

Actually that’s not entirely true. What happens here is that $plues is initialized at 1 and $minus at 2. The reason for this is that a post-increment is used, and not a pre-increment (for more info, see this page).

The difference is in using ++$var and $var++ :wink:

As to address your problem, I think you should use GET variables for what you’re trying to achieve. A link at the bottom of your page saying ‘Next Page’ should point to, e.g. ‘page.php?page=2’. Pick up the GET variable’s value and use that in your include:

[php]
$viewpage = $GET[‘page’];
include ('step
’ . $viewpage . ‘.php’);
[/php]

Indeed, You are correct. (Sorry about that one), but it’s still to the point that by the time $page gets initialized, it’s back to 1. (I always screw up the PRE and POST increment/decrement I do trial by error on that one).

I also agree about using the GET variable as well. None the less you will need (likely) to use one of the variables (Perhaps your $pluses and $minus) to set that up.

I have seen the areas that were giving me some problem and after reviewing what was said I still seem to be having some problems. Ok I have headed in the direction of the $_GET which I tried before with a switch statement, and the same results keep happening. My page.php?next="" only goes as high as 2. So I am thinking that it has something to so with my button.

I am using:

<?php $pages = $_GET['page']; include ( 'step_'.$pages.'.php' ); ?>

Then I have my buttons that should add or subtract from the page #

<?php
if ($pages == 1) {
?>

<a href="<?php $next = "base.php?page=".++$num; echo $next; ?>">
<img src="continue.gif" />
</a>

<?php
} else if ($pages == 16) {
?>

<a href="<?php 	$prev = "base.php?page=".--$num; echo $prev; ?>">
<img src="back.gif" />
</a>

<?php
} else {
?>

<a href="<?php $next = "base.php?page=".++$num; echo $next; ?>">
<img src="continue.gif" />
</a>

<a href="<?php 	$prev = "base.php?page=".--$num; echo $prev; ?>">
<img src="back.gif" />
</a>

<?php
}
?>

Now page 1 and 2 come up and I can move back and forth between them but I can not continue to page 3. It should add 1 every time I press the next button shouldn’t it?

Why are you using $num to define the ‘next page’ link in the buttons? You know what the current page is (since you’re '$_GET’ting it), and you should go from there, right? Not start with 1 and use ++$num (which results in 2, ALWAYS) and --$num (which results in 0, ALWAYS). Rather than that, you should use $pages + 1 and $pages - 1. This way, not only will you mess with the one variable that tells you which page you are on (you don’t really want to change its value for future purposes), but it makes the $num variable unnecessary too.

Btw, wouldn’t it be nicer for the programmer(s) to use:

[php]
if ($pages > 1) {
// ‘Previous’ button here
}

if ($pages < 16) {
// ‘Next’ button here
}
[/php]

Get rid of the $num and just use the $pages instead. Why use an intermediary variable when one is not needed. $pages = the submitted value (The page you are on) so when You ++ or – it you will get the NEXT or the PREVIOUS page.

The other thing you need to be careful of is that when $pages ==1, then the – should remain at 1 and of course when the ++ exceeds the highest page, you should stay at the highest page (or you can wrap around to the beginning).

EDIT: I guess Zyppora and I were typing at the same time…

Ok I think that I have it now I have set up two new vars that I have placed in my script that allow me to move a new page forward and a single page back :D
[php]
$numN = 1;
$numP = 1;

$pages = $_GET['page'];
include ( 'step_'.$pages.'.php' );

[/php]

this seems to work with the setup that I have in my if else if statement

now on my buttons I have set up

[php]
if ($pages == 1) {
?>

<a href="<?php $numN = $pages; $next = "base.php?page=".++$numN; echo $next; ?>">

<?php

} else if ($pages == 16) {
?>

<a href="<?php $numP = $pages; $prev = "base.php?page=".--$numP; echo $prev; ?>">

<?php } else { ?>

<a href="<?php $numN = $pages; $next = "base.php?page=".++$numN; echo $next; ?>">

<a href="<?php $numP = $pages; $prev = "base.php?page=".--$numP; echo $prev; ?>">

<?php } [/php] This seems to work the way that I need it to and I have to thank both of you for helping me, I could not have done it with out both of you.

Don’t make it so difficult…
[php]

<?php // gets page from passed URL. If none is passed, start at page 1 $pages = !empty($_GET['page']) ? $_GET['page'] : 1; include ( 'step_'.$pages.'.php' ); ?>

[/php]

[php]

<?php if ($pages == 1) { // NEXT PAGE (no previous page because we are at beginning) echo ''; echo ' '; } else if ($pages == 16) { // PREVIOUS PAGE (no next page because we are at end ) echo ''; echo ' '; } else { // NEXT PAGE (Not at beginning or end) echo ''; echo ' '; // PREVIOUS PAGE (Not at beginning or end) echo ''; echo ' '; } ?>

[/php]

Thank you Peg I seem to have a way of over thinking things. :)

Not a problem. hope I was able to help you.

Good luck.

Both you and Zyppora did a lot for me and thank you again now I have to go on and build a session that stores info in a db so that it doesn’t end when a user leaves the site.

Sponsor our Newsletter | Privacy Policy | Terms of Service