mysql - Why do I have an undefined variable in my template -
i have following action within controller. when go corresponding view, following php error.
public function viewallpolice($id=null) { $customer = $this->session->read('customer_id'); if ($customer>0) { $this->loadmodel('policy'); $results = $this->policy->find( 'all', array('conditions' => array('customer_id' => $customer)) ); $this->set('results', $this->policy->read(null, $id)); } else { $this->session->setflash('error'); } }
view police code:
<?php foreach ($user $result) ?> <p>customer name /mobile : <?php echo $result['policy']['customer_name']; ?></p>
error:
notice (8): undefined variable: user [app\view\user\viewallpolice.ctp, line 1]
warning (2): invalid argument supplied foreach() [app\view\user\viewallpolice.ctp, line 1]
customer name /mobile :
notice (8): undefined variable: result [app\view\user\viewallpolice.ctp, line 2]
i think using cakephp 3 . in controller :
public function viewallpolice($id=null) { $customer = $this->session->read('customer_id'); if ($customer>0) { $this->loadmodel('policy'); $results = $this->policy->find('all',['conditions' =>['customer_id' => $customer]] )->toarray();; $this->set('results', $results); //use pr($result) print result array } else { $this->session->setflash('error'); } }
in template if customer_name field in table policy :
<?php foreach ($results $result): ?> <p>customer name /mobile : <?= $result['customer_name']; ?></p> <?php endforeach; ?>
Comments
Post a Comment