简体   繁体   中英

Join two tables with where clause using CodeIginter

I'm trying to load all the data from table "main_tests" where main_tests.id = test_name_price.test_id and where test_name_price.list_id = 1

I'm trying to do that but there it's just saying wrong syntax I'm beginner to CodeIginter Thanks For Advance

I have looked a lot for the answer but I couldn't get what I need exactly or either their ways don't work with me

here is my code

  $result =  $this->db->select('test_name_price.*, main_tests.*')
                                    ->from('test_name_price')
                                    ->where('test_name_price.list_id', 1)
                                    ->join('main_tests', 
                                  'test_name_price.test_id = main_tests.id')
                                    ->get();


                                if (count($result) > 0) {

                                    foreach ($result as $itemsl) {

                                        echo "


                    <option value='$itemsl->testname' data-code='$itemsl- 
                                      >test_code'>$itemsl->test_id</option>



                                         ";
                                    }
                                }

                                echo "</select>"

EDIT :

that's the code in my controller , that view is addpatient

    public function addpatient()

{

      $this->load->model("khmodel","Khmodel");


     $this->load->view('addpatient', array('result_array' => 
           $result_array));


}

and here is my view

      <?php foreach ($result_array as $key => $value) : ?>


        <option value='<?= $value['testno'] ?>' data-code='<?= 
          $value['testno'] ?>'><?= $value['test'] ?></option>


                                <?php endforeach; ?>

and here is the MasterPage function I'm using to load the view , tell me if I need to add anything else

    public function MasterPage($view='',$table='',$da='',$spg='',$szpg='')

{
    if (!empty($table)) {
        $data['result']=$this->Khmodel->get($table,$da,$spg,$szpg,"id 
             desc");
        $data['pages']=$this->Khmodel->pagesno($table,$szpg);
    }
    $data['sview']=$view;
    $data['index']=($view == 'home') ? '' : 'iner_page';


    $getlang =$this->uri->segment(1);//$this->input->cookie('shlang', TRUE);

    if($getlang=='en'){



        $this->lang->load('en', 'en');
        $data['lang']='en';
        $data['nav_align']='right';
        $data['xnav_align']='left';


    }
    else{




        $this->lang->load('ar', 'ar');
        $data['lang']='ar';
        $data['nav_align']='left';
        $data['xnav_align']='right';


    }

    //$this->load->view('main/close');
    $this->load->view('main/header',$data);
    if($view!='home'){
        $this->load->view('main/top_header',$data);

    }
    $this->load->view('main/'.$view);

    $this->load->view('main/footer',$data);

}

Well this what's worked with me like a charm !

I have used @Sherif Salah First Part of code INSIDE THE VIEW WITHOUT CONTROLLER OR MODEL and It worked actually !!

Thanks for your Help

                                $table_one = 'test_name_price';
                                $table_two = 'main_tests';
                                $this->db->select("$table_one.*,$table_two.*");
                                $this->db->join($table_two, "$table_two.id = 
                                     $table_one.test_id", 'left');
                                $this->db->from($table_one);
                                $this->db->where("$table_one.list_id = 
                                         1");
                                $query = $this->db->get();
                                $result_object = $query->result();



                                if (count($result_object) > 0) {

                            foreach ($result_object as $itemsl) {

              echo "<option value='$itemsl->tsprice' data-code='$itemsl- 
                          >testno'>$itemsl->test</option>";

                                    }
                                }

Here is how to join tables in codeigniter, but make sure that there are now similar field names or you have to select it individually and rename the field by hand.

$table_one = 'table_one_name';
$table_two = 'table_two_name';
$this->db->select("
    $table_one.*,
    $table_two.*
    ");
$this->db->join($table_two, "$table_two.id = $table_one.table_two_id", 'left');
$this->db->from($table_one);
$query = $this->db->get();

$result_object = $query->result();
$result_array = $query->result_array();

Note: if your code above is correct, you have to get the result as $result->result() as an object or $result->result_array() as an array.


Edit: Here is a quick example about how to use this array in your view, the above code will return array of arrays and you can process this array as:

<?php foreach ($result_array as $key => value) : ?>

    <option value='<?= $value['name'] ?>' data-code='<?= $value['code'] ?>'><?= $value['id'] ?></option>

<?php endforeach; ?>

Note: please keep your logic in your controller, just pass the result to the view.


Edit: Lets keep it simple for now, supposingly your query worked fine and you can check for this by var_dump($result) so now you need to pass this data to the view however you wanna do it but supposingly AGAIN you got you result in the controller and all works fine, now you just have to pass it to the view like this:

$data = array("result" => $result);
$this->load->view('your_view', $data);

and in your view just loop through this array using the variable result or whatever you named it and that's it.

IMHO .. you should work through codeigniter's documentation and follow any step by step tutorial in codeigniter cause your problem is not that you don't know how to join two table, its about how to use the framework itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM