douhao2856 2017-05-05 00:17
浏览 41
已采纳

代码点火器 - 在我的视图中回显我的数组中的变量

Im a designer that i´m taking my first steps on the programing área, mostly on PHP / MySQL. Past week i need to add a blog section on a page made with code igniter.

I make the secction that show all post in order (title and published date). I made a view that takes the id to show the content. But i can`t echo any of the variables.

When i use var_dump or print and it takes all the variables without any problem.

Did i miss somehting? :(

Thank you.

Model:

class blog extends CI_Model { 

    public function get($id){
         $this->db->select('id, title, description, icon, uri, published_at');
         $this->db->from('blogs');
         $this->db->where('id', $id);
         $query = $this->db->get();
         $data  = $query->result();
         return $query->result();
    if(!empty($data)){
    return $this->make_groups($data);
}
}
}

Controller:

Class Pages extends CI_Controller {

    public function blog_detail($id) {

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

        if($data = $this->blog->get($id)) {

            $this->load->model('blog');
            $this->data['blog_menu'] = TRUE;
            $this->data['blog'] = $data;

            $this->data['og'] = array(
                'description' => 'INFO',
                'image' => site_url('assets/images/og/blog.png')
            );

          # layout config

            $this->layout->add_css(site_url('assets/css/blog.css'));
            $this->layout->title_for_layout = "TITLE";
            $this->layout->meta = array(
                array('name' => 'description', 'content' => 'DETAIL'),
                array('name' => 'keywords', 'content' => 'KEYWORDS'),
            );

            $this->layout->render('pages/blog_detail/index', $this->data);

        }
    }
}

View:

<?php var_dump($this->data);
echo $blog->published_at; ?>
  • 写回答

1条回答 默认 最新

  • doukangbin9698 2017-05-05 01:07
    关注

    On your model you are not returning the result

    Check that the filenames and class names only have the first letter upper case.

    Blog_model.php

    class Blog_model extends CI_Model { 
    
        public function get($id) {
            $this->db->select('id, title, description, icon, uri, published_at');
            $this->db->from('blogs');
            $this->db->where('id', $id);
            $query = $this->db->get();
    
            return $query->row_array();
        }
    }
    

    Controller

    controllers > Pages.php

    class Pages extends CI_Controller {
    
       public function __construct() {
           parent::__construct();
           $this->load->model('blog_model');               
       }
    
        public function blog_detail($id) {
    
            $blog_data = $this->blog_model->get($id);
    
            if($blog_data)) {
    
    
                $this->data['blog_menu'] = TRUE;
    
                $this->data['published_at'] = $blog_data['published_at'];
    
                var_dump($blog_data);
    
                $this->data['og'] = array(
                    'description' => 'INFO',
                    'image' => site_url('assets/images/og/blog.png')
                );
    
              # layout config
    
                $this->layout->add_css(site_url('assets/css/blog.css'));
                $this->layout->title_for_layout = "TITLE";
                $this->layout->meta = array(
                    array('name' => 'description', 'content' => 'DETAIL'),
                    array('name' => 'keywords', 'content' => 'KEYWORDS'),
                );
    
                $this->layout->render('pages/blog_detail/index', $this->data);
    
            }
        }
    }
    

    View

    <?php echo $published_at;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号