I am trying to display a drop down menu pulling entries from the database but if there is no Rows if throws PHP error?
Can someone please help me figure out have to return a separate result if no rows exist? I need it to display a link to a form if no menu items exist.
Thanks in advance.
[php]
Model:
<?php class Menu_model extends CI_Model { function menuStones(){ // Parent Menu $cid = $this->session->userdata('cid'); $this->db->select('id, name'); $this->db->where('cid', '15'); $this->db->where('active', '1'); $this->db->order_by('id', 'asc'); $query = $this->db->get('stone_types'); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $data[]=$row; } return $data; }else{ return false; } } } ?>[/php]
Controller:
[php]
[/php]
View:
[php]
-
<?php if ($data = false): ?>
<li><a href="<?php echo $this->load->view('add-stone'); ?>">Add Stone Type</a></li>
<?php else: ?>
<?php foreach ($row as $r):?>
<li><a href="<?php echo site_url($r->id); ?>"><?php echo $r->name; ?></a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</li>
[/php]