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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog