【问题标题】:Codeigniter db query on two tablesCodeigniter db 查询两个表
【发布时间】: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


    【解决方案1】:

    像这样使用它:

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

    【讨论】:

    • 好的。感谢您的快速回复。但是我如何遍历它们,以便我在 html 表的一行中获取所有信息。假设用户在 cv 数据库中有 5 所学校。我希望他们出现在一个 td 中。而不是使用用户名和所有用户信息获得 5 td?
    • 您应该将 DB 中的数据分离到 user group array
      像这样://here is your $group = array(); $data = 来自 DB foreach 的数据 ($data as $k=>$v) { $group[$v['id']] = $v; }
    • 我无法让它工作。只是我很难掌握这一点。我仍然得到 5 行的用户名。你能用这段代码发布一个例子吗?真的很感激 :) 在此先感谢
    • 这是一种不好的做法吗?似乎查询需要更长的时间来检索数据? $data = $this-&gt;db-&gt;query('SELECT users.id, users.first_name, users.username ,users.last_name, GROUP_CONCAT( cv.school ) AS schools, GROUP_CONCAT( cv.occupation ) AS occupation, GROUP_CONCAT( cv.instrument ) AS instrument, GROUP_CONCAT( cv.program ) AS program FROM users INNER JOIN cv ON users.id = cv.owner_id GROUP BY users.id'); ?&gt; 我知道查询没有转义。如果我可以这样做,那么我该如何逃避呢?或者让我们使用活跃的记录?
    • 这不是一个好方法。你应该使用'joins'查询。对于特定的有限数据最好使用GROUP_CONCAT。
    猜你喜欢
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多