我有这个代码的问题,在INSERT中只在每个插入中插入第一个item_price,例如我有三个不同的item_price,谢谢你帮助对不起英文不好
表格中的html部分:
<table>
<tr id="shop1">
<td>1</td> <!-- item_id -->
<td><input type="text" value="33" name="price"></td> <!-- item_price -->
</tr>
<tr id="shop2">
<td>2</td> <!-- item_id -->
<td><input type="text" value="11" name="price"></td> <!-- item_price -->
</tr>
<tr id="shop3">
<td>3</td> <!-- item_id -->
<td><input type="text" value="65" name="price"></td> <!-- item_price -->
</tr>
</table>
<button id="checkoutinventory" class="btn-danger"></button>
javascript部分:
$(document).on("click","button#checkoutinventory, a[href$='checkoutinventory']",function()
{
var items = "";
var price = "";
$("tr[id^=shop] button.btn-danger").each(function()
{
items += $(this).parents("tr").attr("id").replace(/\D/g,'')+",";
price += $(this).parents("tr").find('input[name="price"]').val().replace(/\D/g,'')+","; //this code send price in this format: 1,2,3,etc
});
if (!items)
return displayResponse("ERROR","an error ocurred try again.");
var settings = {};
settings['action'] = "checkoutinventory";
settings['items'] = items;
settings['price'] = price;
$.get("includes/process.php", settings, function(data)
{
processResponse(data);
});
return false;
});
process.php部分:
if ($_GET['action'] == "checkoutinventory")
{
if (!@$character)
exit("REFRESH");
if (!$VALIDATE->blank(@$_GET['items']))
exit();
$items = explode(",", rtrim($_GET['items'],",")); //Here obtain item_id in this format: id_1,id_2,id_3,etc
$price = $_GET['price']; //Here obtain item_price in this format: price1,price2,price3,etc
$isonline = $SERVER->select($query['characterIsonline'],array($COOKIE->get("character")));
if(!$isonline)
{
$itemData = $SERVER->select("SELECT * FROM `items` WHERE `object_id` IN (".implode(",",$items).");",array()); //Here obtain perfectly information for insert later in foreach
} else {
exit("RESPONSE^ERROR^Your character <b>".$character['char_name']."</b> is online disconnect please and try again..");
}
if($itemData) {
foreach($itemData as $itemlist)
{
$selling = $SERVER->insert("INSERT INTO `shop_items` (`itemId`,`itemAmount`,`itemPrice`,`itemEnchant`,`vendedor`,`object_id`) VALUES (?,?,?,?,?,?);",array($itemlist['item_id'],$itemlist['count'],$_GET['price'],$itemlist['enchant_level'],$character['char_name'],$itemlist['object_id']));
}
}
if (count($items) != count($itemData))
exit("RESPONSE^ERROR^One of the items is no longer available for sale.");
if($selling) {
exit("RESPONSE^SUCCESS^Congrats <b>".$character['char_name']."</b>, you sell ".count($items)." item(s)!");
}
exit("RESPONSE^ERROR^Unexpected error occured.");
}
答案 0 :(得分:0)
这假设您希望按位置将价格与项目匹配(即价格数组中的第一个值属于项目数组中的第一个值)
if($itemData) {
$prices = explode(',',$price); //create an array of prices
foreach($itemData as $itemlist) {
$single_price = array_shift($prices); //remove the 1st price from array
$selling = $SERVER->insert("INSERT INTO `shop_items`
(`itemId`,`itemAmount`,`itemPrice`,`itemEnchant`,`vendedor`,`object_id`)
VALUES (?,?,?,?,?,?);",
array($itemlist['item_id'],$itemlist['count'],$single_price,$itemlist['enchant_level'],$character['char_name'],$itemlist['object_id']));
}
}