简体   繁体   中英

foreach loop results to INSERT into database

I have three different div's that contain the checkout information.

  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.

I am trying to figure out how I am going to INSERT the string that displays from my foreach loop into my database. 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. If the result of my foreach loop was:

2 balls

4 shoes.

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?

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:

$('.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:

<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. So I suspect JQuery is handling this somewhere else. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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