title: Magento 1.6.2 Previous Next buttons for moving from one product to another in a sub category
body: <p>i want to create a previous next buttons for moving from one product to another in a specific category.</p>
  
  <p>i was using this code in view.phtml, but now its giving undefined variable error for $search_parameter.</p>
  
  <pre><code>&lt;?php // Previous and Next product links in product page
  
  $_product = $this-&gt;getProduct();
  
  if(!$_product-&gt;getCategoryIds())
  return; // Don't show Previous and Next if product is not in any category
  
  $cat_ids = $_product-&gt;getCategoryIds(); // get all categories where the product is located
  $cat = Mage::getModel('catalog/category')-&gt;load( $cat_ids[0] ); // load first category, you should enhance this, it works for me
  
  $order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
  $direction = 'asc'; // asc or desc
  
  $category_products = $cat-&gt;getProductCollection()-&gt;addAttributeToSort($order, $direction);
  $category_products-&gt;addAttributeToFilter('status',1); // 1 or 2
  $category_products-&gt;addAttributeToFilter('visibility',4); // 1.2.3.4
  
  $cat_prod_ids = $category_products-&gt;getAllIds(); // get all products from the category
  $_product_id = $_product-&gt;getId();
  
  $_pos = array_search($_product_id, $cat_prod_ids); // get position of current product
  $_next_pos = $_pos+1;
  $_prev_pos = $_pos-1;
  
  // get the next product url
  if( isset($cat_prod_ids[$_next_pos]) ) {
  $_next_prod = Mage::getModel('catalog/product')-&gt;load( $cat_prod_ids[$_next_pos] );
  } else {
  $_next_prod = Mage::getModel('catalog/product')-&gt;load( reset($cat_prod_ids) );
  }
  // get the previous product url
  if( isset($cat_prod_ids[$_prev_pos]) ) {
  $_prev_prod = Mage::getModel('catalog/product')-&gt;load( $cat_prod_ids[$_prev_pos] );
  } else {
  $_prev_prod = Mage::getModel('catalog/product')-&gt;load( end($cat_prod_ids) );
  }
  ?&gt;                
  
  
  
  &lt;div class="clear"&gt;&lt;/div&gt;
   &lt;!--PREVIOUS BUTTON NEXT BUTTON STARTS--&gt;
   &lt;div class="previousNext"&gt;
  
  &lt;?php if($_prev_prod != NULL): ?&gt;
  &lt;a href="&lt;?php print $_prev_prod-&gt;getUrlPath(); if($search_parameter):?&gt;?search=1&lt;?php endif;?&gt;"&gt;&lt;span style="margin-right: 270px;"&gt;&lt;?php echo $this-&gt;__('&lt;&lt; PREVIOUS') ?&gt;&lt;/span&gt;&lt;/a&gt;
  &lt;?php endif; ?&gt;
  &lt;?php if($_next_prod != NULL): ?&gt;
  &lt;a href="&lt;?php print $_next_prod-&gt;getUrlPath(); if($search_parameter):?&gt;?search=1&lt;?php endif;?&gt;"&gt;&lt;span style="margin-left: 270px;"&gt;&lt;?php echo $this-&gt;__('NEXT &gt;&gt;') ?&gt;&lt;/span&gt;&lt;/a&gt;
  &lt;?php endif; ?&gt;
  
  &lt;/div&gt;
  &lt;!--PREVIOUS BUTTON NEXT BUTTON ENDS--&gt;
  &lt;div class="clear"&gt;&lt;/div&gt;
  </code></pre>