0

有人告诉我,我可以从帖子中提取单个内容值value,然后将它们放入变量中:

$Product .= "${item_id}-${length}-${Category}-${each_item['quantity']},";

变量的结果类似于:women_boot-16 Inch-Virgin_boots-3在这种情况下,我的产品 ID 是“women_boot”。在我的表单中,我执行$product到值:

echo = '<input type="hidden" name="custom" value="' . $Product . '">';

在页面中,我发布到我有:$product_id_string = $_POST['custom'];这将成为$product_id_string = $_POST['women_boot-16 Inch-Virgin_boots-3,'];


我现在想从帖子中提取每条内容,这样我就可以像这样形成它们:

$id   =  women_boot
$length = 16 Inch
$type = Virgin_boots
$amount = 3

我已经尝试过以下代码,但它只有在我有 1 到 3 个时才有效。我怎样才能使它适用于我的所有价值观?

$product_id_string = rtrim($product_id_string, ","); // remove last comma
$id_str_array = explode(",", $product_id_string); // Uses Comma(,) as delimiter(break point)
$fullAmount = 0;
foreach ($id_str_array as $key => $value) {
$id_quantity_pair = explode("-", $value);
$product_id = $id_quantity_pair[0]; // Get the product ID

更新 我需要提取此内容以使其工作:

$stmt2 = $conn->prepare("
    SELECT bb.Price FROM Product aa, Itemised_Product bb, Category cc 
    WHERE aa.Sef = :product_id
    bb.ItemID = :length
    AND 
    cc.CatID = :Category LIMIT 1");
    $stmt2->bindParam('product_id',$product_id);
    $stmt2->bindParam('length',$length); 
    $stmt2->bindParam('Category',$category);
4

1 回答 1

1
$product_id_string = "men_coat-16 Inch-Virgin_boots-3,women_boot-16 Inch-Virgin_boots-1,";
$product_id_string = rtrim($product_id_string, ","); // remove last comma
$id_str_array = explode(",", $product_id_string); // Uses Comma(,) as delimiter(break point)
$items = array();
foreach ($id_str_array as $key => $value) {
    $id_quantity_pair = explode("-", $value);
    $items[$key]['item_id'] = $id_quantity_pair[0];
    $items[$key]['length'] = $id_quantity_pair[1];
    $items[$key]['category'] = $id_quantity_pair[2];
    $items[$key]['quantity'] = $id_quantity_pair[3];
}
print_r($items);

您可以按如下方式准备查询:

$stmt2 = $conn->prepare("
    SELECT bb.Price FROM Product aa, Itemised_Product bb, Category cc 
    WHERE aa.Sef = :product_id
    bb.ItemID = :length
    AND 
    cc.CatID = :Category LIMIT 1");
    $stmt2->bindParam('product_id',$item[0]['item_id']);
    $stmt2->bindParam('length',$item[0]['length']); 
    $stmt2->bindParam('Category',$item[0]['category']);
于 2013-08-05T13:55:04.000 回答