0

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

3
Contributors
3
Replies
21
Views
2 Years
Discussion Span
Last Post by Ventech_IT
0

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>

Edited by mangel.murti: clear code solution

0

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.';
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.