将图像 ID 传递给网址段


Pass Image ID to URL segment

我正在尝试单击图像上的下一步按钮以从从数据库中获取的结果中加载下一张图像,但加载一个新页面,因此引导轮播无法按我的需要工作。

假设我有一个包含 3 张图片的相册。这些图像的图像 ID 是 - 25、34、36。当我查看包含所有图像的相册时,我可以单击图像ID 34。这将带我 www.domain.com/image/34 当我单击下一步时,我想重定向到 www.domain.com/image/36 然后再次单击下一步将重定向到 www.domain.com/image/25

所以这是我到目前为止所拥有的:

控制器:

function index($image_id)
{
    // get album data                       
    $this->data['image_data'] = $this->album_model->get_album_image_data($image_id);
    $album_id = $this->data['image_data']->album_id;
    $this->data['album_images']  = $this->album_model->get_all_images($album_id);
    $data = $this->data;           
    $this->_render_page('images/image', $data);
}

视图:

foreach($album_images as $img)
{
    if($img->image_id == $this->uri->segment(2))
    {
        echo '<img src="'.$img->image_url.'" class="img-responsive img-panel" class="img-responsive" />';
    }  
}
<!-- Image nav -->
<a class="fui-arrow-left left" href="#"></a>
<a class="fui-arrow-right right" href="#"></a>

如何获得下一个和上一个image_id?

如果我需要包含更多信息,请告诉我。

可以使用

分页,先加载分页库。$this->load->library('pagination');请参阅 https://codeigniter.com/user_guide/libraries/pagination.html?highlight=pagination

控制器

public function index()
{
   $config['base_url'] = site_url('dashboard-clients');
   $config['total_rows'] = //total count of your image or data
   $config['per_page'] = 1;
  //show the number of pages
  $choice = $config['total_rows']/$config['per_page'];
  $config['num_link'] = round($choice);
  $this->pagination->initialize($config);
  $page = ($this->uri->segment(2)) ? $this->uri->segment(2) :0;
  $result = $this->Your_Model->get('table name',$config['per_page'], $page)
  $str_links = $this->pagination->cretae_links();
  $data['links'] = explode('&nbsp', $str_links);
  if($result != FALSE)
  {
    $data['data'] = $result;
    $this->load->view('yoyr view file', $data);
  }
}

public function get($table, $limit, $start)
{
  $this->db->limit($limit, $start);
  $query = $this->db->get($table);
  if($query->num_rows() > 0)
  {
    return $query->reult();
  }
  else
    return FALSE;
}

视图

foreach($data as $img)
{
     echo '<img src="'.$img->image_url.'" class="img-responsive img-panel" class="img-responsive" />'; 
}
//create links like 1 ,2, 3,next
<div id="pagination" align="center">
     <ul class="pagination">
         <?php if (isset($links) and is_array($links)) {
              foreach ($links as $link) {
                  echo '<li>'.$link.'</li>';
               }
           }?>
     </ul>
</div>

感谢您的所有建议,但我实际上找到了一种更简单、更干净的方法。

找到了这个函数,我用它创建了一个数组助手,它帮助我将图像 ID 存储在数组中。

一旦我能够根据需要将我的 id 存储在数组中,我就将其添加到我的视图中:

$index = array_search($this->uri->segment(2), $image_ids);                        
$next = (count($image_ids) == $index+1) ? $image_ids[0] : $image_ids[$index+1];                        
$prev = ($index == 0) ? $image_ids[count($image_ids)-1] : $image_ids[$index-1]; 
<a class="fui-arrow-left left" href="<?=$prev?>"></a>
<a class="fui-arrow-right right" href="<?=$next?>"></a>