传递单个或多个URI段以实现功能(代码点火器)

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; ?>

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; 
}
?>

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.

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.