sasori Posted May 22, 2013 Share Posted May 22, 2013 the stuff is a huge object... to make my "asking for help" explanation simple, I only took the IDs and pushed them in an array..and it goes like this I have items that are being displayed as "comming soon", this means they are not ordered by ID but in the time slot stuff that was recoreded under each item. currently there are 4 comming soon items Array ( [0] => 1624 [1] => 1626 [2] => 1628 [3] => 1627 ) 1 they are displayed like that (horizontaly if you can imagine it with pictures ).. so my problem is,, let's say I am currently viewing the 3rd item's page, and at the browser bar , ofcourse the ID = > 1628 is displayed.and in each page, there is a "previous" and "next" button .....how am I going to get that previous and next ID whenver I am browsing in each pages ? , I tried using the next() and prev() built-in function of PHP but I failed and was bashing my head on table .. Thank you for the help in advance Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 22, 2013 Share Posted May 22, 2013 I assume that you are using the next and prev buttons as 'submit' buttons. So - hide the next/prev ids in hidden input fields in the form with the buttons and retrieve them when you process the button. No? Quote Link to comment Share on other sites More sharing options...
sasori Posted May 22, 2013 Author Share Posted May 22, 2013 I assume that you are using the next and prev buttons as 'submit' buttons. So - hide the next/prev ids in hidden input fields in the form with the buttons and retrieve them when you process the button. No? No, they are not submit buttons ,they are just ordinary buttons.. whereby am planning to give it an href and point each button to the correct item page, any code snippets how to solve that array logic thing? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 22, 2013 Share Posted May 22, 2013 if you have an arbitrary list of data you are traversing, you must find the next/previous based on the current id that was submitted, either by querying the database where you originally got the list from or by storing the list in a session variable. Quote Link to comment Share on other sites More sharing options...
sasori Posted May 22, 2013 Author Share Posted May 22, 2013 if you have an arbitrary list of data you are traversing, you must find the next/previous based on the current id that was submitted, either by querying the database where you originally got the list from or by storing the list in a session variable. I tried it again $next = ''; $prev = ''; $curr = ''; $ar = array(1624,2626,1628,1627); echo "<pre>",print_r($ar),"</pre>"; $curr = $ar[1]; foreach($ar as $a){ if($curr == $a){ $next = prev($ar); } else { $prev = next($ar); } } echo 'current = '. $curr."<br/>"; echo 'previous = '. $prev."<br/>"; echo 'next = ' . $next; and the result was Array ( [0] => 1624 [1] => 2626 [2] => 1628 [3] => 1627 ) 1current = 2626 previous = 1627 next = 2626 Quote Link to comment Share on other sites More sharing options...
Solution Eiseth Posted May 22, 2013 Solution Share Posted May 22, 2013 try flipping the array and compare their keys based on your code $next = ''; $prev = ''; $curr = ''; $ar = array(1624,2626,1628,1627); echo "<pre>",print_r($ar),"</pre>"; $flip_array = array_flip($ar); $curr = $flip_array['1628']; foreach ($flip_array as $a){ if ($a + 1 === $curr) $prev = $a; if ($a - 1 === $curr) $next = $a; } echo 'current = '. $curr."<br/>"; echo 'previous = '. $prev."<br/>"; echo 'next = ' . $next; // result current = 2 previous = 1 next = 3 Quote Link to comment Share on other sites More sharing options...
sasori Posted May 22, 2013 Author Share Posted May 22, 2013 try flipping the array and compare their keys based on your code $next = ''; $prev = ''; $curr = ''; $ar = array(1624,2626,1628,1627); echo "<pre>",print_r($ar),"</pre>"; $flip_array = array_flip($ar); $curr = $flip_array['1628']; foreach ($flip_array as $a){ if ($a + 1 === $curr) $prev = $a; if ($a - 1 === $curr) $next = $a; } echo 'current = '. $curr."<br/>"; echo 'previous = '. $prev."<br/>"; echo 'next = ' . $next; // result current = 2 previous = 1 next = 3 Thanks for this cool array flipping tip...it actually worked, now my code is fixed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.