将单个或多个 URI 段传递给函数(代码点火器)

发布于 2024-12-27 11:09:00 字数 776 浏览 0 评论 0原文

目前我有这个网址来查看数据库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2025-01-03 11:09:00

在您的控制器中使用它

function view() {
    $id = $this->uri->segment(1);
    $id_array = explode(",", $id);
    $this->load->model('Site_model');
    foreach ($id_array as $key => $id) {
    // use alphaID function
    $id = alphaID($id ,true);
    if($query = $this->Site_model->get_images($id)) {
        $data['records_array'][$key] = $query;
    // added second array for comparison in view
        $data['id_array'][$key] = $id;
    } 
    }  
    $this->load->view('view', $data);
}

以供您查看:

<?php 
foreach ($records_array as $key => $records) {
if(isset($records)) : foreach($records as $row) : ?>
    // removed uri and added array
    <?php if($row->alpha_id == $id_array[$key]):    ?>
        <h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
    <?php endif; ?>
    <?php endforeach; ?>
<?php endif; 
}
?>

Use this in your controller

function view() {
    $id = $this->uri->segment(1);
    $id_array = explode(",", $id);
    $this->load->model('Site_model');
    foreach ($id_array as $key => $id) {
    // use alphaID function
    $id = alphaID($id ,true);
    if($query = $this->Site_model->get_images($id)) {
        $data['records_array'][$key] = $query;
    // added second array for comparison in view
        $data['id_array'][$key] = $id;
    } 
    }  
    $this->load->view('view', $data);
}

For your view:

<?php 
foreach ($records_array as $key => $records) {
if(isset($records)) : foreach($records as $row) : ?>
    // removed uri and added array
    <?php if($row->alpha_id == $id_array[$key]):    ?>
        <h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
    <?php endif; ?>
    <?php endforeach; ?>
<?php endif; 
}
?>
难以启齿的温柔 2025-01-03 11:09:00

因为逗号不是有效的路径元素,如果没有将分隔数据放在 ? 的右侧,您将无法使其正常工作。特点。您需要提出另一个方案或遵循@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.

电影里的梦 2025-01-03 11:09:00

你是对的,你可以在 $config['permissed_uri_chars'] 中添加一个逗号,但每次需要时都必须对该段进行操作,除非你挂接到系统核心。

尚未测试此代码,但您会得到一个想法:

<?php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-,'; // Note a comma...

// Controller
class Blog extends CI_Controller
{

    public function posts($ids = NULL)
    {
        // Check if $ids is passed and contains a comma in the string
        if ($ids !== NULL AND strpos($ids, ',') !== FALSE)
        {
            $ids = explode(',', $ids);
        }

        // Convert $ids to array if it has no multiple ids
        is_array($ids) OR $ids = array($ids);

        // $ids is an array now...

    }

    public function new_posts()
    {
        // Check if $ids is passed and contains a comma in the string
        $ids = $this->uri->segment(1);
        if (!empty($ids) AND strpos($ids, ',') !== FALSE)
        {
            $ids = explode(',', $ids);
        }

        // Convert $ids to array if it has no multiple ids
        is_array($ids) OR $ids = array($ids);

        // $ids is an array now...

    }

}

?>

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:

<?php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-,'; // Note a comma...

// Controller
class Blog extends CI_Controller
{

    public function posts($ids = NULL)
    {
        // Check if $ids is passed and contains a comma in the string
        if ($ids !== NULL AND strpos($ids, ',') !== FALSE)
        {
            $ids = explode(',', $ids);
        }

        // Convert $ids to array if it has no multiple ids
        is_array($ids) OR $ids = array($ids);

        // $ids is an array now...

    }

    public function new_posts()
    {
        // Check if $ids is passed and contains a comma in the string
        $ids = $this->uri->segment(1);
        if (!empty($ids) AND strpos($ids, ',') !== FALSE)
        {
            $ids = explode(',', $ids);
        }

        // Convert $ids to array if it has no multiple ids
        is_array($ids) OR $ids = array($ids);

        // $ids is an array now...

    }

}

?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文