I am trying to make a bootstrap slider with CodeIgniter, the slider images should be pulled from the database.
each post has a unique slider, so I have 2 database tables, one for the posts and the other for the slides.
the issue is I can't loop through the slides table to pull out the slides that assigned to the post.
I did a join tables between the posts and the slides table so I can get the associated slides, but the result is only loading one slide.
Model:
public function get_yachts($slug = FALSE){
if ($slug === FALSE) {
$query = $this->db->get('yachts');
return $query->result_array();
}
$this->db->join('yacht_slider', 'yacht_slider.yacht_slide_id = yachts.id');
$query = $this->db->get_where('yachts', array('slug' => $slug));
return $query->row_array();
}
Controller:
public function view($slug = NULL){
$data['yacht'] = $this->yacht_model->get_yachts($slug);
$data['title'] = 'Yachts';
$this->load->view('templates/header', $data);
$this->load->view('yachts/view', $data);
$this->load->view('templates/footer');
}
}
In my HTML code, when I try to load the slides like this <?php echo $yacht['slide'];?>
I only get one row from slides table, so how can I loop to get the rest of the slides. I tried foreach loop and other methods, but none worked.
Hope this will help you :
Your model get_yachts
should be like this :
public function get_yachts($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('yachts');
return $query->result_array();
}
else
{
/*$this->db->select('yachts.title as ytitle');*/
$this->db->join('yacht_slider', 'yacht_slider.yacht_slide_id = yachts.id');
$query = $this->db->get_where('yachts', array('slug' => $slug));
return $query->result_array();
}
}
Controller will remain the same.
In view access $yacht
like this:
this will work when slug is either empty or not empty
<?php
if (! empty($yacht))
{
foreach ($yacht as $key => $item) {
echo $item['slide'];
}
}
?>