Php 在codeigniter中将查询结果插入到另一个查询中
我需要插入查询结果作为另一个查询的输入 这是我试过的代码,请马上帮我 我需要从query1获取id,并将其作为$id插入query2Php 在codeigniter中将查询结果插入到另一个查询中,php,sql,codeigniter,Php,Sql,Codeigniter,我需要插入查询结果作为另一个查询的输入 这是我试过的代码,请马上帮我 我需要从query1获取id,并将其作为$id插入query2 class Get_db extends CI_Model{ public function get_All(){ $query1 = $this->db->query("SELECT name,id from companydetails"); return $query->result(); }
class Get_db extends CI_Model{
public function get_All(){
$query1 = $this->db->query("SELECT name,id from companydetails");
return $query->result();
}
public function getBranches(){
$query2 = $this->db->query("SELECT branches.name FROM branches WHERE branches.companyid=$id ");
return $query2->result();
}
}
我需要从query1中获取id,并将其作为$id插入query2中。方法get_All()返回一个对象,因此您可以使用:
class Get_db extends CI_Model{
public function get_All(){
$query1 = $this->db->query("SELECT name,id from companydetails");
return $query->result();
}
public function getBranches(){
$query2 = $this->db->query("SELECT branches.name FROM branches WHERE branches.companyid=$id ");
return $query2->result();
}
}
$resultQuery1 = get_All();
$output = array();
foreach($resultQuery1 as $q)
$output[$q->id] = getBranches($q->id);
在getBranchs方法中,您应该接受一个参数
public function getBranches($id) {...}
$output变量是一个数组,包含GetBranchs方法的所有结果。这样就可以了
public function get_All()
{
$this->db->select('name, id');
$query = $this->db->get('companydetails');
if ($query->num_rows () > 0)
{
$result = $query->result();
foreach($result as $company)
{
$company->branches = $this->getBranches($company->id);
}
return $result;
}
else
{
return false;
}
}
public function getBranches($id)
{
$query = $this->db->get_where('branches', array('id' => $id));
return $query->result();
}
请尝试以下代码:
<?php
$this->db->select('name,id')
->from('companydetails');
$qry = $this->db->get();
$res = $qry->result();
foreach($res as $val){
$this->db->select('name')
->from('branches')
->where('companyid', $val->id);
$qry2 = $this->db->get();
$res2 = $qry->result();
}
return $res2;
?>
$res数组是第一个查询的执行结果,将作为第二个查询的输入一个简单函数就可以了。使用
加入
-
<?php
class Get_db extends CI_Model
{
public function get_company_branches()
{
$this->db->select('companydetails.name','companydetails.id','branches.name');
$this->db->from('companydetails');
$this->db->join('branches', 'companydetails.id = branches.companyid');
$query = $this->db->get();
$result = array();
if($query->num_rows() > 0)
$result = $query->result_array();
echo '<pre>'; print_r($result); // Your expected result
}
}
控制器:
...
...
public function somename()
{
$companies = $this->get_db->get_All();
$branches = array();
foreach($companies as $company)
{
$branches[] = $this->get_db->getBranches($company->id);
}
echo '<pre>'; print_r($branches); // Expected result
}
public function home(){
$this->load->model('get_company_model');
$this->load->view('view_header');
$data['results'] = $this->get_company_model->get_all();
$this->load->view('view_nav',$data);
$this->load->view('view_content');
$this->load->view('view_footer');
}
。。。
...
公共函数somename()
{
$companys=$this->get_db->get_All();
$branchs=array();
foreach($公司作为$公司)
{
$branchs[]=$this->get_db->getbranchs($company->id);
}
echo“”;print_r($branchs);//预期结果
}
控制器
<div>
<ol class="tree">
<li>
<label for="folder1"><?=$row->name?></label> <input type="checkbox" id="folder1" />
<ol>
<?php
$myresult=$this->get_company_model->get_branch($row->id);
foreach($myresult as $row2):
?>
<li class="file"><a href="#"><?=$row2->name?></a></li>
<?php
endforeach;
?>
</ol>
</div>
<?php
endforeach;
?>
模型
看法
如果两个表位于同一数据库中,则只需一个查询。关键是连接表。如果你不知道这意味着什么,我听说过这本书的好东西,在10分钟内自学SQL。。。。因此,你可以在GetBranchs()中发送id数组作为参数使用foreach创建一个分支数组并返回该数组,但我需要单独的结果,因为我有另一个usageI我明白你的意思,但我不知道如何执行它3713740我明白你的想法,请在函数下帮助我,并在控制器中作为,$res=$this->model\u name->function\u name(参数);
<div>
<ol class="tree">
<li>
<label for="folder1"><?=$row->name?></label> <input type="checkbox" id="folder1" />
<ol>
<?php
$myresult=$this->get_company_model->get_branch($row->id);
foreach($myresult as $row2):
?>
<li class="file"><a href="#"><?=$row2->name?></a></li>
<?php
endforeach;
?>
</ol>
</div>
<?php
endforeach;
?>