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

 

 

??

Link to comment
Share on other sites

<?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)

Link to comment
Share on other sites

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.