php - Codeigniter 数据库查询两个表

标签 php mysql codeigniter

我是 codeigniter 和 php、mysql 的新手,我正在制作一个 codeigniter 应用程序,并且对使用事件记录的数据库查询有疑问。

我想循环遍历“users”表,同时获取一个名为cv的表,其中userid等于cv记录中的owner_id。简历表有很多列,例如学校、开始日期、结束日期、成绩等。这是我的做法:

Controller :

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

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>

现在我的问题是,这是一个好方法吗?我还希望能够使用我稍后创建的表单来搜索页面上的用户和简历表。我应该在表上进行联接,而不是能够进行更好的搜索查询,还是仍然可以这样做?

感谢任何帮助。

问候

乔治

最佳答案

像这样使用它:

$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>

关于php - Codeigniter 数据库查询两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8064377/

相关文章:

PHP - 如何读取 json 文件头和其他对象并插入 mysql

java - 无法添加或更新子行 : a foreign key constraint fails

java - 创建项目时发生错误 - Eclipse

codeigniter - 如何从 webroot 外部加载 codeigniter font Awesome fontface url?

javascript - 通过在 php 中重新加载整个页面来刷新特定的 div

php - 摆脱日期codeigniter中的单引号

php - 如何将 CURRENT_DATE() 函数与具有指定格式字符串的学说 2 一起使用?

PHP套接字连接优化

php - UPDATE DB SET ... 用 0 或 1 填充一个特定的 VARCHAR,无论我设置的值如何

mysql - 在CentOS7,无法启动MySQL