doufen2769 2016-10-25 11:30
浏览 73

将关联JSON数组转换为嵌套SQL WHERE子句

I'm new to backend programming and CodeIgniter is my first framework. This is new to me. Right now, the thing that I'm working is that I need to create a query based on the JSON encoded data saved at my table.

I have here a segmentation form that should build a query to look/search for the customers profiles based on the condition set

Segmentation form

    $segment = array(
       ...
       'filters' => json_encode($post['filters']), //to insert the filters in json
       ...
    );
    if ($this->model->addSegments($segment))

The filters saved in the database are like this :

{"data":[{"data":{"condition":"0","attribute":"gender","sign":"=","value":"Male"},"type":"condition","operator":"0","filter":{"operator":"0"}}],"type":"match","operator":"0"}

Looking for the answers, I found this forum and I relate it to the problem that I had: http://www.codingforums.com/php/202207-turning-json-object-into-nested-sql-where-clause.html

Calling the same like function (solution at the link) to the controller using this lines:

$filter = $this->model->getSegmentsFilters($id_segment);
$filters = $this->model->parseFilterToQuery(json_decode($filter['filters'], true));

With the parameter TRUE from the json_decode, It throws the filters in array and using print_r shown like this:

Array ( 
    [0] => Array ( 
        [data] => Array ( 
            [condition] => 0 
            [attribute] => firstname 
            [sign] => = 
            [value] => John ) 
        [type] => condition 
        [operator] => 0 
        [filter] => Array ( 
            [operator] => 0 ) 
        ) 
    [1] => Array ( 
        [data] => Array ( 
            [condition] => 0 
            [attribute] => gender 
            [sign] => = 
            [value] => Male ) 
        [type] => condition 
        [operator] => condition 
        [filter] => Array ( 
            [operator] => 0 ) 
        ) 
    ) 

the $sql inside if(is_array) doesn't show any value to concatenate with. I want it to concatenate with another query as a WHERE clause like this idea.

$sql = "SELECT * FROM customers WHERE" . parseFilterToQuery($filters);

If you think that the solution/procedure that I came up is correct. My question is that inside the if(is_array) condition, how should I work on it with associative JSON array?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大