Welcome Guest, Not a member yet? Register   Sign In
undefined variable
#1

(This post was last modified: 03-25-2017, 07:13 PM by ciadmin. Edit Reason: Added [code] tags for TL;DR )

i am actually make a basic crud website,i am a beginner in codeigniter.i am following a youtube channel.and copying his exact code and still facing a problem.
my model is
Code:
<?php
class Post_model extends CI_Model{
    public function __construct(){
$this->load->database();

    }
public function get_posts($slug=false){
if($slug===false){
    $this->db->order_by('id','DESC');

    $query=$this->db->get('posts');
    return $query->result_array();

}
$query=$this->db->get_where('posts',array('slug'=>$slug));
    return $query->row_array();
}
public function create_post(){
    $slug=url_title($this->input->post('title'));
    $data=array('title'=>$this->input->post('title'),
        'slug'=>$slug,
        'body'=>$this->input->post('body')
        );
    return $this->db->insert('posts',$data);
}

public function delete_post($id){

    $this->db->where('id',$id);
    $this->db->delete('posts');
    return true;
}

public function update_post()
{
    $slug=url_title($this->input->post('title'));
    $data=array('title'=>$this->input->post('title'),
        'slug'=>$slug,
        'body'=>$this->input->post('body')
        );    $this->db->where('id',$this->input->post('id'));
    return $this->db->update('posts',$data);

    

}


}
my controller is

<?php
class Posts extends CI_Controller{
    public function index(){
        $data['title']='Latest Posts';
        $data['posts']=$this->Post_model->get_posts();




        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }
    public function view($slug=NULL)
    {
        $data['post']=$this->Post_model->get_posts($slug);
        if(empty($data['post'])){
            show_404();
        }
        $data['title']=$data['post']['title'];
        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }

    public function create(){
        $data['title']='create post';
        $this->form_validation->set_rules('title','Title','required');
        $this->form_validation->set_rules('body','body','required');
        if($this->form_validation->run()===false){$this->load->view('templates/header');
        $this->load->view('posts/create',$data);
        $this->load->view('templates/footer');


        }
        else{
            $this->Post_model->create_post();

redirect('posts');
}

        
        }
        public function delete($id)
        {
            $this->Post_model->delete_post($id);
            redirect('posts');
        }



        
    public function edit($id)
    {

        $data['post']=$this->Post_model->get_posts($slug);
        if(empty($data['post'])){
            show_404();
        }
        $data['title']='edit post';
        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }

    public function update()
    {
         $this->Post_model->update_post();
         redirect('posts');
}
}
my view for showing blog post is
<h2><?=$title?></h2>
<?php foreach($posts as $post):?>
    <h3><?php echo $post['title'];?></h3>o
    <small class="post-date">Posted on:<?php echo $post['created at'];?></small><br>
    <?php echo word_limiter($post['body'],60);?>
    <p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
    
<?php endforeach;?>

error it shows when i click on read more is '$posts variable is not defined' without $posts i can't loop through the blog posts of database.plz help me out how to solve this problem.if you won't to have a look at my other files i will be glad to show.

Message: Undefined variable: posts
Filename: posts/index.php
Line Number: 2
Backtrace:
File: /opt/lampp/htdocs/codeigniter/application/views/posts/index.php
Line: 2
Function: _error_handler
File: /opt/lampp/htdocs/codeigniter/application/controllers/Posts.php
Line: 23
Function: view
File: /opt/lampp/htdocs/codeigniter/index.php
Line: 315
Function: require_once
Reply


Messages In This Thread
undefined variable - by anmol - 03-25-2017, 12:39 PM
RE: undefined variable - by Wouter60 - 03-25-2017, 11:25 PM
RE: undefined variable - by anmol - 03-25-2017, 11:35 PM
RE: undefined variable - by Wouter60 - 03-25-2017, 11:49 PM
RE: undefined variable - by anmol - 03-25-2017, 11:56 PM
RE: undefined variable - by anmol - 03-25-2017, 11:58 PM
RE: undefined variable - by Wouter60 - 03-26-2017, 03:11 AM
RE: undefined variable - by ChicagoPhil - 03-26-2017, 01:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB