如何将嵌套的sql转换为Codeigniter Active记录

时间:2016-05-10 01:10:39

标签: php codeigniter activerecord

如何在CodeIgniter活动记录中编写查询语句?

这是使用php程序在 menu.php 文件中查看代码。 (此代码可以运行)

    <?php

    $m=1;

    echo "<table>
    <tr>
      <td>No</td><td>Menu</td><td>Submenu 1</td><td>Submenu 2</td><td>Submenu 3</td>
    </tr>";

        // main menu
        $query1 = $this->db->query("select id,title,parent,order from tbl_menu where parent='0' order by order;");
        foreach($query1->result_array() as $r) {

    echo "<tr>
        <td>$m</td><td>$row[title]</td><td>-</td><td>-</td><td>-</td>
    </tr>";

        $m++;
        $s1=1;

        // submenu 1
        $query2 = $this->db->query("select * from tbl_menu where parent='$row[id]' order by order;");
        foreach($query2->result_array() as $r) {

    echo "<tr>
        <td></td><td >$s1</td><td>$r[title]</td><td>-</td><td>-</td>
    </tr>";

            $s1++;
            $s2=1;

            // submenu 2

            $query3 = $this->db->query("select * from tbl_menu where parent='$r[id]' order by order;");
            foreach($query3->result_array() as $r2) {


    echo "<tr>
        <td></td><td></td><td >$s2</td><td>$r2[title]</td><td>-</td>
    </tr>";

                $s2++;
                $s3=1;
                // submenu 3
                $query4 = $this->db->query("select * from tbl_menu where parent='$r2[id]' order by order;");
                foreach($query4->result_array() as $r3) {

    echo "<tr>
        <td></td><td></td><td></td><td>$s3</td><td>$r3[title]</td>
    </tr>";
        $s3++;

                } // submenu 3
            } // submenu 2
        } // submenu 1
    } // main menu

    echo"</table>";


    ?>

我改变了这个......(这是错误的)

    <?php

    $m=1;

    echo "<table>
    <tr>
      <td>No</td><td>Menu</td><td>Submenu 1</td><td>Submenu 2</td><td>Submenu 3</td>
    </tr>";

        // main menu
        foreach($query1 as $r) {

    echo "<tr>
        <td>$m</td><td>$row[title]</td><td>-</td><td>-</td><td>-</td>
    </tr>";

        $m++;
        $s1=1;

        // submenu 1
        foreach($query2 as $r) {

    echo "<tr>
        <td></td><td >$s1</td><td>$r[title]</td><td>-</td><td>-</td>
    </tr>";

            $s1++;
            $s2=1;

            // submenu 2

            foreach($query3 as $r2) {


    echo "<tr>
        <td></td><td></td><td >$s2</td><td>$r2[title]</td><td>-</td>
    </tr>";

                $s2++;
                $s3=1;
                // submenu 3
                foreach($query4 as $r3) {

    echo "<tr>
        <td></td><td></td><td></td><td>$s3</td><td>$r3[title]</td>
    </tr>";
        $s3++;

                } // submenu 3
            } // submenu 2
        } // submenu 1
    } // main menu

    echo"</table>";


    ?>

以下是 Menu_model.php 文件中的模型代码。

<?php


class Menu_model extends CI_Model {

    public function menu_q1() {
    $this->db->select('*')->from('tbl_menu')->where('parent','0')->order_by('order', 'ASC');
    $query1 = $this->db->get();
    return $query1->result_array();
    }

    public function menu_q2() {
    $this->db->select('*')->from('tbl_menu')->where('parent',$row['id'])->order_by('order', 'ASC');
    $query2 = $this->db->get();
    return $query2->result_array();
    }

    public function menu_q3() {
    $this->db->select('*')->from('tbl_menu')->where('parent',$r['id'])->order_by('order', 'ASC');
    $query3 = $this->db->get();
    return $query3->result_array();
    }

    public function menu_q4() {
    $this->db->select('*')->from('tbl_menu')->where('parent',$r2['id'])->order_by('order', 'ASC');
    $query4 = $this->db->get();
    return $query4->result_array();
    }

}

?>

这是 Menu_controller.php 文件中的控制器代码。

<?php

Class Menu_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('menu_model'); //Load profil_model
    }

    public function profil() {

        $query1 = $this->profil_model->menu_q1();
        $query2 = $this->profil_model->menu_q2();
        $query3 = $this->profil_model->menu_q3();
        $query4 = $this->profil_model->menu_q4();

    $this->load->view('profil', array('query1' => $query1,'query2' => $query2,'query3' => $query3,'query4' => $query4));


}

?>

这是表格

    --------------------------------------------------------
    No  |  Menu       | Submenu 1 | Submenu 2  | Submenu 3
    --------------------------------------------------------
    $m  | $row[title] | -         | -          | -
        |          $s1| $r[title] | -          | -
        |             |        $s2| $r2[title] | -
        |             |           |         $s3| $r3[title]
    --------------------------------------------------------

但是在这句话中得到错误:

1)where(&#39; parent&#39;,$ row [&#39; id&#39;])

2)where(&#39; parent&#39;,$ r [&#39; id&#39;])

3)where(&#39; parent&#39;,$ r2 [&#39; id&#39;])

1 个答案:

答案 0 :(得分:0)

发生错误是因为您没有将数据传递给模型函数。如果没有传递值,您不能使用任何数据。

POST https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/topics HTTP/1.1
Ocp-Apim-Subscription-Key: mykeyhere
Accept: application/json
Content-Type: application/json
Host: westus.api.cognitive.microsoft.com
Content-Length: 1451699
Expect: 100-continue
Connection: Keep-Alive

{"documents":[{"id":"1","text":"Need to delete unwanted transaction data from QuickBooks? Look no further than the Transaction Pro Deleter. With a few clicks you have deleted unwanted QuickBooks transaction  ....."}]

如果传递函数中的值(上面的示例代码),则可以分别在函数中使用它。