将单个或多个 URI 段传递给函数(代码点火器)
目前我有这个网址来查看数据库(codeigniter)domain.com/view/id
中的图像
我希望能够接受多个以逗号分隔的IDdomain.com/view /id,id,id
知道如何去做吗?感谢
查看控制器部分:
function view() {
$id = alphaID($this->uri->segment(1) ,true);
$this->load->model('Site_model');
if($query = $this->Site_model->get_images($id)) {
$data['records'] = $query;
}
$this->load->view('view', $data);
}
<?php if(isset($records)) : foreach($records as $row) : ?>
<?php if($row->alpha_id == $this->uri->segment(1)): ?>
<h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Currently I have this url to view an image from the db (codeigniter) domain.com/view/id
I'd like to be able to accept multiple ids comma separated domain.com/view/id,id,id
Any idea how to go about it? Thanks
view controller part:
function view() {
$id = alphaID($this->uri->segment(1) ,true);
$this->load->model('Site_model');
if($query = $this->Site_model->get_images($id)) {
$data['records'] = $query;
}
$this->load->view('view', $data);
}
<?php if(isset($records)) : foreach($records as $row) : ?>
<?php if($row->alpha_id == $this->uri->segment(1)): ?>
<h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的控制器中使用它
以供您查看:
Use this in your controller
For your view:
因为逗号不是有效的路径元素,如果没有将分隔数据放在 ? 的右侧,您将无法使其正常工作。特点。您需要提出另一个方案或遵循@jprofitt 的评论。
Because commas aren't valid path elements you won't be able to get this to work without having your delimited data on the right side of a ? character. You'll need to come up with another scheme or go with the comment from @jprofitt.
你是对的,你可以在 $config['permissed_uri_chars'] 中添加一个逗号,但每次需要时都必须对该段进行操作,除非你挂接到系统核心。
尚未测试此代码,但您会得到一个想法:
example.com/index.php/blog/posts/2,4,6,8
请再次注意,代码可能不准确因为我还没有测试过,但认为它会对你有所帮助。
You are right, you can add a comma in
$config['permitted_uri_chars']
but you'll have to manipulate with that segment every time you need it, unless you hook into the system core.Haven't tested this code but you'll get an idea:
example.com/index.php/blog/posts/2,4,6,8
Please note once again, code may not be accurate because I haven't tested it but think it will help you.