Jump to content

Creating next button from an array


Pjack125

Recommended Posts

I am trying to figure out how to get info from an array and use them for a "next" button to go to a new page. for example lets say we have

 

$MyArray = array("page1","page2","page3");

 

what i want to do is when the user clicks the next button it would go to newpage.php?Myarray=Page1 and then while on that page if the next button is clicked it will become newpage.php?Myarray=Page2 and so on.. how would i go about doing that?

Link to comment
https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/
Share on other sites

Close but something is still off to where its not working.

 

I just added two lines to it to create URLS with value from the next and and pref variable.

 

<?php

$MyArray = array("page0","page1","page2","page3");
$curPage = array_search($_GET['page'],$MyArray);
$next = next($MyArray);
$pref = prev($MyArray);

echo "<a href=leWizPM.php?page=".$pref."><<please work</a> |";
echo "<a href=leWizPM.php?page=".$next.">please work>></a>";

?>

 

I think it doesn't work because we are not setting $_GET['page'] as the current array value. my issue is now i can't figure out how to write that any idea? I am totally new at this

<?php

if(!isset($_GET['page'])) $page = 'page1';
else $page = $_GET['page'];

$MyArray = array("page0","page1","page2","page3");
$curPage = array_search($page,$MyArray);
$next = next($MyArray);
$pref = prev($MyArray);

echo "<a href=leWizPM.php?page=".$pref."><<please work</a> |";
echo "<a href=leWizPM.php?page=".$next.">please work>></a>";

?>

the think you need the double prev function in there, because it moves the pointer to next, and when you use prev, it brings the pointer back to the current selection, so you need to do prev again to go to the one before it.

 

You will need to add two if constructs to check to see if the value is in the array.

 

if(!isset($_GET['page'])) $page = 'page1';
else $page = $_GET['page'];

$MyArray = array("page0","page1","page2","page3");
$curPage = array_search($page,$MyArray);
$next = next($MyArray);
$pref = prev(prev($MyArray));

echo "<a href=leWizPM.php?page=".$pref."><<please work</a> |";
echo "<a href=leWizPM.php?page=".$next.">please work>></a>";

Still no dice I'm testing it at the bottom of  a form i am going to use it on located here

http://www.pixelfxmedia.com/leWizPM.php?page=page1

 

I still think the issue is we need to set the current page in the array if that is possible I'm not too familiar with php. or somehow get which key the current page is set to in the array then move up or down the array that way.

Try this:

 

if(!isset($_GET['page'])) $page = 'page1';
else $page = $_GET['page'];

$MyArray = array("page0","page1","page2","page3");
$curPage = array_search($page,$MyArray);
$next = next(next($MyArray));
$pref = prev(prev($MyArray));

echo "<a href=leWizPM.php?page=".$pref."><<please work</a> |";
echo "<a href=leWizPM.php?page=".$next.">please work>></a>";

I tried using the following and it works except now i have the issue of it going out of the array range.

<?php

if(!isset($_GET['page'])) $page = 'page1';
else $page = $_GET['page'];

$MyArray = array("page0","page1","page2","page3");
$curPage = array_search($page,$MyArray);
$next = $MyArray[$curPage  +1];
$pref = $MyArray[$curPage -1];


echo "<a href=leWizPM.php?page=".$pref."><<please work</a> |";
echo "<a href=leWizPM.php?page=".$next.">please work>></a>";

?>

 

The one thing i noticed is that the $curPage = array_search($page,$MyArray); just return the key value of the array. what I am thinking is there anyway to set the current attribute of of an array to something. such as making the "current($MyArray) val equal the index for lets say "page2"  and then use the next($MyArray) to get the next val from there. Help with either of the two would be much appreciated I am not just trying to get this page done but I like to learn as i go so a few days down the road i wont ask the same question again. 

Here is what I got to work... I don't know if its proper coding or not but it works. If anyone can figure out how to make the first Idea work please write it in here for future ref.

 

<?php

if(!isset($_GET['page'])) $page = 'Monkey';
else $page = $_GET['page'];

$MyArray = array("EE","ME","OE","SW","SE","ST");
$curPage = array_search($page,$MyArray);
$next = $MyArray[$curPage  +1];
$pref = $MyArray[$curPage -1];

$blah = sizeof($MyArray) -1; //counts the length of the array and substract it by two that way the last +1 wont give a null vall

if (0<$curPage){ 
echo "<a href=leWizPM.php?page=".$pref."> << ".$pref."</a> |";
}
if ($curPage< $blah){ // Constrains the line below to the array length/size
echo "<a href=leWizPM.php?page=".$next.">".$next.">></a>";
}


?>

 

hope this helps someone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.