我正在尝试编写一个可以做两件事的脚本:
1)运行两列表,然后将内容推送到两列数组
2)运行那两个列数组(在#1中制作)并将其重新插入到两列MySQL表中。
这是我对事物#1的尝试:
//grab desired product options incase of 'undo'
$optionArray = array();
$fabricArray = array();
$optionSelect = "SELECT * FROM $clientOptions WHERE bag_id ='$bagID' ";
$optionResult = mysql_query($optionSelect, $db_bags);
while ($optionData = mysql_fetch_assoc($optionResult)){
$optionSku = $optionData['option_sku'];
$fabricSku = $optionData['fabric_sku'];
array_push($optionArray, $optionSku);
array_push($fabricArray, $fabricSku);
array_push($optionArray, $fabricArray);
}
$_SESSION['options_array'] = $optionArray;
以下是我对第2项的尝试:
foreach ($_SESSION['options_array'] as $optionSku){
foreach ($optionSku as $fabricSku){
$insertOption = "INSERT INTO $clientOptions (bag_id, product_sku, option_sku, fabric_sku) VALUES ('$bagID', '$productSku', '$optionSku', '$fabricSku')";
mysql_query($insertOption, $db_bags);
}//end of fabric sku for loop
}//end of option sku for loop
一般目的:向所有用户撤消删除功能。用户可以删除产品及其选项,然后通知“撤消”删除。刚删除的产品存储在会话中,选项(和结构)存储在会话数组中。
答案 0 :(得分:1)
您正在构建的阵列将如下所示:
optionArray[0] = optionSku
optionArray[1] = array(fabricSku)
依旧......
如果想要完全想要数据,你会想要这样的东西:
optionArray[0] = array(optionSku, fabricSku)
为此,您需要这样做:
while ($optionData = mysql_fetch_assoc($optionResult)){
$optionSku = $optionData['option_sku'];
$fabricSku = $optionData['fabric_sku'];
array_push($optionArray, array($optionSku, $fabricSku));
}
我最终会将其减少为:
while ($optionData = mysql_fetch_assoc($optionResult)){
array_push($optionArray, array($optionData['option_sku'], $optionData['fabric_sku']));
}
那么你的循环就是(虽然我不确定bagID或productSku来自哪里):
foreach($_SESSION['options_array'] as $optionSkuData) {
$insertOption = "INSERT INTO $clientOptions (bag_id, product_sku, option_sku, fabric_sku) VALUES ('$bagID', '$productSku', '$optionSkuData[0]', '$optionSkuData[1]')";
mysql_query($insertOption, $db_bags);
}