CodeIgniter Forums
Trying to figure out how to get related portfolios - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Trying to figure out how to get related portfolios (/showthread.php?tid=64485)



Trying to figure out how to get related portfolios - doomie22 - 02-24-2016

Hi there,

I am trying to figure out how to get related portfolios depending on the category of the one they are looking at.  I have the following that works but it has the category hard coded and I cannot for the life of me see a way of doing it.

Code:
  public function get_relatedportfolios() {
      $this->db->select('*');
      $this->db->from('cms_portfolio');
      $this->db->where('category_id', 'Retail');
      $this->db->limit(5);
      $query = $this->db->get();
      return $query->result();
  }

As you can see in my where section has 'Retail' added, I am trying to figure out how I can get information from the page to get the category_id of which ever they are bringing up so I can get the others that are around.

Thanks


RE: Trying to figure out how to get related portfolios - skunkbad - 02-24-2016

If you're looking at a portfolio, you might have the portfolio ID in the URL. You would query the database for that portfolio's category, and then you would have it.

If you're looking at a portfolio category, you might have the portfolio category name or ID in the URL. You would use $this->uri->segement(x) to get it.

So, the data is always there, you just might require more queries or method calls to do what you want to do.

Does this help?


RE: Trying to figure out how to get related portfolios - RobertSF - 02-25-2016

Get the category ID of the portfolio they're looking at, and pass it to get_relatedportfolios().

In your controller:
PHP Code:
// assuming the current portfolio is in an array called $portfolio[] and assuming your model is called portfolio_model
$relatedportfolios $this->portfolio_model->get_relatedportfolios($portfolio['category_id']); 

In your model:
PHP Code:
public function get_relatedportfolios($category_id) {
    
$this->db->select('*');
    
$this->db->from('cms_portfolio');
    
$this->db->where('category_id'$category_id);
    
$this->db->limit(5);
    
$query $this->db->get();
    return 
$query->result();