【发布时间】:2011-11-09 11:31:09
【问题描述】:
我是 codeigniter 和 php、mysql 的新手,我正在制作一个 codeigniter 应用程序,并且对使用活动记录的 db 查询有疑问。
我想遍历“users”表,同时得到一个名为 cv 的表,其中 userid 等于 cv 记录中的 owner_id。 cv 表有很多列,例如学校、开始日期、结束日期、成绩等。我是这样做的:
控制器:
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>
现在我的问题是,这是一个好方法吗?我还希望能够使用稍后创建的表单来搜索页面上的用户和简历表。我应该在表上进行连接以进行更好的搜索查询还是仍然可以这样做?
任何帮助表示赞赏。
问候
乔治
【问题讨论】:
标签: php mysql codeigniter