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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里