Hi guys,

Could someone please help me with a small problem i am having?

I want to display a message if there are no entry's in a table but for some reason i cant get this right.

Here is my Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Landing extends CI_Controller{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Ksm_model');
    }

    public function index(){
            $data['title'] = "Welcome";
            $data['main_content'] = 'home';
            $data['events'] = $this->Ksm_model->get_events();
            $this->load->view('templates/template', $data);
    }
and here is my Model:
    public function get_events(){
        $query = $this->db->get('events',1, 1);
        return $query->result_array();
        }
}
and finally the part in my view i want the message to display in:
                    <div class="panel-body" id="attractions">
                        <?php foreach ($events as $event_item): ?>
                            <?php echo "<p style='font-weight: bold; color: #555555;'>".$event_item['eventName']."</p>"; ?>
                            <?php echo "<p>".$event_item['eventInfo']."</p>"; ?>
                            <?php echo "<p style='color: #555555; font-weight: bold;'>"."Event Date: ".$event_item['eventDate']."</p>"; ?>
                        <?php endforeach; ?>
                    </div>

Please advise me on how to solve this problem?

thanks in advance

Riaan

Recommended Answers

All 3 Replies

As in your view you can just put condition. hope this helps you.

     <div class="panel-body" id="attractions">
         <?php if(count($events) > 0){?>
           <?php foreach ($events as $event_item): ?>
           <?php echo "<p style='font-weight: bold; color: #555555;'>".$event_item['eventName']."</p>"; ?>
           <?php echo "<p>".$event_item['eventInfo']."</p>"; ?>
           <?php echo "<p style='color: #555555; font-weight: bold;'>"."Event Date: ".$event_item['eventDate']."</p>"; ?>
       <?php endforeach; ?>
       <?php }else{?>
         No records
       <?php }?>
     </div>

The method result_array() will return an array, or an empty array if the query does not return any results, so:

if(count($events) > 0)
{
    foreach($events as $event_item)
    {
        # ... code here ...
    }
}

else
{
    echo 'No items.';
}

Thanks i knew it had to be something simple...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.