Welcome Guest, Not a member yet? Register   Sign In
MVC
#1

[eluser]Unknown[/eluser]
Let's say that I have one table with categories like management and secretaries and another table with employees data. each employee belongs to a category.
When I want to show it like this:

management
tony m
rami p

and so on, I need to run one loop for the categories and a nested loop for the employees data (as shown in the attached image)

If I create such loops in the model I don't know how to show it correctly in the view
(basically because I can return only one array variable from the model)

like:
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
#2

[eluser]Cristian Gilè[/eluser]
Hi ronen,

your example is not very clear. Please, provide a more detailed schema for your DB with some entries and use BBCode to highlight your code.
#3

[eluser]Dennis Rasmussen[/eluser]
Use the model to fetch the categories data and another model for the employee data.
Then in your controller you could create a big assoc. array containing first the categories and then the employees as a subarray within each category.

Example:
Code:
$categories = array();
$categories = $this->category_model->get();

foreach($categories as $cat) {
  $cat['employees'] = array();
  $cat['employees'] = $this->employee_model->get_where(array('category_id' => $cat['id']));
}

$data['list'] = $categories;
$this->load->view('template', $data);

Then in your view you just do a loop on 'list' and a nested loop for the employees.
Makes sense?

There are other methods as well such as making one big array with extra data to distinct categories from emlpyees (PHPBB method).




Theme © iAndrew 2016 - Forum software by © MyBB