undefined index

#11
[eluser]swgj19[/eluser]
Just changed and no result. I will post my view, controller,and model for the posts that is working fine. Keep in mind that the methods for comments and posts are almost identical. So maybe by looking at the posts mvc, we can find the answer to the comments issue.

view

Code:
<br />
<h1>&lt;?php echo $title;?&gt;</h1>

&lt;?php
echo form_open('admin/posts/edit');
echo "<p><label for='ptitle'>Title</label><br/>";
$data = array('name'=>'title','id'=>'ptitle','size'=>25, 'value' =>$post['title']);
echo form_input($data) ."</p>";

echo "<p><label for='ptags'>Tags</label><br/>";
$data = array('name'=>'tags','id'=>'ptags','size'=>25, 'value' =>$post['tags']);
echo form_input($data) ."</p>";

echo "<p><label for='category_id'>Category </label><br/>";
echo form_dropdown('category_id', $cats, $post['category_id']) ."</p>";

echo "<p><label for='long'>Content</label><br/>";
$data = array('name'=>'body','id'=>'long','rows'=>5, 'cols'=>'40', 'value' =>$post['body']);
echo form_textarea($data) ."</p>";

echo "<p><label for='status'>Status</label><br/>";
$options = array('draft' => 'draft', 'published' => 'published');
echo form_dropdown('status', $options,  $post['status']) ."</p><br />";

echo form_hidden('id', $post['id']);
echo form_submit('submit', 'update page');
echo form_close();
?&gt;
<br />


