Welcome Guest, Not a member yet? Register   Sign In
set_value() default value issues
#1

[eluser]megabyte[/eluser]
I have a form, and the same view does adds and edits so I'm using this format for my code in the form


Code:
$course_name = array('name' => 'course_name',
            'id' => 'course_name',
            'class' => 'text long',
            'value' => set_value('course_name', $row->course_name));
echo form_input($course_name);

//And in the controller

function add()
{
  $data['row'] = array();
  $this->load->view($this->view.'edit', $data);      
}
    
        
function edit()
{
  $query = $this->db->get_where($this->tbl, array($this->primary_key => $this->id));
  $data['row'] = $query->row();
  $this->load->view($this->view.'edit', $data);    
}

notice the empty
Code:
$data['row'] = array();


in the add method. I thought this would suppress any erros but im getting
Code:
A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Someone tell me what im doing wrong?
#2

[eluser]umefarooq[/eluser]
in add $row is not an object, just a simple variable where as in edit its a database object which has course_name as object variable. in case of add leave the set value blank, where as in edit you can use it
Code:
$course_name = array('name' => 'course_name',
            'id' => 'course_name',
            'class' => 'text long',
            'value' => (is_object($row)?set_value('course_name', $row->course_name):''));
  
or use object

$data['row'] = (object) array('course_name'=>'');

hope it will work fine.




Theme © iAndrew 2016 - Forum software by © MyBB