如何使用 codeigniter uri 格式创建搜索表单?

发布于 2024-11-03 23:01:02 字数 1043 浏览 2 评论 0原文

亲爱的大家,我是 codeigniter 的菜鸟,

我想寻求建议,了解如何创建一个搜索表单,该表单将导致 url 为:

http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val< /a>

表格将类似于

<form action="http://domain/class/index">
   <input name="searchvar1" value="searchvar1val">
   <input name="searchvar2" value="searchvar2val">
   <input type="submit">
</form>

什么是最佳实践?

我已经用谷歌搜索过包括 stackoverflow 帖子,仍然找不到任何解决方案。

谢谢 :)

~thisismyfirstpostthere

更新 ::

我想强调,我的目标是处理来自 uri 字符串的搜索变量(上面),不是来自后变量;所以我认为将表单搜索设置为 POST 不是一个选项? :)

更新 (2)

我不认为这可以在 codeigniter 中开箱即用,我正在考虑将 vars/vals 形式客户端处理为使用 jQuery 的 URI 格式的表单操作。 完成后将在此处发布更新。

dear all, i'm a noob in codeigniter,

i would like to ask for advise, on how to create a search form that would result to url as:

http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val

the form would be like

<form action="http://domain/class/index">
   <input name="searchvar1" value="searchvar1val">
   <input name="searchvar2" value="searchvar2val">
   <input type="submit">
</form>

what is the best practice for this?

i've googled around including stackoverflow posts, still can't find any solution.

thanks :)

~thisismyfirstposthere

update ::

i would like to emphasize that my objective is to process search variables from the uri string (above), not from post vars; so i think setting the form search to POST is not an option? :)

update (2)

i don't think this can be done out of the box in codeigniter, i'm thinking of client-processing the form vars/vals into the form action in URI format using jQuery.
will post an update here later when done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

埋情葬爱 2024-11-10 23:01:02

查看 user_guide

$this->uri->assoc_to_uri()

采用关联数组作为输入
并从中生成一个 URI 字符串。
数组键将包含在
细绳。示例:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');

$str = $this->uri->assoc_to_uri($array);

// Produces: product/shoes/size/large/color/red 

我认为如果您的表单是这样的:

<form action="http://domain/class/index/search">
   <input name="product" value="shoes">
   <input name="size" value="large">
   <input name="color" value="red">
   <input type="submit">
</form>

那么在搜索控制器中的某个位置执行此 $this->uri->assoc_to_uri($data); 恕我直言,将生成 product/shoes /尺寸/大/颜色/红色

taking a look at the user_guide :

$this->uri->assoc_to_uri()

Takes an associative array as input
and generates a URI string from it.
The array keys will be included in the
string. Example:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');

$str = $this->uri->assoc_to_uri($array);

// Produces: product/shoes/size/large/color/red 

and i think if your form is this:

<form action="http://domain/class/index/search">
   <input name="product" value="shoes">
   <input name="size" value="large">
   <input name="color" value="red">
   <input type="submit">
</form>

then somewhere in the search controller do this $this->uri->assoc_to_uri($data); IMHO will produce product/shoes/size/large/color/red

嘿咻 2024-11-10 23:01:02

嘿伙计们,感谢您的回复和讨论,我认为我会顺其自然:

查看 -->形式--> jquery,在表单提交时:用表单参数替换操作属性 -->带有 uri 字符串的控制器 -->流程的其余部分,

jquery 部分将类似于以下

$("#searchFormID").submit(function(){

    // $(this).serialize() outputs param1=value1¶m2=value2 string
    // var.replace(regex, string) outputs param1/value1/param2/value2 string
    // newact would be http://localhost/class/index/param1/value1/param2/value2
    var newact = $(this).attr("action") + "/" + $(this).serialize().replace(/&|=/g,"/");

    $(this).attr("action", newact); // or $(this).attr("target", newact);

    return true;
});

注释:

  • 向表单添加一个 id 属性 ->对于 jquery 中的表单选择器,
  • 请使用表单中的 post 方法(这是 codeigniter 中的默认设置)->所以操作 url 不会填充 GET 字符串

hey guys, thanks for replying and discussing, I think imma go with flow:

View --> form --> jquery, on form submit: replace action attribute with form params --> controller with uri string --> rest of the process