Controller:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Posts extends CI_Controller {

// --------------------------------------------------------------------

/**
  * __construct()
  *
  * Constructor PHP 5+ NOTE: Not needed if not setting values!
  *
  * @access public
  * @return void
  */
public function __construct()
{
  parent::__construct();

  $this->load->model('post_model', 'posts');
  $this->load->model('category_model', 'categories');

  if ($this->session->userdata('user_id') < 1)
  {
   redirect('blog/login','refresh');
  }
}

// --------------------------------------------------------------------

/**
  * index()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function index()
{
  $data['title'] = "Manage Posts";
  $data['main'] = 'admin_posts_home';
  $data['posts'] = $this->posts->get_all_posts();
  $data['cats'] = $this->categories->get_top_categories();

  $this->load->vars($data);
  $this->load->view('dashboard');  
}

// --------------------------------------------------------------------

/**
  * create()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function create()
{
  if ($this->input->post('title'))
  {
   $this->posts->add_post();
   $this->session->set_flashdata('message','Post created');

   redirect('admin/posts/index','refresh');
  }
  else
  {
   $data['title'] = "Create Post";
   $data['main'] = 'admin_posts_create';
   $data['cats'] = $this->categories->get_categories_dropdown();

   $this->load->vars($data);
   $this->load->view('dashboard');    
  }
}

// --------------------------------------------------------------------

/**
  * edit()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function edit($id = 0)
{
  if ($this->input->post('title'))
  {
   $this->posts->update_post();
   $this->session->set_flashdata('message', 'Post updated');

   redirect('admin/posts/index', 'refresh');
  }
  else
  {
   $data['title'] = "Edit Post";
   $data['main'] = 'admin_posts_edit';
   $data['post'] = $this->posts->get_post($id);
   $data['cats'] = $this->categories->get_categories_dropdown();

   $this->load->vars($data);
   $this->load->view('dashboard');    
  }
}

// --------------------------------------------------------------------

/**
  * delete()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function delete($id = 0)
{
  $this->posts->delete_post($id);
  $this->session->set_flashdata('message', 'Post deleted');

  redirect('admin/posts/index', 'refresh');
}

}


// ------------------------------------------------------------------------
/* End of file admin_model.php */
/* Location: ./application/models/admin_model.php */

#12
[eluser]swgj19[/eluser]
Model:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post_model extends CI_Model {

// --------------------------------------------------------------------

/**
  * __construct()
  *
  * Constructor PHP 5+ NOTE: Not needed if not setting values!
  *
  * @access public
  * @return void
  */
public function __construct()
{
  parent::__construct();
}

// --------------------------------------------------------------------

/**
  * get_post()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function get_post($id)
{
  $data = array();

  $this->db->where('id', $id);
  $this->db->limit(1);

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

  if ($query->num_rows() > 0)
  {
   $data = $query->row_array();
  }

  $query->free_result();    

  return $data;    
}

// --------------------------------------------------------------------

/**
  * get_all_posts()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function get_all_posts()
{
  $data = array();

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

  if ($query->num_rows() > 0)
  {
   foreach ($query->result_array() as $row)
   {
    $data[] = $row;
   }
  }

  $query->free_result();  

  return $data;
}

// --------------------------------------------------------------------

/**
  * get_all_posts_category()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function get_all_posts_category($cat_id)
{
  $data = array();

  $this->db->where("category_id", $cat_id);
  $this->db->where('status', 'published');

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

  if ($query->num_rows() > 0)
  {
   foreach ($query->result_array() as $row)
   {
    $data[] = $row;
   }
  }

  $query->free_result();  

  return $data;
}

// --------------------------------------------------------------------

/**
  * get_live_posts()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function get_live_posts($limit)
{
  $data = array();

  $this->db->limit($limit);

  $this->db->where('status', 'published');
  $this->db->order_by('pub_date', 'desc');

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

  if ($query->num_rows() > 0)
  {
   foreach ($query->result_array() as $row)
   {
    $data[] = $row;
   }
  }

  $query->free_result();  

  return $data;
}

// --------------------------------------------------------------------

/**
  * add_post()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function add_post()
{
  $now = date("Y-m-d H:i:s");

  $data = array(
   'title'   => $this->input->post('title'),
   'tags'   => $this->input->post('tags'),
   'status'  => $this->input->post('status'),
   'body'   => $this->input->post('body'),
   'category_id' => $this->input->post('category_id'),
   'user_id'  => $this->session->userdata('user_id'),
   'pub_date'  => $now,
  );

  $this->db->insert('posts', $data);  
}

// --------------------------------------------------------------------

/**
  * update_post()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function update_post()
{
  $data = array(
   'title'   => $this->input->post('title'),
   'tags'   => $this->input->post('tags'),
   'status'  => $this->input->post('status'),
   'body'   => $this->input->post('body'),
   'category_id' => $this->input->post('category_id'),
   'user_id'  => $this->session->userdata('user_id'),
  );

  $this->db->where('id', $this->input->post('id'));
  $this->db->update('posts', $data);
}

// --------------------------------------------------------------------

/**
  * delete_post()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function delete_post($id)
{
  $this->db->where('id', $id);
  $this->db->delete('posts');
  
  // this will delete - remark out or delete the top 2 lines
  $this->db->delete('posts', array('id' => $id));
}

}


// ------------------------------------------------------------------------
/* End of file post_model.php */
/* Location: ./application/models/post_model.php */

#13
[eluser]ppwalks[/eluser]
If your array is empty then that would be the first place i would look for the fault, run some tests on every level, model controller then view

#14
[eluser]PhilTem[/eluser]
Please use http://gist.github.com to post your looooooong codes (it's much nicer to read ever since the forums have changed styles and you can edit it online avoiding to post the code over and over again)

To answer your question:

[quote author="swgj19" date="1354020979"]I just used print_r ($comment);
The result is Array()

I also used var_dump ($comment);
The result is array(0) { } [/quote]

Do you know what this means? The item you want to access is empty hence there is no key named 'name' which means in return that your database query returns an empty set. Either because there's an error in one of these many lines OR because there is no matching item in the database.

#15
[eluser]ppwalks[/eluser]
Nicely put...

#16
[eluser]swgj19[/eluser]
@ppwalks
Thanks for the help. I know the db is correct. Something in the code which I will need to further debug.

@PhilTem
Thanks for the clarification. I will debug some more and post the solution when I find it.

#17
[eluser]swgj19[/eluser]
I solved my problem, but somehow created another one.

On my blog, where the comments are displayed, I was using the same function to display user's corresponding post_id comments and user's corresponding comments by id in the admin panel.

So I had to write a new function for the back-end.

However, the main problem was I had to change this

Code:
public function get_comments($post_id = null)
{
  
  $data = array();

  $this->db->where('post_id', $post_id);

  $query = $this->db->get('comments');

  if ($query->num_rows() > 0)
  {
   foreach ($query->result_array() as $row)
   {
    $data[] = $row;
   }
  }

  $query->free_result();  

  return $data;
}

to

Code:
public function get_comments($id)
{
  
  $data = array();

  $this->db->where('id', $id);

  $query = $this->db->get('comments');

  if ($query->num_rows() > 0)
  {
   $data = $query->row_array();
  }

  $query->free_result();  

  return $data;
}

Instead of using result_array(), I needed to use row_away.
I will have to look in the ci_manual to answer why.

Now the other problem that was created, is that in the admin panel, the comments are displayed in a table with id, name, email, body, post_id, status, actions.

Now when a comment is inserted on the blog home page, the comment is displayed and stored in the db.
However, when I update a comment in the admin panel (changing status enum from active to inactive or any change at all) the post_id will dissappear and default to 0, which causes the comment on the home blog page to dissappear. The reason why, is because in the function that displays the comments, it is by post_id. So somehow in one of my function I need to pass the $post_id, but I have messed with this for 4 hours and do not want to break my code.

Any suggestioins. Thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.