For each loop not looping all rows - php

I'm trying to loop all rows of a table name cart but the foreach is only displaying the last row entered and always ignore the previous ones. Say I have 5 products in the cart_table well only product_id[5] will be displayed. If user adds a sixth item, only product_id[6] will now be displayed. The $item_count will also always equal 1 as if there is only one item when there are multiple. To my understanding in the foreach($items as $item) $items is not seen as an array even with multiple items. when I var_dump($items); it shows array(1) { [0]=>...
add_cart.php
<?php
ob_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
$product_id = isset($_POST['product_id'])? sanitize($_POST['product_id']):'';
$size = isset($_POST['size'])? sanitize($_POST['size']):'';
$available = isset($_POST['available'])? sanitize($_POST['available']):'';
$quantity = isset($_POST['quantity'])? sanitize($_POST['quantity']):'';
$item = array();
$item[] = array(
'id' => $product_id,
'size' => $size,
'quantity' => $quantity,
'available' => $available
);
$domain =($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false;
$query = $db->query("SELECT * FROM product WHERE id = '{$product_id}'");
$product = mysqli_fetch_assoc($query);
$_SESSION['success_flash'] = $product['prod_name']. ' was added to your cart.';
//check if the cart cookie exists
if (is_array($cart_id != ' ')) {
$cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$cart = mysqli_fetch_assoc($cartQ);
$previous_items = json_decode($cart['items'],true);
$item_match = 0;
$new_items = array();
foreach($previous_items as $pitem){
if ($item[0]['id'] == $pitem['id'] && $item[0]['size'] == $pitem['size']) {
$pitem['quantity'] = $pitem['quantity'] + $item[0]['quantity'];
if ($pitem['quantity'] > $available) {
$pitem['quantity'] = $available;
}
$item_match = 1;
}
$new_items[] = $pitem;
}
if ($item_match != 1) {
$new_items = array_merge($item,$previous_items);
}
$items_json = json_encode($new_items);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = '{cart_id}'");
setcookie(CART_COOKIE,'',1,"/",$domain,false);
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}else{
// add to databse and set cookie
$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}')");
$cart_id = $db->insert_id;
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}
?>
cart.php
<?php
require_once 'core/init.php';
//include 'includes/headerpartial.php';
if($cart_id != ' ') {
$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
$result = mysqli_fetch_assoc($cartQ);
$items = json_decode($result['items'], true);
$i = 1;
$sub_total = 0;
$item_count = 0;
}
?>
<?php if($cart_id == ' '): ?>
<div class="bg-danger">
<p class='text-center text-danger'>Your cart is empty.</p>
</div>
<?php else: ?>
<?php
foreach ($items as $item) {
var_export($items);
$product_id = $item['id'];
$productQuery = $db->query("SELECT * FROM product WHERE id ='{$product_id}' ");
$product = mysqli_fetch_assoc($productQuery);
$sArray = explode(',', $product['sizes']);
/* foreach ($sArray as $sizeString) {
$s = explode(':', $sizeString);
if($s[0] == $item['size']) {
$available = $s[1];
}
}*/
?>
<tr class="p">
<td class="image"><img src="<?= $product['image_1']; ?>"/></td>
<td class="name"><?= $product['prod_name']; ?></td>
<td class="price"><?= money($product['price']); ?></td>
<td class="quantity"><?= $item['quantity']; ?></td>
<td class="pricesubtotal"><?= money($item['quantity'] * $product['price']); ?></td>
<td class="remove">
<div>&times</div>
</td>
</tr>
<?php
$i ++;
$item_count += $item['quantity'];
$sub_total += ($product['price'] * $item['quantity']);
}
$tax = TAXRATE * $sub_total;
$tax = number_format($tax, 2);
$grand_total = $tax + $sub_total;
<?php endif;?>

Here is one of the problems. You told me that your id is an auto increment int, so I would like to propose this answer. The problem is in your sql command.
UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = {cart_id}
Be sure to put that command in quotes. Also, I highly recommend preparing the JSON with this command:
$itemJson = addslashes($itemJson);
then, you can run the command. Another possibility would be to use the prepare method from mysqli. Here is a link to some examples:
w3schools.com
if you have any further questions, feel free to update your question, but be sure to #McStuffins in the comments.

Related

List shopping cart in phpmailer

