【问题标题】:Pass single OR multiple URI segments to function (code igniter)将单个或多个 URI 段传递给函数(代码点火器)
【发布时间】:2012-02-12 06:50:48
【问题描述】:

目前我有这个 url 来查看来自 db (codeigniter) domain.com/view/id的图像

我希望能够接受多个 id 逗号分隔的domain.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; ?>

【问题讨论】:

  • 您可以将其传递为domain.com/view/id/id/id 并获取每个ID 的URI 段吗?
  • id 像逗号分隔或:符号,出于 ocd 原因
  • @user1072983 将 id 作为 views/id/id/id 传递的更好方法...并使用 php 中的可变参数来获取所有 ids
  • 好的,我会试试的。如果我通常使用 $this->uri->segment(2) 获取 id;对于现在无限多个 id 的可能性,这将如何改变?谢谢
  • @user1072983 请将您当前的视图模型和控制器的相关部分添加到您的帖子中,我想我可以给您一个答案,但如果没有这些信息,它将非常抽象。

标签: php codeigniter uri


【解决方案1】:

在你的控制器中使用它

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

【讨论】:

  • 您可能想检查数组中的 id 是否为数字。您可以使用 is_numeric php.net/manual/en/function.is-numeric.php 函数。
  • 我的 id 看起来像 'i3eD' a-zA-Z0-9
  • 哦,那么 is_numeric 函数显然不适合你 :)
  • 再次嗨,这对我不起作用:/ 我没有输出继承我的控制器功能:pastebin.com/qjP2MdAQ 并在视图中:pastebin.com/DSahV1pr 你能看到任何明显的错误吗?仅供参考 alphaID() 将字母数字 id 转换为真实的 db id
  • 除非url中只有一个id,否则仍然没有输出,非常困惑:s
【解决方案2】:

您是对的,您可以在$config['permitted_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

请再次注意,代码可能不准确,因为我没有测试过,但认为它会对您有所帮助。

【讨论】:

  • ci 也将 $ids 作为单个数组传递给函数,内容为“2,4,6,8”,而不是每个 id 单独,干杯
  • 基本上是的......这就是它的作用。但是这里要注意一点,它不是 CI,是你 :) 你将 "2,4,6,8" 作为 $ids 参数的值传递,此时这个参数也是一个 URI 段......看看在控制器文档中:codeigniter.com/user_guide/general/controllers.html PS从一开始$ids 是一个值为“2,4,6,8”的字符串,只有在explode 之后才变成一个数组。请参阅上面的更新代码...
  • 好的,最后我还会使用 $this->uri->segment(1) 来获取 $ids 吗?
【解决方案3】:

因为逗号不是有效的路径元素,如果没有将分隔数据放在 ?特点。你需要想出另一个方案或接受@jprofitt 的评论。

【讨论】:

  • 如果我将它们添加到 $config['permitted_uri_chars'] 怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多