Pjack125 Posted December 1, 2008 Share Posted December 1, 2008 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 More sharing options...
The Little Guy Posted December 1, 2008 Share Posted December 1, 2008 There are a few ways... Here is one of them (untested): $MyArray = array("page1","page2","page3"); $curPage = array_search($_GET['page'],$MyArray); $next = next($MyArray); $pref = prev(prev($MyArray)); Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703007 Share on other sites More sharing options...
Pjack125 Posted December 1, 2008 Author Share Posted December 1, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703033 Share on other sites More sharing options...
gevans Posted December 1, 2008 Share Posted December 1, 2008 <?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>"; ?> Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703044 Share on other sites More sharing options...
The Little Guy Posted December 1, 2008 Share Posted December 1, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703049 Share on other sites More sharing options...
Pjack125 Posted December 1, 2008 Author Share Posted December 1, 2008 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. Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703058 Share on other sites More sharing options...
The Little Guy Posted December 1, 2008 Share Posted December 1, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703111 Share on other sites More sharing options...
Pjack125 Posted December 1, 2008 Author Share Posted December 1, 2008 I thought about that before all that does is generate the following error Warning: next(): Passed variable is not an array or object the values end up being blank. Is there another way to approach this maybe i should give up on the array idea? Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703118 Share on other sites More sharing options...
Pjack125 Posted December 1, 2008 Author Share Posted December 1, 2008 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. Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703148 Share on other sites More sharing options...
Pjack125 Posted December 1, 2008 Author Share Posted December 1, 2008 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. Link to comment https://forums.phpfreaks.com/topic/134987-creating-next-button-from-an-array/#findComment-703190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.