简体   繁体   English

构建查询字符串链接

[英]Building query string links

I'm building a filters section for my search page and I was wondering what is the best way to go about doing the query strings. 我正在为搜索页面构建一个过滤器部分,我想知道执行查询字符串的最佳方法是什么。 My problem is that these links function a lot like checkboxes, so some, all or none can be on. 我的问题是这些链接的功能与复选框非常相似,因此某些,全部或全部都不会启用。 I'd have to loop through each of about 30 or so links, removing or adding that specific link's value depending on its state. 我必须遍历大约30个左右的链接,并根据其状态删除或添加该特定链接的值。

My first concern is: 我首先关心的是:

Should I pass the arrays like 我应该像这样传递数组吗

colors=red,blue,green   //explode?

or 要么

colors[]=red&colors[]=blue&colors[]=green   //parse_str?

What is the fastest/best way to remove a certain value of a certain array as I loop through each link? 当我遍历每个链接时,最快/最好的方法是删除某个数组的某个值吗? I imagine it'd be a bit more complicated using the second method I've posted above, yes? 我想使用上面发布的第二种方法会更加复杂,是吗?



EDIT2 - What do you think of this? 编辑2-您对此有何看法?

I've ran into a tutorial online and came up with this: 我在网上遇到了一个教程,并提出了以下建议:

function remove( $filters = array(), $remove_key = NULL, $remove_val = NULL )
    {
        if( $remove_key != NULL && array_key_exists($remove_key,$filters) )
        {
            if( $remove_val != NULL && array_key_exists($remove_val,array_flip($filters[$remove_key])) )
            {
                $filters[$remove_key] = array_diff($filters[$remove_key],array($remove_val));
            } else {
                unset($filters[$remove_key]);
            }
        }
        return http_build_query( $filters );
    }

Currently, I can pass $remove_key to remove a key and $remove_val to remove a value from a key in an array. 当前,我可以传递$ remove_key删除键,并传递$ remove_val从数组的键中删除值。

What do you guys think? 你们有什么感想? Would this be too slow for for doing 30-50 links? 这样做30-50个链接是否太慢? Thanks! 谢谢!

 colors[]=red&colors[]=blue&colors[]=green 

Would be a the Way a Form would submit the data (when method="get"). 将是表单提交数据的一种方式(当method =“ get”时)。 And you can access it via $_GET['colors'] which is the native and by that probably the fastest way. 您可以通过$ _GET ['colors']来访问它,这是本机的,并且可能是最快的方法。

EDIT: to get that string via http_build_query just fill them in the array color 编辑:通过http_build_query获得该字符串,只需将它们填充为数组颜色

$data = array('colors' => array('green','red','blue'));
echo http_build_query($data); // colors[0]=green&colors[1]=red&colors[2]=blue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM