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
#2

If you take a good look at the error message, you will see that the problem lies in the view method of your controller.
There, you define the $data['post'] variable (post is single here), which is passed to the view posts/index. That's why the posts/index view says the posts variable (prural) has not been defined.
You are using the same view for listing all posts and for showing the details of one individual post. Using two different views makes more sense, I guess.
Reply
#3

(This post was last modified: 03-25-2017, 11:47 PM by anmol.)

no that's is not the way i was doing it.i am using index view, for all the posts and route " '/posts/' .$post['slug']);"for individual blog posts.on clicking read more, it redirects to the individual blog post, i suppose,tho i am beginner but i am assuming that.my index view is working fine but when i click on read more shows it the error, means $route['posts/(:any)']='posts/view/$1'; this is the route that does not work.my view for individual blog post is.
"<h2><?php echo $post['title'];?></h2>
<small class="post-date">postedon:<?php echo $post['created_at'];?></small><br>
<div class="post-body">
<?php echo $post['body'];?>
</div>
<hr>
<a class="btn btn-default pull-left" href="posts/edit/<?php echo $post['id'];?>">edit</a>
<?php echo form_open('/posts/delete/'.$post['id']);?><form>
<input type="submit" vlaue="delete" class="btn btn-danger">
</form>"

my route file is
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['posts/create']='posts/create';
$route['posts/update']='posts/update';
$route['posts/(:any)']='posts/view/$1';
$route['posts']='posts/index';

$route['default_controller'] = 'pages/view';
$route['(:any)']='pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
my doubt is that if it was singular $post why is it listing all the posts in the index view.and showing error only when i click on read more to see the individual blog post.if it lists all the blog post that somehow means that foreach loop is running and listing all the blog posts from the database.i have worked in laravel for a long time,and in that we create objects plural when we want all and singular when we need individual,now i for some reason have to move to codeigniter and i am feeling lost,confused here.
Reply
#4

The view method in your controller:
PHP Code:
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');
 
    

It clearly says it's loading the posts/index view!!!!!
Reply
#5

and moreover $posts have been defined in the index function
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');
Reply
#6

(This post was last modified: 03-26-2017, 12:00 AM by anmol.)

sorry i had changed that after writing the question here.but i had changed that to
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');
}
Reply
#7

So is it working now? Or do you get a new error message?
Reply
#8

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






The problem lies in the bold. You are loading the same view file for both method calls. For the index method your variable is posts and for your view method your variable is post
Reply




Theme © iAndrew 2016 - Forum software by © MyBB