I try to send an email with phpmailer after "Place Order".
The problem is that I cannot list the shopping cart as an email (product, quantity, price of each product and total price).
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
This shows me that everything seems to work.
cart.php:
<tr>
<?php
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
</tr>
<tr>
<td>
<img src="<?= $row['photo'] ?>" width="150px"><br />
<?= $row['name'] ?>
</td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td>
<?php echo $_SESSION['qty_array'][$index]; ?>
</td>
<td>
<b><i class="fas fa-dollar-sign"></i> <?php echo number_format($_SESSION['qty_array'][$index]*$row['price'], 2); ?></b>
</td>
<?php $total += $_SESSION['qty_array'][$index]*$row['price']; ?>
</tr>
<?php
$index ++;
}
}
?>
phpmailer.php:
echo var_dump($_SESSION['cart']);
echo var_dump($_SESSION['qty_array']);
foreach($_SESSION['cart'] as $key => $product) {
$name = $product['name'];
$price = $product['price'];
$qty = $product['qty'];
$tprice = $product['totalPrice'];
}
$mail->Body = nl2br("$name\r\n$\r\n$qty\r\n$tprice");
This is not working at all. I tried a few things, and most of what worked was just the listing of the last product on the shopping cart list. But only the ID.
Edit:
I have tried another way, but still only the last item is listed.
phpmailer.php:
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$service = $row['name'];
$qty = $_SESSION['qty_array'][$index];
$qtyPrice = number_format($row['price'], 2);
$qtyTotalprice = number_format($_SESSION['qty_array'][$index]*$row['price'], 2);
$total += $_SESSION['qty_array'][$index]*$row['price'];
$mail->Body = nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n \r\nTOTAL: $ <u>$total</u>");
$index ++;
}
}
I found a solution:
phpmailer.php
...
$mail->Body = nl2br("Hi {$_POST['name']} \r\n");
/// List Cart Item(s) Start
$total = 0;
if(!empty($_SESSION['cart'])){
include 'config.php';
$index = 0;
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM products WHERE id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$index;
$service = $row['name'];
$qty = $_SESSION['cart'][$index];
$qtyPrice = $row['price'];
$qtyTotalprice = number_format($_SESSION['cart'][$index]*$row['price'], 2);
$total += $_SESSION['cart'][$index]*$row['price'];
$mail->Body .= nl2br("$service: ($qty) x ($$qtyPrice) = $$qtyTotalprice \r\n");
$index ++;
}
}
$mail->Body .= nl2br(" \r\nTOTAL: $ <u>$total</u>");
/// Cart Item(s) End
$mail->Body .= nl2br("Kind regards \r\n");
...

Shopping Cart no values

