Jump to content

how to work on the previous and next page?


Go to solution Solved by Eiseth,

Recommended Posts

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

 

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?  :happy-04:

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.

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
  • Solution

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

 

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  :happy-04:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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