the jquery part would be something like this

$("#searchFormID").submit(function(){

    // $(this).serialize() outputs param1=value1¶m2=value2 string
    // var.replace(regex, string) outputs param1/value1/param2/value2 string
    // newact would be http://localhost/class/index/param1/value1/param2/value2
    var newact = $(this).attr("action") + "/" + $(this).serialize().replace(/&|=/g,"/");

    $(this).attr("action", newact); // or $(this).attr("target", newact);

    return true;
});

notes:

  • add an id attribute to the form -> for the form selector in jquery
  • use post method in the form (this is default in codeigniter) -> so action url will not be populated with the GET string
人间不值得 2024-11-10 23:01:02

您可能需要启用查询字符串并使用$_GET. IMO,这是查询字符串用途的完美示例。

确保将表单更改为

然后,您的 URL 中将包含一个查询字符串,例如 ?searchvar1=value1&searchvar2=value2

您可以像这样访问字符串的一部分: $this->input->get('searchvar1')

使用您的方法当前使用中,如果用户搜索 $config['permissed_uri_chars'] 中不允许的任何字符,您将必须自己进行大量编码和解码以确保字符串安全/在您的网址中可读。 CI 输入类会为您处理这个问题,所以这是我的建议。

基于 URI 段的搜索参数的另一个缺点是没有正确的方式来表达像 ?values[]=one&values[]=two&values[]=三 这样的数组代码>.有一些技巧可以解决这个问题,但它们都只是:技巧。你最终会遇到麻烦。

使用 $_POST 非常繁琐,并且强制使用表单提交来获取分页结果和链接,并且还确保您无法链接到搜索结果或正确使用后退按钮。

如果您必须使用此方法,只需将表单发布到您的控制器,读取输入,然后redirect()到您从中构造的URL。 Littlechad 在 他的答案

// This URL:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val

// With this:
$search = $this->uri->uri_to_assoc();

// Produces this:
array(
    'searchvar1'    =>    'searchvarval1',
    'searchvar2'    =>    'searchvarval2',
);

// And you can use it like this:
$arg1 = $search['searchvar1'];
$arg2 = $search['searchvar2'];

要到达此网址,您可以对表单帖子执行类似的操作(在帖子后的控制器中):

$var1 = $this->input->post('searchvar1');
$var2 = $this->input->post('searchvar2');
redirect('searchvar1/'.urlencode($var1).'/searchvar2/'.urlencode($var2));

我相信您可以看到这比仅仅使用尝试过的工作量要多得多- and-true 查询字符串...

You may want to enable query strings and use $_GET. IMO, this is a perfect example what the query string is intended for.

Make sure to change your form to <form method="get" action="http://domain/class/index">

You will then have a query string in your URL like ?searchvar1=value1&searchvar2=value2

You can access parts of the string like this: $this->input->get('searchvar1')

With the method you are currently using, if a user searches for any characters that are not allowed in your $config['permitted_uri_chars'], you will have to do a lot of encoding and decoding yourself to make sure the string is safe/readable in your URL. The CI Input class takes care of this for you, so it is my recommendation.

Another disadvantage to URI segment based search params is that there is no proper way to express an array like ?values[]=one&values[]=two&values[]=three. There are hacks to work around this, but they are all exactly that: hacks. you will eventually run into trouble.

Using $_POST is very tedious and forces the use of form submits for paginated results and links, and also ensures that you cannot link to the search results or use the back button properly.

If you must use this method, simply have the form post to your controller, read the input, and then redirect() to the url that you construct from it. littlechad has the right idea for this method in his answer

// This URL:
http://domain/class/index/searchvar1/searcvar1val/searchvar2/searchvar2val

// With this:
$search = $this->uri->uri_to_assoc();

// Produces this:
array(
    'searchvar1'    =>    'searchvarval1',
    'searchvar2'    =>    'searchvarval2',
);

// And you can use it like this:
$arg1 = $search['searchvar1'];
$arg2 = $search['searchvar2'];

To get TO this url, you can do something like this with your form post (in the controller after post):

$var1 = $this->input->post('searchvar1');
$var2 = $this->input->post('searchvar2');
redirect('searchvar1/'.urlencode($var1).'/searchvar2/'.urlencode($var2));

I'm sure you can see how much more work this is than just using the tried-and-true query string...

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