CodeIgniter Forums
set_value() default value issues - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: set_value() default value issues (/thread-20904.html)



set_value() default value issues - El Forum - 07-24-2009

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


set_value() default value issues - El Forum - 07-25-2009

[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.