用编码器将查询结果插入到另一个查询中


insert result of query into another query in 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();
    }
    public function getBranches(){
        $query2 = $this->db->query("SELECT branches.name FROM branches WHERE           branches.companyid=$id ");
        return $query2->result();
    }
}

我需要从query1中获取id并将其插入query2中作为$id

get_All()方法返回一个对象,因此您可以使用:

$resultQuery1 = get_All();
$output = array();
foreach($resultQuery1 as $q)
    $output[$q->id] = getBranches($q->id);

在getBranches方法中你应该接受一个参数

public function getBranches($id) {...}

$output变量是一个数组,包含getBranches方法的所有结果。

试试下面的代码:

    <?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数组是第一个查询的执行结果,它将作为第二个查询

的输入

这就行了

      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();
      }

可以在中使用一个简单的函数。使用 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
   }
}


如果您不想使用JOIN并直接回答您的问题,那么您可以使用


模型:

...
...
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
}

controller

 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;
    ?>