dss67853 2018-05-31 14:57
浏览 72
已采纳

Codeigniter和Bootstrap滑块,我如何循环和加载幻灯片

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.

  • 写回答

1条回答 默认 最新

  • dongpa2000 2018-05-31 16:10
    关注

    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'];
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器