PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I have a list of images and i want to loop them with a next and previous button. But i cannot get my head around coding it. please let me know if u have any ideas.
<?php
// An array of items
$items = array('A', 'B', 'C', 'D', 'E');
// Hyperlink factory
function link ($i)
{
printf('<a href="?i=%1$s">%1$s</a>', $i);
}
// Shows a list of items and their reference numbers
echo '<ul>';
foreach ($items as $i => $item) {
echo '<li>'.$item.': ';
link($i + 1);
echo '</li>';
}
echo '</ul>';
$first = 1;
$last = count($items);
$current = $first;
// If an index is requested and the request is a number
if (isset($_GET['i']) && !preg_match('/[^0-9]/', $_GET['i'])) {
// If the request is too low...
if ($_GET['i'] < $first) {
// ...loops around to the end
$current = $last;
// If the request is too high...
} else if ($_GET['i'] > $last) {
// ...loops around to the beginning
$current = $first;
// Otherwise...
} else {
// ...uses the requested index
$current = $_GET['i'];
}
}
$previous = $current - 1;
$next = $current + 1;
?>
<ul>
<li>First: <?php link($first); ?></li>
<li>Previous: <?php link($previous); ?></li>
<li>Current: <?php link($current);
// The current item's index is one less than $current
echo ' ('.$items[$current - 1].')'; ?></li>
<li>Next: <?php link($next); ?></li>
<li>Last: <?php link($last); ?></li>
</ul>
Edit: Oops, looks like Eric! beat me too it. But, Eric!, your solution doesn't work because $curr is not passed between pages.
My solution has the advantage of being bookmark-able.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 11:35 am, edited 1 time in total.
<?php
// An array of items
$items = array('A', 'B', 'C', 'D', 'E');
// Hyperlink factory
function link ($i)
{
printf('<a href="?i=%1$s">%1$s</a>', $i);
}
// Shows a list of items and their reference numbers
echo '<ul>';
foreach ($items as $i => $item) {
echo '<li>'.$item.': ';
link($i + 1);
echo '</li>';
}
echo '</ul>';
$first = 1;
$last = count($items);
$current = $first;
// If an index is requested and the request is a number
if (isset($_GET['i']) && !preg_match('/[^0-9]/', $_GET['i'])) {
// If the request is too low...
if ($_GET['i'] < $first) {
// ...loops around to the end
$current = $last;
// If the request is too high...
} else if ($_GET['i'] > $last) {
// ...loops around to the beginning
$current = $first;
// Otherwise...
} else {
// ...uses the requested index
$current = $_GET['i'];
}
}
$previous = $current - 1;
$next = $current + 1;
?>
<ul>
<li>First: <?php link($first); ?></li>
<li>Previous: <?php link($previous); ?></li>
<li>Current: <?php link($current);
// The current item's index is one less than $current
echo ' ('.$items[$current - 1].')'; ?></li>
<li>Next: <?php link($next); ?></li>
<li>Last: <?php link($last); ?></li>
</ul>
Edit: Oops, looks like Eric! beat me too it. But, Eric!, your solution doesn't work because $curr is not passed between pages.
My solution has the advantage of being bookmark-able.
I can't from this machine to my annoyance. Sorry I forgot the echo. I did spot another thing, count returns the total element number, so you need to subtract one to move it back into the range of the index.
I still think it is a pretty simple solution, even if it didn't compile right in my head and people can't cut and paste it instantly. (I did say there might be a bug or two...)