如果我使用错误的单词定义,请提前道歉...我使用SimpleCart将$ .Post变量传递给PHP页面。如果我打印数组我得
数组([货币] => CAD [运费] => 0 [税] => 1.69 [taxRate] => 0.13 [itemCount] => 3 [item_name_1] =>晚餐盘[item_quantity_1 ] => 1 [item_price_1] => 5 [item_options_1] =>代码:110 [item_name_2] =>侧板[item_quantity_2] => 1 [item_price_2] => 4 [item_options_2] =>代码:125 [item_name_3] =>混合碗[item_quantity_3] => 1 [item_price_3] => 4 [item_options_3] =>代码:66 )
我正在努力(以及围绕圈子)是一种方法来执行以下操作。
分解[item_options]变量以去掉CODE:值的一部分,然后保留数字部分。
将这些值连接成一个字符串,这样我就可以使用SELECT语句来只提取在[item.options]中传递了ID的记录。
我理解如何爆炸单个参数,但无法解决如何遍历数组,爆炸密钥并创建SQL所需的值。
非常感谢任何相关教程的帮助或指示
答案 0 :(得分:0)
$codes = array();
foreach ($_POST as $key => $value) { // Loop through the $_POST array
if (preg_match('/^item_options_/', $key)) { // And validate the value
$item_arr = explode(' ', $value);
$item_id = $item_arr[1]; // Get the ID number from the value
if (is_numeric($item_id)) { // Validate it
$codes[] = $item_id; // Add it to the array we're building
}
}
}
$codes_string = implode(', ', $codes); // Concatenate them into a string that can be used in a SQL IN clause
$sql = "SELECT * from table WHERE id IN ($codes_string)"; // Build the SQL