我需要插入查询结果作为另一个查询的输入 这是我尝试过的代码,请立即提供帮助 我需要从query1获取id并将其作为$ id
插入query2class 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答案 0 :(得分:1)
方法get_All()返回一个对象,因此您可以使用:
$resultQuery1 = get_All();
$output = array();
foreach($resultQuery1 as $q)
$output[$q->id] = getBranches($q->id);
在getBranches方法中,您应该接受参数
public function getBranches($id) {...}
$ output变量是一个包含getBranches方法的所有结果的数组。
答案 1 :(得分:1)
试试这段代码:
<?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 数组是第一个查询的执行结果,该查询将作为第二个查询的输入
答案 2 :(得分:0)
这样做......
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();
}
答案 3 :(得分:0)
可以在一个简单的功能中使用。使用 JOIN
-
<?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 get_All()
{
$this->db->select("id","name");
$this->db->from("companydetails");
$query = $this->db->get();
return $query->result();
}
public function getBranches($id)
{
$this->db->select("name");
$this->db->from("branches");
$this->db->where("companyid", $id)
$query = $this->db->get();
return $query->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
}
答案 4 :(得分:0)
控制器
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');
}
模型
public function get_All(){
$query = $this->db->query("SELECT name,id from companydetails");
return $query->result();
}
public function get_branch($companyID){
$query = $this->db->query("SELECT name,id from branches WHERE companyid='".$companyID."'");
return $query->result();
}
视图
<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;
?>