Jump to content

Diether

Members
  • Posts

    41
  • Joined

  • Last visited

Diether's Achievements

Member

Member (2/5)

0

Reputation

  1. Hi guys Good day, how to insert multiple data in the database? when i insert data that has multiple orders, only one item is inserted in the database. pls help me to put this on loop. Heres the code i use: foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } Heres what i did but it gives me syntax error, pls help me to do the proper looping for this. thanks foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $item_id_count = count($item_id) ; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; foreach($i=0,$i < $item_id_count,$i++){ mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } }
  2. array masters.. please help me to solve my problem
  3. <?php ob_start(); ?> <?php session_start();?> <?php include("includes/functions.php");?> <?php error_rep(); ?> <?php require_once("includes/connection.php");?> <?php //Section 1 (if user attempts to add something to the cart from the product page) if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } redirect_to("cart.php"); exit(); } ?> <?php // Section 2 (if user chooses to empty their shopping cart) if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php // Section 3 (if user chooses to adjust item quantity) if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php // Section 4 (if user wants to remove an item from cart) if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php // Section 5 (render the cart for the user to view on the page) $pp_checkout_btn =""; $cartOutput = ""; $cartTotal = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["name"]; $price = $row["price"]; $details = $row["details"]; } //$_SESSION["cart_array"][0]['pricetotal'] = $pricetotal = $price * $each_item['quantity']; $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; echo $price ."<br/>"; echo $pricetotal ; $pricetotal = number_format($pricetotal,2); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /> <img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>Php ' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="Update" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /> <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; // $cartOutput .= '<td>' . $size . '</td>'; //experiment $cartOutput .= '</tr>'; $i++; } $productItems = $_SESSION["cart_array"] ; // foreach($productItems as $key => $productItem){ $newIndex = $key + 1; } $productItems[$newIndex]['pricetotal'] = $cartTotal ; // $cartTotal = number_format($cartTotal,2); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." Php</div>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Your Cart</title> </head> <body> <div id="wrapper"> <?php include("includes/header.php"); ?> <div id="mainMenu"> <!-- Start css3menu.com BODY section id=1 --> <ul id="css3menu1" class="topmenu"> <li class="topfirst"><a href="#" style="height:18px;line-height:18px;">Dashboard</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">order</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">Customer</a></li> <li class="topmenu"><a href="#" style="height:18px;line-height:18px;">Inventory</a></li> <li class="toplast"><a href="#" style="height:18px;line-height:18px;">Report</a></li> </ul> <p style="display:none"><a href="http://css3menu.com/">Easy CSS Drop Down Menu Css3Menu.com</a></p> <!-- End css3menu.com BODY section --> </div> <!-- end of mainmenu --> <div id="Buttons"> <a href = "index.php">Continue shopping </a><br/> <a href = "delivery_details.php">Checkout </a> </div> <!--buttons --> <div id="pageContent" style="margin:24px; text-align:left;"> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="6"> <tr> <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td> <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td> <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td> <td width="45%" bgcolor="#C5DFFA"><strong>size</strong></td> </tr> <?php echo $cartOutput; ?> </table> <?php echo $cartTotal; ?> <br /> <br /> <br /> <br /> <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a> </div><!-- End of pageContent --> <br /> <?php echo "<br/>"; // $_SESSION['newProductItems'] = $productItems; foreach($productItems as $key => $item) { if(isset($result[$key])) { $productItems[$key]['price'] = $result[$key]; } } echo "<pre>"; print_r($productItems); echo "</pre>"; ?> <?php include("includes/footer.php"); ?> <?php ob_flush(); ?> </div> <!--end of mainWrapper --> </body> </html> Here is the cart.php code.. the array is came from a session variable..
  4. Hi guys good day, im a student and i want to practice my php skills, i have a big problem in arrays that i cant solve almost a week so i decided to attach the php files as well as its database.pls help me so that i can study this later. i'm really having a hard time in arrays. please help me so that i can move on. There are 3 most important forms in my php files. 1. index.php 2.product.php 3. cart.php Here is my screenshot of what i want to do in cart.php. i want to include the unit price and the sumofprice in my array as indicated in my screenshot so that i can include this information in a session variable later on. My screenshot: My phpfiles : http://www.mediafire.com/?od2jdnd71v5xvud
  5. hi Guys Good day.. i have this two array that i want to combine: 1st array: when using print_r($productItems) Here is the result: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ) ) 2nd array: When using print_r($prices) Here is the result: Array ( [price] => 3,200.00 ) Array ( [price] => 509.00 ) By using the array_merge, i came up with a solution: Here is what i did: $result = array_merge($productItems, $prices); print_r($result); Here is the output: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ) [price] => 509.00 ) but thats not the result i want to get : this is my expected output: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 [price] => 3,200.00 ) [1] => Array ( [item_id] => 15 [quantity] => 1 [price] => 509.00 ) [2] => Array ( [pricetotal] => 3709 ) ) please help me to do this,, im stuck for almost 3 hours already thats why i seek for help. thanks
  6. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "<tr><td>$id</td> <td><strong>$product_name</strong></td> <td>Php$price</td> <td><em>$date_added</em></td> <td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>"; } } else { $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>"; } ?> this solve my problem a while ago.. hope it will help a beginner like me
  7. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>"; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "<tr><td>$id</td> <td><strong>$product_name</strong></td> <td>Php$price</td> <td><em>$date_added</em></td> <td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>"; } } else { $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>"; } ?> this solves my problem a while ago. hope it can help beginners
  8. what line are you pertaining that it gives me this kind of result?
  9. this code does not follow my desired output. any solution guys.? please help me to solve this
  10. Hi guys Good day, this codes works perfectly for me. <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM product ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["name"]; $details = $row["details"]; $size = $row["size"]; $price = $row["price"]; $quantity = $row["quantity"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } ?> My problem is i want to refactor this part and put it into table in order to give a pleasant look: $product_list .= "Product ID: $id - <strong>$product_name</strong> - Php$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; My Screenshots for my desired output. Here is what i did to refactor but it did not give me a good result: $product_list .= "?> <html> <table > <tr> <td> Product ID: <?php echo $id ?> </td> <td> <?php echo $product_name; ?> </td> <td> Php <?php echo $price; ?> </td> <td> Added <?php echo $date_added; ?> </td> <td> <?php echo <a href='inventory_edit.php?pid=$id'>edit</a> • ?> </td> <td> <?php echo <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; ?> </td> </tr> </table> pls help me to solve my desired output,, thanks
  11. hi Guys, Pls give me an example code so that $pricetotal will not be null? just an example code so that i will have the idea on how to fix that. thanks
  12. Hi guys, this codes works perfectly to me, except that i would like to add pricetotal in session variable. my problem is pricetotal does not display values when i use print_r. Heres the output when i use print_r to display session variable: Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => ) ) It should be like this: Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => 5500) ) please help me to fix my problem: thanks the code use: <?php //Section 1 (if user attempts to add something to the cart from the product page) if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1,"pricetotal" => $pricetotal)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1,"pricetotal" => $pricetotal))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "pricetotal" => $pricetotal)); } } redirect_to("cart.php"); exit(); } ?> <?php // Section 2 (if user chooses to empty their shopping cart) if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php // Section 3 (if user chooses to adjust item quantity) if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity,"pricetotal" => $pricetotal))); } // close if condition } // close while loop } // close foreach loop } ?> <?php // Section 4 (if user wants to remove an item from cart) if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php // Section 5 (render the cart for the user to view on the page) $pp_checkout_btn =""; $cartOutput = ""; $cartTotal = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["name"]; $price = $row["price"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; $pricetotal = number_format($pricetotal,2); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /> <img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>Php ' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="Update" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /> <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } $cartTotal = number_format($cartTotal,2); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." Php</div>"; } ?>
  13. pls help me to fix my codes, it took me two hours already and i can't move on
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.