php arrays next() and prev()
I have an array that looks like this,
Array
(
[0] => 849710414
[1] => 849710726
[2] => 849710744
[3] => 849712728
[4] => 849713005
[5] => 849713439
[6] => 849714856
[7] => 849714924
[8] => 849716371
[9] => 849716441
[10] => 849717118
[11] => 849719043
[12] => 849719187
[13] => 849722865
[14] => 849723412
[15] => 849723777
[16] => 849725052
[17] => 849726294
[18] => 849726457
[19] => 849726902
[20] => 849728239
[21] => 849728372
[22] => 849728449
[23] => 849730353
[24] => 849733625
)
These are ID's that I need to add to an URL to navigate around the site, the IDs are the results that get returned from a user's search, the user then clicks on a search result and has the option of going to the n开发者_如何学Goext search result are look at the previous one, now I though I would be able to do this, by doing
<a href="<?=next($array);?>">Next</a>
<a href="<?=prev($array);?>">Prev</a>
How ever I does not seem to recognise what the current result ID I am looking at is to work what is next and previous? Does anybody now how I can solve this?
next()
and prev()
deal with the internal pointer of the array while your script is processing.
What you need to do is find out which index of the array you are currently viewing, (array_search()
) then add 1 to get the next value, and subtract 1 to get the previous value.
// Find the index of the current item
$current_index = array_search($current_id, $array);
// Find the index of the next/prev items
$next = $current_index + 1;
$prev = $current_index - 1;
Your HTML:
<?php if ($prev > 0): ?>
<a href="<?= $array[$prev] ?>">Previous</a>
<?php endif; ?>
<?php if ($next < count($array)): ?>
<a href="<?= $array[$next] ?>">Next</a>
<?php endif; ?>
Using $current_index + 1 and $current_index - 1 may not work if the array keys are not sequential. Here is a better way to find an array key relative to the current one.
function array_key_relative($array, $current_key, $offset = 1, $strict = true) {
// create key map
$keys = array_keys($array);
// find current key
$current_key_index = array_search($current_key, $keys, $strict);
// return desired offset, if in array, or false if not
if(isset($keys[$current_key_index + $offset])) {
return $keys[$current_key_index + $offset];
}
return false;
}
I find this function pretty useful for previous and next buttons and use it a lot.
Of course, now you're going to need to keep the search terms in your url so you can perform the search every page load. Something like this:
http://www.example.com/search/?s=some+search&row=4
...then perform the search and display the result number 4. Your previous and next buttons would reference the row number and do something like this:
$previous_key = array_key_relative($search_results, $current_row, -1);
$next_key = array_key_relative($search_results, $current_row, 1);
That should do it.
PHP arrays contain an internal pointer. This pointer keeps track of the current element. When you create a new array the pointer is to the first element in the array. Calling next()
and prev()
move the pointer forwards and backwards. I suspect that what you want to do is:
<a href="<?php echo $array[$id + 1]; ?>">Next</a>
<a href="<?php echo $array[$id - 1]; ?>">Prev</a>
This example is simplified as it doesn't check that $id
, $id - 1
or $id + 1
are valid.
Take a look at the php docs for current()
. It explains how array pointers work.
I guess you are looking to set the pointer at certain location. There is no out of the box function for that and you will have to do that manually with something like this:
function set (&$array,$key) {
reset ($array);
while (key($array) !== $key) {
if (next($array) === false) throw new Exception('Invalid key');
}
}
$array = array(4=>'a',2=>'b',0=>'c',9=>'d');
set($array,2);
echo current($array);
echo next($array)
If you call next() and then prev(), you're back where you started. These functions move an internal pointer in the array that determines which element is returned by a call to current().
What you probably want to do is keep a reference to the index of the ID for the page you're on, let's say it's stored in $i, and then do something like this:
<a href="<?=$array[$i + 1]?>">Next</a>
<a href="<?=$array[$i - 1]?>">Prev</a>
With appropriate array bounds checking, of course.
精彩评论