【问题标题】:GET url parameters Codeigniter获取 url 参数 Codeigniter
【发布时间】:2017-07-28 12:28:08
【问题描述】:

一个视图,一个控制器,需要使用get参数,通过链接改变路径。所以最后变成了这样:

即将发布 -> http://mywebsite.com/tournaments?type=upcoming

完成 -> http://mywebsite.com/tournaments?type=finished

【问题讨论】:

  • 您的问题到底是什么?请说明?
  • 如何实现!?
  • 如果可能的话使用代码示例
  • 如何使用GET正确传递URL中的参数?
  • 这正是您所需要的

标签: codeigniter-3


【解决方案1】:

这里是处理你的 GET 变量的控制器

Controller
<?php 
    class Demo_controller extends CI_Controller 
    {
      public function demo($id)
      {
         echo $id; //the id which you will get in URL string
      }
     }
?>

查看您将传递变量的文件

<?php
$id = "1";//dyanmic id which will be pased with the form
echo form_open('demo/demos-function'.$id);
//in between your code
echo form_close
?>

Routes
Inside routes.php
$route['demo/demos-function/(:any)] = Demo_controller/demo/$1

无需更改 $1 它将解释它具有 URL 参数的路由。 所以最终你的链接将是

https://localhost/demo/demos-function/1
https://localhost/demo/demos-function/2

【讨论】:

  • 这一切都不错,但我需要这一切通过 if(){} 运算符,并检查是否有 GET 参数,然后加载一个视图,如果没有则加载另一个...
【解决方案2】:

在这样的 Codeigniter 网址中:

即将发布 -> http://mywebsite.com/controller/method/upcoming

完成 -> http://mywebsite.com/controller/method/finished

public function method_name()
{
    $type = $this->uri->segment(3); // third level value of URL - Segment(1) is controller and followed by method and query string 

   if($type == 'upcoming')
   {
      echo $type;
     $this->load->view('upcoming');
   }
   else
    {
      echo $type;
      $this->load->view('finished');
    }
}

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2021-05-09
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多