0
votes

I am a beginner in codeigniter and I am making a basic crud for learning. I am facing an error which is in index.php

<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;?>

A PHP Error was encountered
Severity: Notice
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

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/view', $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 model file is

<?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);

    }

}
1
wait sorry i was busy uploading the code.look at it again.it has something to do with the $posts variable that is been used in foreach loop. - anku
You auto-load your model ? - John
You haven't loaded your model in the controller so we are assuming you have it in autoload.php in your configs. Please dump the data array by print_r() or var_dump before rendering views to see what you are getting from your model - Malik Mudassar
yes,i do.$autoload['model'] = array('Post_model'); - anku
my $post[body] and all the table values are loading perfectly.model is returning values of title and and body fine.but $posts is not defined - anku

1 Answers

0
votes

On your edit function on controller you have edit($id)

But where you load your model you have get_posts($slug) it should be get_posts($id)

And also change $data['post'] to $data['posts']

public function edit($id)  {

    $data['posts'] = array();

    // rename slug to id
    $data['posts'] = $this->post_model->get_posts($id);

    if(empty($data['posts'])){
        show_404();
    }

    $data['title'] = 'edit post';

    $this->load->view('templates/header');

    /*
      Tip You don't have to use index.php on for view you could create a
      another view instead $this->load->view('posts/edit_view',$data);
    */

    $this->load->view('posts/index',$data); 
    $this->load->view('templates/footer');
}

On the when load model on controller

Change

$this->Post_model->get_posts($id);

To

$this->post_model->get_posts($id);

https://www.codeigniter.com/user_guide/general/models.html#loading-a-model

On Autoload.php

Change

$autoload['model'] = array('Post_model');

To

$autoload['model'] = array('post_model');

And load database in autoload.php make life easier

$autoload['libraries'] = array('database');