【问题标题】:how to query two tables from database and show the result in a single row using codeigniter如何使用codeigniter从数据库中查询两个表并在一行中显示结果
【发布时间】:2014-05-26 13:51:22
【问题描述】:

我有两张表,一张供用户使用,另一张用于项目列表

我的问题是我想显示每个项目(来自 project_table)和属于在单行上列出项目(来自 user_table)的用户的电子邮件

project_table 有一行 user_id(即来自 user_table 的标识发布项目的用户的 id)

这是我的观点(project_view): 在这种情况下,我正在显示来自 project_table 的数据,但我想显示来自 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>
<?
 }

我的模特:

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

我的控制器:

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

任何关于如何实现这一点的想法将不胜感激

【问题讨论】:

  • 这可以通过连接或嵌套的 foreach() 来完成。我将尝试证明两者(作为回答)。

标签: php mysql sql codeigniter


【解决方案1】:

您可以在模型中使用联合查询

function get_projects()
{

    $query =$this->db->select('p.*,u.email')
                 ->from('project_table p')
                 ->join('user_table u','p.user_id = u.id')
                 ->get();
     return $query->result();

}

在你看来你可以这样做

<?php
foreach($query as $row)
{ 
?>
    <p> <?php echo $row->pro_name; ?></p>
    <p> <?php echo $row->duration; ?></p>
    <p> <?php echo $row->budget; ?></p>
    <p><?php  echo $row->email;?></p>

<?php
 }
?>

【讨论】:

    【解决方案2】:

    我会重写控制器和模型函数以允许 user 参数:

    function get_projects($user=null)
    {
        if ($user == null){
            $query = $this->db->get('project_table');
        }else{
            $query = $this>db->get('project_table')->where('user_id',$user);
        }
            return $query->result();
    }
    
    
    function show_projects($user)
    {
        $data['query']=$this->project_model->get_projects($user);
        $this->load->view('project_view', $data);
    }
    

    现在您可以使用用户参数调用您的控制器

    http://..../show_user/1
    

    【讨论】:

      【解决方案3】:

      第一种方法是加入

      $this->db->select('project_table.* as project, user.id as user_id, user.email as email')
               ->from('project_table')
               ->join('user', 'project.id = user_id');
      $result = $this->db->get();
      

      第二个不推荐

      function get_projects()
      {
          $query = $this->db->get('project_table');
          return ($query->num_rows() > 0) ? $query->result() : FALSE;
      }
      
      function get_email($id = FALSE) {
          if ($id === FALSE) return FALSE;
          $query = $this->db->get_where('user', array('user_id' => $id));
          return ($query->num_rows() > 0) ? $query->result() : FALSE;
      }
      

      控制器中的某个地方...

      if (!$projects = $this->model->get_projects()) {
      
          foreach ($projects as $key => $project) {
              # code...
              $projects[$key]->user_email = $this->model->get_email($project->user_id);
          }
      
          //projects is now with user_email field
          $this->load->view()...
      
      } else {
      
          //no data recieved
          redirect(); //redirect to default_controller
      }
      

      【讨论】:

        【解决方案4】:

        试试join这个-


        1) 型号

        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) 控制器

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


        3) 查看

        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>";
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-27
          • 1970-01-01
          • 2019-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多