How can I use the ajax product filtering function to determine how to store the various options of the product to a database field (cp_leibie) in json format)
For example, I saved three
Their options are
{"208": ["209"], "5": ["182"], "6": ["30"], "27 ": ["36"], "28": ["34"]} if the data id is 1
{"92": ["201", "202", "204"], "93": ["137", "138", "139"], "94": ["143", "144"]} if the data id is 2
{"208": ["209"], "5": ["182"], "6": ["29"], "27 ": ["35"], "28": ["33"]} assume that the data id is 3
Traverse by array when retrieving
-----------------------------------------------------------------
For example, one of the items is (applicable environment: all office modern European)
Then $ peistr is the id of the "applicable environment", and $ liestr is the id of the suboptions next to it,
I will not post any details about how to receive them,
In the for loop below, there is no need to consider why we subtract two.
The following are the query statements submitted by ajax:
$ Strpei = "select 'id', 'name', 'huiyuanjia ', 'cpim', 'shichangjia', cp_leibie from "'. $ db_prefix. "chanpin 'where' ifxianshi '= 1 and 'cpfl' = '". $ sid. "'"; $ dqcp = $ Db-> FetchAll ($ strpei); // This is the method for executing SQL statements $ strpei. = "and id in ("; $ strpei2 = ""; foreach ($ dqcp as $ dkey => $ dvalue) {// traverse all items $ darr = json_decode (urldecode ($ dvalue ['CP _ leibie ']), true ); // decodes json Data for ($ I = 0; $ I
$ Dddvalue) {// traverse the decoded data. The key name is the idif of the received item option ($ liestr [$ I] ==$ dddvalue) {// if the sub-option id is the same as the value in the json data, add the data id to the condition $ strpei2. = $ dvalue ['id']. "," ;}}}$ strpei. = $ strpei2 .")";
If the conditions are met, the corresponding item id is written into the in syntax of the conditions.
If you click a subitem of 209, the IDS with IDs 1 and 3 will be added to the condition.
The problem is that it is okay to receive the data for the first time. when you click 201 for the second time, there should be no matching data, but the data of the three IDs is displayed.
It should be the if statement in the loop that is not written correctly. I don't know how to write the condition. Or you can solve it elsewhere.
Reply to discussion (solution)
$ Peistr can be printed out and checked?
$ Peistr can be printed out and checked?
If you select a subitem of 209
Print the result of $ peistr
Array (
[0] = & gt; 208
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
)
Print the result of $ liestr
Array (
[0] = & gt; 209
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
)
After selecting a subitem of 209, select a subitem of 30.
Print the result of $ peistr
Array (
[0] = & gt; 208
[1] =>
[2] => 6
[3] =>
[4] =>
[5] =>
[6] =>
)
Print the result of $ liestr
Array (
[0] = & gt; 209
[1] =>
[2] => 30
[3] =>
[4] =>
[5] =>
[6] =>
)
The last two in the array, that is, the key name is 5 or 6, which is useless, that is, why 2 is required in the for loop. In fact, there are only five options for commodity filtering, and the following are additional items.
In this case, only the id 3 that meets the criteria should be selected.
The condition I wrote above
If ($ liestr [$ I] ==$ dddvalue ){/
$ Strpei2. = $ dvalue ['id']. ",";
}
Calculate all the qualified items.
That is, the subitem of 208, which has id 1 and id 3. All the data of the two IDs is retrieved.
$ Peistr can be printed out and checked?
The last sentence is "so he checked the data of both IDs"
Strange positioning of data organization forms, strange query construction methods
Strange positioning of data organization forms, strange query construction methods
This is also an idea. The number of each option in each topic is different. you cannot create a field for each option.
I came up with the idea that only one index has a value when the $ peistr array receives it for the first time. When receiving the second request, two indexes have values. Then we can judge this value when we receive it for the first time. When receiving the second message, both values are determined at the same time. And so on. But I haven't figured out how to write it.
Another idea is that the id of the qualified product is added to the condition when receiving the message for the first time. When receiving the second request, if there is one in the original id, it is added. Add this ID to the condition.
I changed my judgment.
if($liestr[$i]==$dddvalue){ $strpei2.=$dvalue['id'].",";}
$where.="$liestr[$i]==$dddvalue &&";if($i==(count($peistr)-2)){$where= substr($where,0,-2);}if($where){$strpei2.=$dvalue['id'].",";}
But in this case, $ where is a string to be placed in if for determination. No judgment can be performed. no matter what $ where is, it is 1. This is just an idea. let's take a look.
I changed my judgment.
if($liestr[$i]==$dddvalue){ $strpei2.=$dvalue['id'].",";}
$where.="$liestr[$i]==$dddvalue &&";if($i==(count($peistr)-2)){$where= substr($where,0,-2);}if($where){$strpei2.=$dvalue['id'].",";}
But in this case, $ where is a string to be placed in if for determination. No judgment can be performed. no matter what $ where is, it is 1. This is just an idea. let's take a look.
Yes, because it is connected as a string, if determines that the string result is 1. This method may not work.
I thought of another method. I put the sub-item id in a field and used FIND_IN_SET for query. and FIND_IN_SET is added after each query, query the received subitem id in the field.