拉出并存储个人内容

时间:2013-08-05 13:34:38

标签: php

有人告诉我,我可以从发布的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

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

$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);

1 个答案:

答案 0 :(得分: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']);