https://kopy.io/6Ud9J https://kopy.io/7tFRb
In first code I have view_cart.php but it doesn't show me the values.
Viewcart.php
<form method="post" action="cart_update.php">
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>pret</th><th>Total</th><th>Remove</th></tr></thead>
<tbody>
<?php
if(isset($_SESSION['prodcos'])) //check session var
{
$total = 0; //set initial total value
$b = 0; //var for zebra stripe table
foreach ($_SESSION['prodcos'] as $cart_itm)
{
var_dump($cart_itm);
//set variables to use in content below
$titlu = $cart_itm['titlu'];
$cantitate = $cart_itm['cantitate'];
$pret = $cart_itm['pret'];
$id = $cart_itm['id'];
$subtotal = ($pret * $cantitate); //calculate pret x Qty
var_dump($titlu);
var_dump($pret);
var_dump($titlu);
echo '<tr>';
echo '<td><input type="text" size="2" maxlength="2" name="cantitate['.$id.']" value="'.$cantitate.'" /></td>';
echo '<td>'.$titlu.'</td>';
echo '<td>'.$pret.$currency.'</td>';
echo '<td>'.$currency.$subtotal.'</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="'.$id.'" /></td>';
echo '</tr>';
$total = ($total + $subtotal); //add subtotal to total var
}
$grand_total = $total + $shipping_cost; //grand total including shipping cost
foreach($taxes as $key => $value){ //list and calculate all taxes in array
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount; //add tax val to grand total
}
$list_tax = '';
foreach($tax_item as $key => $value){ //List all taxes
$list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
}
$shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
}
?>
<tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
<tr><td colspan="5">Add More Items<button type="submit">Update</button></td></tr>
</tbody>
</table>
<input type="hidden" name="return_url" value="<?php
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>
update_cart.php
<?php
session_start();
include_once("includes/config.php");
//add product to session or create new one
if(isset($_POST['type']) && $_POST['type']=='add' && $_POST['cantitate']>0)
{
foreach($_POST as $key => $value){ //add all post vars to new_product array
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
//remove unecessary vars
unset($new_product['type']);
unset($new_product['return_url']);
$id = $_POST['id'];
$sth = $db->prepare("SELECT titlu, pret FROM produse WHERE id = :id");
$sth->bindParam(':id', $id);
$sth->execute();
// $statement = $db->query('SELECT titlu, pret FROM produse WHERE id="$id"');
while($sth->fetch(PDO::FETCH_ASSOC)){
//fetch product name, pret from db and add to new_product array
$new_product['titlu'] = $titlu;
$new_product['pret'] = $pret;
if(isset($_SESSION['prodcos'])){ //if session var already exist
if(isset($_SESSION['prodcos'][$new_product['id']])) //check item exist in products array
{
unset($_SESSION['prodcos'][$new_product['id']]); //unset old array item
}
}
$_SESSION['prodcos'][$new_product['id']] = $new_product; //update or create product session with new item
}
}
//update or remove items
if(isset($_POST['cantitate']) || isset($_POST['remove_code']))
{
//update item quantity in product session
if(isset($_POST['cantitate']) && is_array($_POST['cantitate'])){
foreach($_POST['cantitate'] as $key => $value){
if(is_numeric($value)){
$_SESSION['prodcos'][$key]['cantitate'] = $value;
}
}
}
//remove an item from product session
if(isset($_POST['remove_code']) && is_array($_POST['remove_code'])){
foreach($_POST['remove_code'] as $key){
unset($_SESSION['prodcos'][$key]);
}
}
}
//back to return url
$return_url = (isset($_POST['return_url']))?urldecode($_POST['return_url']):''; //return url
header('Location:'.$return_url);
?>
When I ADD the product, in shopping cart, it doesn't show the details..

Mysql row returns last value in array, but I expect the whole row

Here is my cart code:
function cart() {
$total = 0;
$item_quantity = 0;
$item_name = 0;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if($value > 0 ) {
if(substr($name, 0, 8) == "product_"){
$length = strlen($name - 8);
$id = substr($name, 8 , $length);
$query = query("SELECT * FROM products WHERE product_id = " . escape_string($id)." ");
confirm($query);
$titleArr = array();
while($row = fetch_array($query)) {
$titleArr[$id] = $row['product_title'];
$product_name = implode(",", $titleArr);
$order_product = $product_name . "-". $value;
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$item_number++;
$amount++;
$quantity++;
$id++;
}
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
$_SESSION['item_name'] = $order_product;
}
}
}
}
I searched online for a long time, please advise.
$_SESSION['item_name'] = $order_product;
It gives last value from fetch_array I want all product title on cart page.
Either of these changes should work...
This puts the m all into an array...
$_SESSION['item_name'] = array();
foreach ($_SESSION as $name => $value) {
...
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
array_push($_SESSION['item_name'],$order_product);
...
}
This puts them into a comma separated string...
$_SESSION['item_name'] = '';
foreach ($_SESSION as $name => $value) {
...
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
$_SESSION['item_name'] .= $order_product.', ';
...
}
$_SESSION['item_name'] = rtrim($_SESSION['item_name'],' ');
$_SESSION['item_name'] = rtrim($_SESSION['item_name'],',');
Please make two dimensional Array, if you want to show all product title on cart page.
Replace this
$order_product = $product_name . "-". $value;
with
$order_product[] = $product_name . "-". $value;
Before while loop assign the array:-
$order_product = array();
updated while loop
$total = 0
while($row = fetch_array($query)) {
$titleArr[$id] = $row['product_title'];
$product_name = implode(",", $titleArr);
$order_product = $product_name . "-". $value;
$sub = $row['product_price']*$value;
$item_quantity +=$value;
$item_number++;
$amount++;
$quantity++;
$id++;
$total += $sub;
$_SESSION['item_name'][] = $order_product;
}
$_SESSION['item_total'] = $total;
$_SESSION['item_quantity'] = $item_quantity;

