我是codeigniter和php,mysql的新手,我正在制作一个codeigniter应用程序,并且对使用活动记录的db querys有疑问。
我想循环遍历“users”表,同时获取一个名为cv的表,其中userid等于cv记录中的owner_id。 cv表有很多列,比如school,start_date,end_date,grade等等。继承人我是怎么做到的:
控制器:
function index() {
$data['members'] = $this->user_model->_search_members();
$data['main_content'] = 'agency/start';
$this->load->view('site_view', $data);
}
模特:
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();
}
观点:
<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>
现在我的问题是,这是一个很好的方法吗?我还希望能够使用我稍后创建的表单搜索页面上的用户和简历表。我应该在桌子上加入,以便能够做出更好的搜索查询,还是我仍然可以这样做?
任何帮助表示感谢。
此致
乔治
答案 0 :(得分:3)
像这样使用:
$this->db->select('*');
$this->db->from('users');
$this->db->join('cv', 'users.id = cv.owner_id','left');
$query = $this->db->get();
了解更多信息: http://codeigniter.com/user_guide/database/active_record.html
更新:
$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>