duandou2763 2015-05-15 23:50
浏览 5

将用户输入作为数组并逐个分发

I am developing a website that can let the user buy something. The problem comes if the user buys many items at once.

For example: User A bought item 1 with quantity 2 User B bought item 2 with quantity 1

I want the confirmation page to display:

You have bought 
Item 1, quantity: 2
Item 2, quantity: 1

The problem is that I can't find how to get each of the quantity based on the selected item. I am just able to display directly like this

You have bought
Item 1, quantity: 2 quantity 1
Item 2, quantity: 2 quantity 1

This is my user input form

echo "<input type=\"text\" hidden name=\"cart[]\" id=\"itemIDValue\" value=\" ".$itemID ."\"/>";
echo "<input type=\"text\" hidden name=\"qty[]\" value=\"".$itemQty."\">";

and this is my PHP code

$sql = "this will get all the data from database";
$result = mysqli_query($connection, $sql);

            while ($row = mysqli_fetch_array($result)){

                  echo"<p> Brand : " . $row[4] . " </p>
                       <p> Name : " . $row[3] ."</p>";
            }

Basically I can get the item name and the brand correctly, but I can't get the quantity correctly for that particular item.

I tried to use foreach loop

 while ($row = mysqli_fetch_array($result)){

             foreach($qty as $a => $value){
                  echo"<p> Brand : " . $row[0] . " </p>
                       <p> Name : " . $row[1] ."</p>
                       <p> Qty : " . $value . "</p>";
            }
 }

The foreach loop will give me what I want, but now the problem is, if the user bought 2 items, the data output will be repeated and as a result there will be 4 items(2 items x2)

Can anybody help me please? Any help given is highly appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • doujiang7258 2015-05-16 00:44
    关注

    here's an adaption for your problem, already answered in that question (dublicate):

    $_POST Array from html form

    while ($row = mysqli_fetch_array($result)){
        // that's what you need:
        $qty=$_POST['qty'];
    
        foreach($qty as $a => $value){
             echo"<p> Brand : " . $row[0] . " </p>
                  <p> Name : " . $row[1] ."</p>
                  <p> Qty : " . $value . "</p>";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度