Robert Kibet Robert Kibet - 2 months ago 6
PHP Question

View Events by Category using PHP Code Igniter

I'm a not that advance in PHP, I need some assistance:

I have a table

events
with column name
event_type


I want when I click
business
I would get all events under
business
category. Same goes to
education
...

This is my views, View Image here

This is a code snippet from by views,
business
category.
`

<div class="sell-or-buy">

<div class="icon">
<i class="et-line-briefcase"></i>
</div>

<div class="clear"></div>

<div class="content">

<h3 class="uppercase">Business</h3>

<p>
Business is always Good when you talk of a good setup. Get
the best Meetings right here.
<br>
</p>
<a href="<?php echo base_url('index.php/home/category' . $data['event_type'])?>">View</a>

</div>

</div>

</div>`


I dont know how to configure my controller and my model

.
I am using Code Igniter 3.0 with PHP

Answer

Ok.So what i get from your question is,you had table events with event_type and you want to fetch the event data depending on event clicked/triggered.

From your posted URL,it looks like Home is your controller and category is the function and you are passing event_type to that function

<?php echo base_url('index.php/home/category/' . $data['event_type'])?>

So your controller function will go like this (I hope you are following CI user guide to define controller);

//Controller function
    function category($event_type)
    {

    // pass the received event_type to your model
    $this->load->model('event_model');
    $data['events'] = $this->event_model->get_events($event_type);

    // Pass the data to your views
    $this->load->view('events',$data);
}


// Model code goes like this
function get_events($event_type)
{
  **// result() returns object where as result_array() returns array**
   $query = $this->db->get_where('events',array('event_type'=>$event_type))->result();
   if (count($query) > 0)
   {
      return $query;
   }
   else
      return false;
}

Let me know if any queries.