JSHINER Posted March 18, 2009 Share Posted March 18, 2009 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 More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 What is your code so far that tells the page that it is "Banana" Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787720 Share on other sites More sharing options...
JSHINER Posted March 18, 2009 Author Share Posted March 18, 2009 The page hits another query using the ?id= in the address. So the page knows its "Banana" by the page.php?id=11 (11 is the category_id of Banana) Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787721 Share on other sites More sharing options...
Andy-H Posted March 18, 2009 Share Posted March 18, 2009 $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 https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787728 Share on other sites More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 What Where is your code so far that tells the page that it is "Banana" Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787731 Share on other sites More sharing options...
JSHINER Posted March 18, 2009 Author Share Posted March 18, 2009 Andy-H - Thanks! But... That returns Next as 14 - Last as 73 What I was looking for was Next: 73, Prev: 25 Any thoughts? Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787755 Share on other sites More sharing options...
JSHINER Posted March 18, 2009 Author Share Posted March 18, 2009 Actually sorry - that returns nothing. Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787765 Share on other sites More sharing options...
Andy-H Posted March 18, 2009 Share Posted March 18, 2009 Do you have any actual code you are using for this already, it would be alot easier to understand what you are trying to achieve if you could paste it here? Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787793 Share on other sites More sharing options...
JSHINER Posted March 18, 2009 Author Share Posted March 18, 2009 <?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 https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787798 Share on other sites More sharing options...
Andy-H Posted March 18, 2009 Share Posted March 18, 2009 $last = $i - 1; $next= $i + 1; Sorry, try this in place of - $last = --$i; $next = ++$i; Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787802 Share on other sites More sharing options...
JSHINER Posted March 18, 2009 Author Share Posted March 18, 2009 Perfect! Thanks! Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787803 Share on other sites More sharing options...
Andy-H Posted March 18, 2009 Share Posted March 18, 2009 NP Link to comment https://forums.phpfreaks.com/topic/149993-solved-help-with-an-array/#findComment-787805 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.