Inconsistent Setting Variables CakePHP -
so have following in controller:
$employee_options = array( 'conditions' => array('employee.id' => $this->employee_id), 'recursive' => 4, ); $employees = $this->employee->find('all', $employee_options); $this->set('employees', $employees); $initial_dept_id = $this->employee->field('department_id', array('id' => $this->employee_id)); first had this
$index_chosen = $this->employee->field('index_chosen_section', array('id' => $this->employee_id)); $this->set('initial_dept_id', $initial_dept_id); $this->set('$index_chosen', $index_chosen); then changed $index_chosen this, when couldn't work:
$index_chosen = $employees[0]; $this->set('initial_dept_id', $initial_dept_id); $this->set('$index_chosen', $index_chosen); here view:
<pre> <?php print_r($employees[0]) ?> </pre> <pre> <?php print_r($index_chosen); ?> </pre> both approaches(in controller) resulted in $index_chosen show nothing. in second approach, printing same thing, first shows. ideas on why happening? thanks
don't use $ in first half of ->set():
$this->set('$index_chosen', $index_chosen); // <-- no $this->set('index_chosen', $index_chosen); // <-- yes in past, believe cake auto camelcase variables, depending version you're using, after making above change remove $, if $index_chosen variable still not showing in view, try $indexchosen instead (which recommended case variables anyway).
$indexchosen = $employees[0]; $this->set('indexchosen', $indexchosen); // <-- better side note:
when setting multiple variables, instead of putting each on it's own line, it's common practice use php's compact():
$this->set(compact('initial_dept_id', 'index_chosen'));
Comments
Post a Comment