Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用CodeIginter用where子句连接两个表_Php_Mysql_Codeigniter - Fatal编程技术网

Php 使用CodeIginter用where子句连接两个表

Php 使用CodeIginter用where子句连接两个表,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在尝试加载“main_tests”表中的所有数据,其中main_tests.id=test\u name\u price.test\u id和test\u name\u price.list\u id=1 我试着这么做,但它只是说了错误的语法我是代码输入的初学者谢谢你的进步 我已经寻找了很多答案,但我无法得到我需要的确切答案,或者他们的方式不适合我 这是我的密码 $result = $this->db->select('test_name_price.*, main_tes

我正在尝试加载“main_tests”表中的所有数据,其中
main_tests.id=test\u name\u price.test\u id
test\u name\u price.list\u id=1

我试着这么做,但它只是说了错误的语法我是代码输入的初学者谢谢你的进步

我已经寻找了很多答案,但我无法得到我需要的确切答案,或者他们的方式不适合我

这是我的密码

  $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>"
这是我的观点

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


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


                                <?php endforeach; ?>

下面是如何在codeigniter中连接表,但请确保现在有类似的字段名,或者您必须单独选择它并手动重命名字段

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

注意:如果上面的代码是正确的,则必须将结果作为
$result->result()
对象或
$result->result\u array()
数组


编辑:下面是一个关于如何在视图中使用此数组的快速示例,上面的代码将返回数组数组数组,您可以按以下方式处理此数组:

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

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

<?php endforeach; ?>
在您的视图中,只需使用变量
result
或任何您命名的变量循环这个数组,就可以了


IMHO。。你应该阅读codeigniter的文档,并遵循codeigniter中的任何分步教程,因为你的问题不是你不知道如何连接两个表,而是关于如何使用框架本身。

好吧,这就是我的魅力所在

我在没有控制器或模型的视图中使用了@Sherif Salah代码的第一部分 而且它确实起作用了

谢谢你的帮助

                                $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>";

                                    }
                                }
$table_one='test_name_price';
$table_two=‘主_测试’;
$this->db->select($table_one.*,$table_two.*);
$this->db->join($table\u two,“$table\u two.id=
$table_one.test_id“,'left');
$this->db->from($table_one);
$this->db->where(“$table\u one.list\u id=
1");
$query=$this->db->get();
$result_object=$query->result();
如果(计数($result\u object)>0){
foreach($result\u对象为$itemsl){
回显“$itemsl->test”;
}
}

您会遇到什么错误,您可以发布错误吗?您必须将结果作为
$result->result()
对象或
$result->result\u array()
数组。然后处理这个对象或数组。好的,我已经在主控制器中的一个函数中完成了上面的代码,现在我已经绑定到传递它到视图,就像你在编辑值时一样,但是我收到了这样一条消息:未定义变量:result\u array我确信你没有像这样传递它
$this->load->view('your_view',array('result_array'=>$result_array));
您必须将数据作为一个值数组传递到视图。我并没有真正添加它,但在添加它之后,$result_数组下有一个错误,表示它是一个未定义的变量。请记住函数和索引(){$this->load->view('your_view',array('result_array'=>$result_array));}位于同一控制器中,感谢您的关心!编辑您的问题并在模型、控制器、视图中添加您的修改。
<?php foreach ($result_array as $key => value) : ?>

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

<?php endforeach; ?>
$data = array("result" => $result);
$this->load->view('your_view', $data);
                                $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>";

                                    }
                                }