CodeIgniter Forums
undefined index - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: undefined index (/showthread.php?tid=56107)

Pages: 1 2


undefined index - El Forum - 11-27-2012

[eluser]swgj19[/eluser]
I am getting the error. I am not sure why. Because my categories model and post model is working just fine with similar functions. I know this is not a db error as I have already tested.

Error:

A PHP Error was encountered
Severity: Notice

Message: Undefined index: name

Filename: views/admin_comments_edit.php

Line Number: 9

This error is happening for every name of field in my form.

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 Comments 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('comment_model', 'comments');

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

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

/**
  * index()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function index()
{
  $data['title']  = "Manage Comments";
  $data['main']  = 'admin_comments_home';
  $data['comments'] = $this->comments->get_all_comments();

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

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

/**
  * create()
  *
  * Description:
  *
  * @access public
  * @return void
  */  
public function create()
{

  if ($this->input->post('name'))
  {
   $this->comments->add_comment();
   $this->session->set_flashdata('message', 'Comment created');

   redirect('admin/comments/index', 'refresh');
  }
  else
  {
   $data['title']  = "Create Comment";
   $data['main']  = 'admin_comments_create';
   $data['comments'] = $this->comments->get_comments();

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

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

/**
  * edit()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
function edit($id=0){
   if ($this->input->post('name')){
    $this->comments->update_comment();
    $this->session->set_flashdata('message','Comment updated');
    redirect('admin/comments/index','refresh');
   }else{
  
  //$id = $this->uri->segment(4);
  $data['title'] = "Edit Comment";
  $data['main'] = 'admin_comments_edit';
  $data['comment'] = $this->comments->get_comments($id);
  $data['comments'] = $this->comments->get_all_comments();
  $this->load->vars($data);
  $this->load->view('dashboard');    
}
  }  


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

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

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

}


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



undefined index - El Forum - 11-27-2012

[eluser]swgj19[/eluser]
Model:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment_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_comments()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
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;
}

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

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

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

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

  $query->free_result();  

  return $data;
}

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

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

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

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

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

  $query->free_result();  

  return $data;
}

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

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

  $data = array(
   'name'  => $this->input->post('name'),
   'email'  => $this->input->post('email'),
   'body'  => strip_tags(substr($this->input->post('body'), 0, 255)),
   'post_id' => $this->input->post('post_id'),
   'pub_date' => $now,
  
  );

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

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

/**
  * update_comment()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function update_comment()
{
  $data = array(
   'name'  => $this->input->post('name'),
   'email'  => $this->input->post('email'),
   'body'  => $this->input->post('body'),
   'post_id' => $this->input->post('post_id'),
   'pub_date' => $this->input->post('pub_date'),
   'status' => $this->input->post('status'),
  );

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

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

/**
  * delete_comment()
  *
  * Description:
  *
  * @access public
  * @param string
  * @return void
  */
public function delete_comment($id)
{
  $this->db->where('id', $id);
  $this->db->delete('comments');

  // this will delete - remark out or delete the top 2 lines
  $this->db->delete('comments', array('id' => $id));
}

}


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



undefined index - El Forum - 11-27-2012

[eluser]ppwalks[/eluser]
Have you auto-loaded the form helper because it is not included in the controller and no field name in your form is called "name".


undefined index - El Forum - 11-27-2012

[eluser]swgj19[/eluser]
Quote:Have you auto-loaded the form helper because it is not included in the controller and no field name in your form is called “name”.

Yes, the form helper is auto-loaded in my config/autoload.php

I changed my from field names to:

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

&lt;?php

$data = array();
echo form_open('admin/comments/edit');
echo "<p><label for='name'>Name</label><br/>";
$data = array('name'=>'name','id'=>'name','size'=>25, 'value' => $comment['name']);
echo form_input($data) ."</p>";

echo "<p><label for='email'>email</label><br/>";
$data = array('name'=>'email','id'=>'email','size'=>40, 'value' => $comment['email']);
echo form_input($data) ."</p>";

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

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

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

I still have the same errors.


undefined index - El Forum - 11-27-2012

[eluser]ppwalks[/eluser]
is this for your "edit form", you have $comment['name'] and in your controller its called "comments", this will throw an error and also if it is an edit form how is the array being sent to the form is it an sdobject array if so you would write $comments->name;


undefined index - El Forum - 11-27-2012

[eluser]ppwalks[/eluser]
print your array using print_r function


undefined index - El Forum - 11-27-2012

[eluser]swgj19[/eluser]
Yes, it is admin_comments_edit.php form.
I changed to $comments->name and I get error saying trying to get property of non-object.
So I changed back the way I had it.
I also did echo $comments in the form and it just says "Array".


undefined index - El Forum - 11-27-2012

[eluser]ppwalks[/eluser]
use print_r thats what it is for, show the array through print_r not echo


undefined index - El Forum - 11-27-2012

[eluser]swgj19[/eluser]
I just used print_r ($comment);
The result is Array()

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


undefined index - El Forum - 11-27-2012

[eluser]ppwalks[/eluser]
Remove function edit($id=0){ and change it to edit($id){