Jump to content

[SOLVED] Help With An Array


JSHINER

Recommended Posts

Array
(
    [0] => Array
        (
            [category_id] => 25
            [category_name] => Apple
        )

    [1] => Array
        (
            [category_id] => 11
            [category_name] => Banana 
        )

    [2] => Array
        (
            [category_id] => 73
            [category_name] => Clown
        )

    [3] => Array
        (
            [category_id] => 14
            [category_name] => Dog
        )
);

 

With the above array, let's say I'm on a page that is currently "Banana" (page.php?id=11) - how can I get it to know what the next and previous category is? So Next: 73, Previous 25 (using the category_id)

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/
Share on other sites


$cat_id = array(25, 11, 73,14);
$cat_name = array('Apple', 'Bananna', 'Clown', 'Dog');

$category = (int)$_GET['id'];

for ($i = 0; $i < count($cat_name); $i++)
{

if ($cat_id[$i] == $category)
{
$last = --$i;
$next = ++$i;
}

}

echo '<a href="page.php?id=' . $cat_id[$last] . '"><< ' . $cat_name[$last] . '</a> || ';
echo '<a href="page.php?id=' . $cat_id[$next] . '">' . $cat_name[$next] . ' >></a> 

 

 

??

<?php

// Sample Code

$cat_id = array(25, 11, 73,14);
$cat_name = array('Apple', 'Bananna', 'Clown', 'Dog');

$category = 11;

for ($i = 0; $i < count($cat_name); $i++)
{

if ($cat_id[$i] == $category)
{
$last = --$i;
$next = ++$i;
}

}

echo '<a href="page.php?id=' . $cat_id[$last] . '"><< ' . $cat_name[$last] . '</a> || ';
echo '<a href="page.php?id=' . $cat_id[$next] . '">' . $cat_name[$next] .  '></a>';
?>

 

Using the above. It works great for $last, but for $next it is showing as the current (in this case 11 since I set it to that)

Archived

This topic is now archived and is closed to further replies.

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