简体   繁体   English

foreach循环结果插入到数据库

[英]foreach loop results to INSERT into database

I have three different div's that contain the checkout information. 我有三个不同的div,其中包含结帐信息。

  1. Shipping info 运输信息
  2. Billing Info 计费信息
  3. Order confirmation 订单确认

The shipping information and billing information is obtained by the customer entering that information in manually, but the order confirmation, that contains what they are ordering, the quantity, and pertinent information to that order resides there. 运送信息和开票信息是通过客户手动输入该信息而获得的,但是订单确认(包含他们正在订购的内容,数量和与该订单相关的信息)驻留在此。 That information is obtained from a foreach loop I have in place that displays the information based on the product's ID. 该信息是从我已经到位的foreach循环中获得的,该循环根据产品的ID显示信息。

I am trying to figure out how I am going to INSERT the string that displays from my foreach loop into my database. 我试图弄清楚如何将从我的foreach循环中显示的字符串INSERT到数据库中。 I have an order report page that I want to display what was ordered. 我有一个订单报告页面,我想显示所订购的东西。

For the shipping information, I validate it and then send my query in with that information. 对于运输信息,我将对其进行验证,然后使用该信息发送查询。 Like this... 像这样...

<?php
if(Input::exists()) {
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
        'fullname' => array(
            'required' => true,
            'min' => 2,
            'max' => 50
        )

if($validation->passed()) {
        if(isset($_POST['create'])){ 
            $fullname = trim( $_POST['customer_name'] );
  ?>

<div class="field">
     <label class="paddingleft" for="fullname">Full Name</label>
     <div class="center"><input type="text"  class="biginputbarinline" name="fullname" value="<?php echo escape(Input::get('firstname')); ?>" required></div>
</div>

The part that I am really confused with is how to INSERT the actual string this foreach loop displays. 我真正困惑的部分是如何插入此foreach循环显示的实际字符串。 If the result of my foreach loop was: 如果我的foreach循环的结果是:

2 balls 2球

4 shoes. 4鞋。

I want that information to send in with my query. 我希望该信息随查询一起发送。

This is how I have the Order confirmation section as of now.. 到目前为止,这就是我的“订单确认”部分的方式。

<div class="checkoutconfirmationcontainer">
<?php foreach($_SESSION['shopping_cart'] as $id => $product) {
      $product_id = $product['product_id'];
?>
      <span class="tealmedium"><?php echo $product['quantity'] . " - "  . $products[$product_id]['name'] . $message; ?></span><br><br><br>
           <div class="floatleft"><div class="smallerimgcontainer">
           <?php
           $result = mysqli_query($con,"SELECT * FROM products");
           if($row = mysqli_fetch_array($result)) {
                $products[$row['product_id']] = $row;
                if($row['image'] == ""){
                     echo "<img class='sizedimg' src='/productpics/coming_soon.png' alt='Coming Soon'>";
                } else {
                     echo "<img class='sizedimg' src='/productpics/".$row['img']."' alt='Product Picture'>";
                }
                echo "<br><br><br><br>";
           }
           ?>
           </div></div>
           <div class="checkoutitemsummary">
           <?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'];?><?php echo $products[$product_id]['name']; ?> </a>
                <p><span class="redprice"><?php echo '$' . $products[$product_id]['price'] . "<br />"; }?></span></p>
           </div>

How can I get the results of my foreach loop to be inserted into my database with my query? 如何获取与查询一起插入到数据库中的foreach循环的结果?

Shopping Cart 购物车

<tr>
                                <th class="cartth">Name</th>
                                <th class="cartth">Price</th>
                                <th class="cartth">Category</th>
                                <th class="cartth">Quantity</th>
                            </tr>
<?php                               
                        $base_price = 0;
                        foreach($_SESSION['shopping_cart'] as $id => $product) {
                                    $product_id = $product['product_id'];
                                    $base_price += $products[$product_id]['price'] * $product['quantity'];
                                    $shipping_price += $products[$product_id]['shippingprice'] * $product['quantity'];
?>
                                <tr>
                                        <td class="carttd"><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'];?><?php echo $products[$product_id]['name']; ?> </a>
                                        </td>
                                        <td class="carttd"><?php echo '$' . $products[$product_id]['price']; ?></td> 
                                        <td class="carttd"><?php echo $products[$product_id]['category']; ?></td>
                                        <td class="carttd">
                                        <?php echo "<input type='text' name='quantity[$product_id]'  value='" . $product['quantity'] . "' />"; ?> </td>                                         
                                </tr>
<?php
                                }

Javascript/Jquery that produces my div transition: 产生我的div转换的Javascript / Jquery:

$('.checkoutmenu>li').on('click',function(e){
    $('.checkoutprocess>.'+ e.target.classList[0]).show().siblings().hide();
});


<script>                    
$('.paymentinfocontainer').hide();
$('.confirmationinfocontainer').hide();
</script>
<script>
$('#button2').click(function () {
    $(".checkoutprocess > div").hide();
    $('.paymentinfocontainer').show("slow");
});
</script>
<script>
$('#button3 ').click(function () {
    $(".checkoutprocess > div").hide();
    $('.confirmationinfocontainer').show("slow");
});
</script>

<script>
/*
$('#button1').click(function () {
    $(".checkoutprocess > div").hide();
    $('.shippinginfocontainer').show("slow");
});
</script>
<script>
$('#button2 ').click(function () {
    $(".checkoutprocess > div").hide();
    $('.paymentinfocontainer').show("slow");
});
</script>
<script>
$('#button3 ').click(function () {
    $(".checkoutprocess > div").hide();
    $('.confirmationinfocontainer').show("slow");
});
*/
</script>

Ok, when I clicked on "3. Order Confirmation", I got the following HTML: 好的,当我单击“ 3. Order Confirmation”时,得到以下HTML:

<div class="confirmationinfocontainer" style="display: block;">
    <span class="summarytitle"><p>Order Confirmation</p></span>
    <br>
    <div class="floatrightinline">
        <div class="confirmshippinginfo">
            <p>Shipping to:</p>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
        </div>
    </div>
    <div class="checkoutconfirmationcontainer">
        <div name="product_id"></div>
        <span class="tealmedium">1 - Lakers Hat</span>
        <br>
        <br>
        <br>
        <div class="floatleft">
            <div class="smallerimgcontainer">
                <img alt="Coming Soon" src="/productpics/coming_soon.png" class="sizedimg">
                <br>
                <br>
                <br>
                <br>
            </div>
        </div>
        <div class="checkoutitemsummary">
            <a href="./viewProduct.php?view_product=11">Lakers Hat </a>
            <p><span class="redprice">$25<br></span></p>
        </div>
        <input type="hidden" value="405bb6b2b562b4f00dac620d3f68620f" name="token">
        <input type="submit" value="Place Your Order" class="widebutton">
        <br>
    </div>
</div>

So I see you're already making use of a hidden field. 因此,我看到您已经在使用隐藏字段。 So when a users clicks the "Place Your Order" button, you want the details to be passed along. 因此,当用户单击“下订单”按钮时,您希望传递详细信息。 You can simply add these details back to the form via a hidden field. 您可以简单地通过隐藏字段将这些详细信息添加回表单。 Like so: 像这样:

<div class="checkoutconfirmationcontainer">
<?php
foreach($_SESSION['shopping_cart'] as $id => $product) {
     $product_id = $product['product_id'];
?>
     <input type="hidden" name="product_quantity[<?php echo $id; ?>]" value="<?php echo $product['quantity']; ?>" />
     <span class="tealmedium"><?php echo $product['quantity'] . " - "  . $products[$product_id]['name'] . $message; ?></span><br><br><br>
     <div class="floatleft"><div class="smallerimgcontainer">
     <?php
     $result = mysqli_query($con,"SELECT * FROM products");
     if($row = mysqli_fetch_array($result)) {
          $products[$row['product_id']] = $row;
          if($row['image'] == ""){
               echo "<img class='sizedimg' src='/productpics/coming_soon.png' alt='Coming Soon'>";
          } else {
               echo "<img class='sizedimg' src='/productpics/".$row['img']."' alt='Product Picture'>";
          }
          echo "<br><br><br><br>";
     }
     ?>
     </div></div>
     <div class="checkoutitemsummary">
     <?php echo "<a href='./viewProduct.php?view_product=$id'>{$product['name']} {$products[$product_id]['name']}</a>"; ?>
          <p><span class="redprice"><?php echo '${$products[$product_id]['price']}<br />"; }?></span></p>
     </div>
     <input type="hidden" name="token" value="405bb6b2b562b4f00dac620d3f68620f">
     <input class="widebutton" type="submit" value="Place Your Order">
     <br />
</div>

Looking over this, I see no <form> for this. 看这个,我没有看到<form> So I suspect JQuery is handling this somewhere else. 因此,我怀疑JQuery正在其他地方处理此问题。 So my fix may not really get read if your JQuery or app does not now what to look for, or does not hook onto that hidden field. 因此,如果您的JQuery或应用程序现在没有查找内容或没有连接到该隐藏字段,则可能无法真正读取我的修复程序。 If this is a framework that you're using or Catalog that you're modifying, check their support. 如果这是您正在使用的框架或要修改的目录,请检查其支持。 I would say you have a lot more work ahead of you. 我想说您还有很多工作要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM