• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about reusing a model function or writing a new onw

I am completely new to CI so I'm still working my way through figuring our best practices.

I am building an app and I would like to load one of a couple different views based on the type of data being returned from the model. I have a table (toycat) and within that, a field named 'cattype'. I currently retrieve the data and tie it to

I want to load a different view file based on whether or not 'cattype' is 0,1,2 etc.

Should I write another function just to return the cattype and call it from the controller or should I just requery the model again? Not sure the best practice here. Code below:

function index($parent = null)
  if ($parent == false)
   $parent = 1;

  $data['cats'] = $this->category_model->getAllChildren($parent);

  //seo data
  $data['title'] = 'category page title here';
  $data['description'] = 'Category description here...'; //passed to meta description tag
  //end seo data

  //send the user to the item view if the category they requested is an item list
  if (//this is where the cattype code would go currently)
   $data['content'] = 'category_item_view'; //this is the name of the view
  } else {
   $data['content'] = 'category_view'; //this is the name of the view
  //load the view
  $this->load->view('includes/template', $data);  


And the model
//gets all child catagories
function getAllChildren($parent_id)
  //get categories that are children of parent ID
  $query = $this->db->get('toycat');
  if ($query->num_rows() > 0)
   return $query->result();
   return FALSE;

Thanks in advance!

[eluser]Mr. Pickle[/eluser]
Is there a reason why you don't store the view name in the toycat table (as it seems to be depending on this)?

Furthermore: will this action take place for (almost) every controller?
If so, you can best make a Public_Controller.php in the libraries folder, let this extend codeigniter's controller and extend your Public_Controller() in every controller that needs this functionality.
You can then for example do a switch() on the $data['cattype'] in the Public_Controller() and determine the view (instead of endless elseif's of the lists is/gets longer) This also keeps it central instead of making this switch in every controller. All of this of course only usefull if you app uses more controllers :p)

Thanks for your reply!

No, this won't be used unless it's this particular controller.

Also, I am using an existing database and the information would have to be populated.

There will also only be 3 different possible views to load.

Thank you!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.