Lindsay is a social girl, so she meets a lot of people and she'd like to organize her notebook as such:
first the tabs, that she names somewhat like 'school', 'work', 'party', 'online' etc.
inside each tab she creates groups like: 'cool', 'handy', 'clingy', 'good kissers', 'marriage material' etc.
and inside those groups are actual guys, each one in one group only.
So, now I'm querying the database to show her the notebook at glance.
function getNotebook($user_id)
{
// $data[][][] = array(); // declare 3-dimensional array
$sql = "SELECT tab_id, tab_name, tab_color FROM tab WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
foreach ($query->result() as $row)
{
// ... (put each tab array in the first dimension of $data array)
$sql = "SELECT group_id, group_name, group_size, group_position FROM group WHERE tab_id = ?";
$query2 = $this->db->query($sql, $row->tab_id);
foreach ($query2->result() as $row2)
{
// ... (put each group array in the second dimension of $data)
$sql = "SELECT person_id, person_name, person_gender, person_eye_color FROM person WHERE group_id = ?";
$query3 = $this->db->query($sql, $row2->group_id);
foreach ($query3->result() as $row3)
{
// ... (put each person array in the third dimension of $data)
}
}
}
return $data;
}
I know it's not good to create HTML right here in the model, so I need to save it (in array?) and return it to controller, and then to model, where I would need to go through it and add HTML tags.
Comments in the function is where I need your help.
Solution #2:
Add user_id field to the person table and get all the information with this query:
$sql = "SELECT * FROM person p
INNER JOIN group g ON g.id = p.group_id
INNER JOIN tab t ON t.id = g.tab_id
WHERE p.user_id = ?
ORDER BY t.id, g.id";
$query = $this->db->query($sql, $user_id);
function getNotebook($user_id)
{
$data = array(); // not applicable
$sql = "SELECT tab_id, tab_name, tab_color FROM tab WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
foreach ($query->result() as $row)
{
$temp1 = array(
'id' => $row->tab_id,
'name' => $row->tab_name,
'color' => $row->tab_color,
'groups' => array()
); // create temporary array for storage after loop
$sql = "SELECT group_id, group_name, group_size, group_position FROM group WHERE tab_id = ?";
$query2 = $this->db->query($sql, $row->tab_id);
foreach ($query2->result() as $row2)
{
$temp2 = array(
'id' => $row2->group_id,
'name' => $row2->group_name,
'size' => $row2->group_size,
'position' => $row2->group_position,
'people' => array()
); // create another temporary array for storage after loop for second dimension
$sql = "SELECT person_id, person_name, person_gender, person_eye_color FROM person WHERE group_id = ?";
$query3 = $this->db->query($sql, $row2->group_id);
foreach ($query3->result() as $row3)
{
$temp2['people'][] = array(
'name' => $row3->person_name,
'gender' => $row3->person_gender,
'eye_color' => $row3->person_eye_color
); // store data
}
$temp1['groups'][] = $temp2; // store temp array
}
$data[] = $temp1; // store temp array
}
return $data;
}
Is that what you're looking for?