douxunwei8259 2015-01-04 05:51
浏览 60

javascript发送变量但插入数组php无法正常工作

i have a problem with this code, in INSERT only insert the first item_price in every insert, in example i have three differents item_price, thanks for you help sorry for bad english

html part in table:

<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 part:

$(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 part:

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.");
    }
  • 写回答

1条回答 默认 最新

  • doujia2463 2015-01-04 06:22
    关注

    This assumes you want to match the price to the item by position (i.e. 1st value in the price array belongs to the 1st value in the item array)

    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']));
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)