How to compare values in a loop?

I have orders come in. Each order has a type i.e 'Vanilla' and a size i.e 'Mini with a quantity.
If there are 5 Mini Vanillas I want it to display once not 5 times. I also want to tally the quantity.
Here is one of the many things I attempted
$prevSize = null;
$prevType= null;
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
if($prevSize != $currentSize){
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
echo $count;
}
$prevSize = $currentSize;
$prevType = $currentType;}
We have analyzed your query, and according to your need, here is my solution:
$order_type = array();
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
$count = 0;
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
$temp_order_type = $currentSize.'_'.$currentType;
if(!in_array($temp_order_type, $order_type)){
$order_type[] = $temp_order_type;
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
}
echo $count;
}
Above code will display each order type once not display multiple times. And display the quantity ordered in each order.

Using Pagination on product view using Category ID

I feel I have asked alot of questions recently and Im sorry if this is relatively simple but I am struggling to get this working correctly.
I want to add pagination to my product view but when I add the pagination numbers code as seen at the bottom of the page they do not show.
I'm also aware that I should be using mysqli but I want to get this working first before moving over.
Thanks.
Show Product
<div class="link" style="width:100%; height:100%; background-color: white">
<?php
include("../script/dbconnect.php");
include("../script/get_product.php");
$posts = get_posts(null, $_GET['id']);
foreach ( $posts as $post ) {
if ( ! category_exists('name', $post['name']) ) {
$post['name'] = 'Uncategorised';
}
?>
<ul class='featured'>
<li class='headhighlight'><?php echo $post['title']; ?></li>
<li class='pricehigh'><?php echo $post['price']; ?></li>
<li class='imagefeat'><img class='imagelink' src='<?php echo $post['picture']; ?>' alt='$name'></li>
</ul>
<?php
}
?>
</div>
get_product.php
<?php
function get_posts($id = null, $cat_id = null) {
$posts = array();
//Pagination Code
$perpage = 10;
if(isset($_GET["page_num"]))
{
$page_num = intval($_GET["page_num"]);
}
else
{
$page_num = 1;
}
if ($page_num < 1)
{
$page_num = 1;
}
$calc = $perpage * $page_num;
$start = $calc - $perpage;
//End pagination code
$query ="SELECT `products`.`id` AS `name` , `products_cat`.`id` AS `category_id` , `products`.`name` AS `title` , `description` , `price` , `sale` , `picture`
FROM `products`
INNER JOIN `products_cat` ON `products`.`prod_id` = `products_cat`.`id` ";
if ( isset($id) ) {
$id = (int) $id;
$query .= " WHERE `products`.`id` = {$id}";
}
if ( isset($cat_id) ) {
$cat_id = (int) $cat_id;
$query .= " WHERE `products_cat`.`id` = {$cat_id}";
}
$query .= " ORDER BY `products`.`price` DESC Limit $start, $perpage";
$query = mysql_query($query);
echo mysql_error();
while ( $row = mysql_fetch_assoc($query) ) {
$posts[] = $row;
}
return $posts;
}
Pagination Code - to add page numbers - would be placed in showproduct.php
<p class="pagination">
<?php
if(isset($page_num))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page_num <=1 )
{
echo '<span id="page_links" style="font-weight:bold;"> < </span>';
}
else
{
$j = $page_num - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page_num=' . $j . '"> < </a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page_num)
{
echo '<span>' . $i . '</span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page_num == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page_num + 1;
echo '<span> > </span>';
}
}
?>
</p>
$posts = get_posts(null, $_GET['id']);
$result = mysql_query($posts);
In the get_posts function you declare this variable as an array, fill it from a query result and then you return it... so it will never work. What were you trying to do here? You have to pass a string to the mysql_query function with correct MySQL query structure.
EDIT: adding the part of the code that is giving problems, and a possible fix.
$posts = get_posts(null, $_GET['id']);
$i = 0;
foreach ($posts as $post){
....
does it work this way? You already did the query with the LIMIT inside the get_posts function and returned the data.
$posts = get_posts(null, $_GET['id']);
returns array, of something. After it you trying to make mysql_query(array); Try to make
var_dump($posts); and copy print here. Or try next:
$posts = get_posts(null, $_GET['id']);
foreach($posts as $post){
$result[] = mysql_query($posts);
}

Categories