phpcodeignitermodel-view-controllergroupingone-to-many

Show records from two one-to-many database tables as an HTML table with Codeigniter


I am making a CodeIgniter app and have a question about database queries using query builder methods and appropriate use of MVC design.

I want to loop through the "users" table and at the same time get a table called cv where the userid is equal to owner_id in the cv records. The cv table has a lot of columns like school, start_date, end_date, grades etc. Here's how I did it:

The controller:

public function index()
{
    $data['members'] = $this->user_model->_search_members();
    $data['main_content'] = 'agency/start';
    $this->load->view('site_view', $data);
}

The model:

public function _search_members()
{
    $this->db->select('id,username,first_name,last_name,company,presentation,title_1,title_2,title_3,last_login,user_pic,counties,municipalities,birthday,gender,webpage')->from('users');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
    }
}

public function _get_cv($id)
{
    $this->db->select()->where('owner_id',$id);
    $query = $this->db->get('cv');

    return $query->result();
}

The view:

<section id="main">

<h2>Search members</h2>

<table>
    <tr>
        <td></td>
        <td>User</td>
        <td>Gender</td>
        <td>Name</td>
        <td>Title</td>
        <td>Location</td>
        <td>Age</td>
        <td>School</td>
    </tr>
    <?php foreach ($members as $member) : ?>
        <?php $cvs = $this->user_model->_get_cv($member->id); ?>
        <tr>
            <td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
            <td><a href="profile/view/<?php echo $member->id; ?>"><?php echo $member->username;?></a></td>
            <td><?php echo $member->gender;?></td>
            <td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
            <td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
            <td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
            <td><?php echo $member->birthday;?></td>
            <td>
                <?php foreach ($cvs as $cv) : ?>
                    <?php echo $cv->school; ?>
                <?php endforeach; ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

Is this a good way to do it? I also want to be able to search through the users and cv tables on the page with a form I am creating later. Should I do a join on the tables instead to able to make better search queries or can I still do it this way?


Solution

  • Use it like this:

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('cv', 'users.id = cv.owner_id','left');
    $query = $this->db->get();
    

    For more information: http://codeigniter.com/user_guide/database/active_record.html

    Updated:

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('cv', 'users.id = cv.owner_id','left');
    $data = $this->db->get();
    
    $group_cv = $users = array();
    foreach ($data as $k=>$v) {
        $group_cv[$v['id']][] = $v;
    }
    
    foreach ($data as $k=>$v) {
        $users[$v['id']] = $v;
    }
    
    ?>
    
    <section id="main">
    
    <h2>Search members</h2>
    
    <table>
        <tr>
            <td></td>
            <td>User</td>
            <td>Gender</td>
            <td>Name</td>
            <td>Title</td>
            <td>Location</td>
            <td>Age</td>
            <td>School</td>
        </tr>
        <?php foreach ($users as $member) : ?>
            <tr>
                <td><img src="<?=base_url()?>images/users/thumbs/<?=$member->user_pic;?>" alt=""></td>
                <td><a href="profile/view/<?php echo $member->id; ?>"><?php echo $member->username;?></a></td>
                <td><?php echo $member->gender;?></td>
                <td><?php echo $member->first_name; ?> <?php echo $member->last_name; ?></td>
                <td><?php echo $member->title_1; ?> / <?php echo $member->title_2; ?> / <?php echo $member->title_3; ?></td>
                <td><?php echo $member->counties; ?> i <?php echo $member->municipalities; ?></td>
                <td><?php echo $member->birthday;?></td>
                <td>
                    <?php foreach ($group_cv[$member['id']] as $cv) : ?>
                        <?php echo $cv->school; ?>
                    <?php endforeach; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>