![](/img/trans.png)
[英]how to insert data in two separate tables using single query in codeigniter?
[英]how to query two tables from database and show the result in a single row using codeigniter
我有兩張表,一張用於用戶,另一張用於項目列表
我的問題是我想在一行中顯示每個項目(來自 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);
}
關於如何實現這一點的任何想法將不勝感激
嘗試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>";
}
您可以在模型中使用聯接查詢
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
}
?>
我會重寫控制器和模型函數以允許用戶參數:
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
第一種方法是 JOIN
$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();
2、不推薦
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.