doumei1908 2014-04-12 10:14
浏览 32

如何从数据库中查询两个表,并使用codeigniter将结果显示在一行中

I have two tables One for users and the other for project listing

My problem is that i want to display each project(from project_table)and email belonging to user who listed the project(from user_table) on a single row

The project_table has a row for user_id(i.e id from user_table that identifies the user who posted the project)

here's my view(project_view): I this case im displaying data from project_table but i want to display email for a particular user from user_table

<?php 
 foreach($query as $row)
 { 
?>
    <p> <?echo $row->pro_name ?></p>
<p> <?echo $row->duration ?></p>
<p> <?echo $row->budget ?></p>
<p>User email will be displayed here</p>
<?
 }

my model:

function get_projects()
{
    $query = $this->db->get('project_table');
    return $query->result();
}

my controller:

function show_projects()
{
    $data['query']=$this->project_model->get_projects();
    $this->load->view('project_view', $data);
}

Any ideas on how to implement this will be much appreciated

  • 写回答

4条回答 默认 最新

  • dpvhv66448 2014-04-12 10:25
    关注

    Try join this -


    1) Model

    function get_projects()
    {
        $this->db->select('*');
        $this->db->from('project_table');
        $this->db->join('user_table', 'project_table.user_id = user_table.id');
    
        $query = $this->db->get();
    
        if($query->num_rows() > 0)
        {
            return $query->result();
        }
    }
    


    2) Controller

    $data['user_project'] = $this->project_model->get_projects();
    $this->load->view($view, $data);
    


    3) View

    foreach($user_project as $row)
    { 
        echo "<p>" .$row->pro_name. "</p>";
        echo "<p>" .$row->duration. "</p>";
        echo "<p>" .$row->budget. "</p>";
        echo "<p>" .$row->email. "</p>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题