georgesamper
georgesamper

Reputation: 5169

Codeigniter db query on two tables

Im new to codeigniter and php, mysql and i am making a codeigniter app, and have a question about db querys using active records.

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. Heres how i did it:

The controller:

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

The model:

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();
    }
}

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>

Now my question is, 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?

Any help appreciated.

Regards

George

Upvotes: 0

Views: 3697

Answers (1)

Oyeme
Oyeme

Reputation: 11225

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>

Upvotes: 3

Related Questions