douan6815 2015-08-03 13:03
浏览 17

加入查询与篝火(codeigniter)

I'm trying to do a join query in bonfire (which is great for the beginner i am btw)

I first tried to set a public function in the method, but after a while, i though it would be better to put it in the controller (maybe i'm wrong...?)

However, i guess my join request is valid, but i can't get it in my view...

Here is my controller:

class Patient extends Admin_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('post_model');

        Template::set_block('search/search', 'search/search');
        //Template::set('toolbar_title', 'Manage Your Blog');
        Template::set_theme('custom', 'default');
        Template::set_block('nav', 'nav');
    }

    public function detail($id = null) {
        $this->load->helper('typography');
        $this->load->model('operation_model');

        $this->db->select('*');
        //$this->db->from ('operation AS ope');
        $this->db->from('operation');

        $this->db->join('patient', "patient.zkp_pat = operation.zkf_pat ");

        $query = $this->db->get();
        $post = $this->post_model->find($id);

        //Template::set('post', $query);
        Template::set('post', $post);

        Template::set_view('detail');
        Template::render();
    }
}

And my view file:

<code>
    <div class="post">
        <?php
        echo ($post->nom . ' ');
        echo ($post->prenom . ' ');
        echo ($post->zkp_pat . ' ');

        echo ($post->dob);

        foreach ($query as $row) :
            echo ($row->zkf_pat . ' ' );
            echo "titre de l'opération: " . ($row->titre);
        endforeach;
        ?>
    </div>
</code>

Thanks for your advices and help !

  • 写回答

1条回答 默认 最新

  • duanji8887 2015-08-04 07:19
    关注

    I asked for help on the BF forum too, and somebody gave me the solution=>hopefully this will help somebody else. The issue was with the $query , which is a DB_result object. http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

    to solve the problem i had to doo: $query->result()

    Final code:

    Controller:

    public function detail ($id = null){
        $this->load->helper('typography');
    
    
        $this->db->from ('operation as ope');
        $this->db->join ('patient',
                        "patient.zkp_pat = ope.zkf_pat " );
        $this->db->where ('patient.zkp_pat', $id);
        $query = $this->db->get();
    
        $post = $this->post_model->find($id);
    
        Template::set('query', $query);
        Template::set('post', $post);
    
    
        Template::set_view('detail');
        Template::render()        
    }
    

    And view file:

    <div class="post">
        <h2><?php e($post->nom); ?></h2>
    <?php 
    echo ($post->nom . ' '); echo ($post->prenom . ' '); 
    echo($post->zkp_pat . ' ');
    echo ($post->dob);
    
    foreach ($query->result() as $row) :
        echo '<div>'
        echo " 
    "; 
        echo ($row->zkf_pat . '  ');
        echo "titre de l'opération: " . ($row->titre);
    ?>
        </div>
    <?php 
        endforeach;
    ?>
    
    </div>
    

    $query->get()->result() returns an object with all fetched data, whereas $query->get()->row()returns only a single result row, if the result has one row, it returns only the first one. The result is given as an object...

    If you like my edit, please upvote, that would be very helpful !

    评论

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'