doushi1847 2009-01-29 04:06
浏览 35
已采纳

PHP数组的问题

I'm sending some data over AJAX to a PHP file. It's about filter options. Anyway, I am sending it like this:

filter[0][data][type]   string
filter[0][data][value] automobiles
filter[0][field] product

filter[1][data][type] numeric
filter[1][data][value] 6000
filter[1][field] price

This above is taken from FireBug console. Then, in PHP:

$filter = $_POST['filter'.$i];
if (is_array($filter)) {
    for ($i=0;$i<count($filter);$i++){
        switch($filter[$i]['data']['type']){
            case 'string' : 
                        // I'm doing my select from database based on that and so on

So, the translation of this would be: "Get me all the records from database that are: hmm, let's check the filters... I'm getting the first filter type which is "string" which needs to be applied to the mysql column named "product"...So I'm searching for the value of "automobiles" there...But I'm not finished yet, the second filter refers to a numeric filter for the column "price" from database. so I'm taking its value and add it to the query. So I'll end up selecting all the automobiles that have a price greater than 6000.

So far so good. The problem is that my way of getting data have changed and I can't send my data in this format no more. The new format is an URL which looks like this:

filter[0][field]=prodct&filter[0][data][type]=string&filter[0][data][value]=automobiles&filter[1][field]=price&filter[1][data][type]=numeric&filter[1][data][value]=6000

I can do an explode on this one by the "&" and end up with an array...I can do a lot... The problem is that I don't know how to adjust my query building script to work with that kind of received data..So that I can do a "switch($filter[$i]['data']['type']){" again...

Any ideas for modifying the code ?

Thank you!

  • 写回答

2条回答 默认 最新

  • duanguan5922 2009-01-29 04:28
    关注

    parse_str($queryString); and then do everything as normal. That method will process the query string and import the variables to the global namespace (which could be dangerous), so maybe use its second form (listed on the manual page):

    $result = array();
    parse_str($queryString, $result)
    $filters = $result['filter'];
    
    foreach($filters as $filter) {
    // your code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法