Jump to content

[Help]problem with arrays


Diether

Recommended Posts

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:

1743411776718c5675e145f399dc8b804648c222
My phpfiles :
http://www.mediafire.com/?od2jdnd71v5xvud

 

Link to comment
Share on other sites

<?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..

Edited by Diether
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.