5 replies [Last post]
mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 7 years 16 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Hey All,

Posting here after a real long time Smile its been quite busy and i will write a separate post about whats new! anyways, back to the current topic.

I have developed a shopping cart for on of my custom scripts that i am writing. The cart is working perfectly (like it should), but when i submit the cart, and try to use the foreach loop to insert the products in the orders table, the array output numbers in descending order - one for each sub-array. Below, i have mentioned the php array, and the resultant foreach loop. I have never seen this happen, so am a bit lost.

Array:

Array ( [clientnumber] => 4 [products_17] => Array ( [name] => 2.7 [cost] => 0 [quantity] => 1 [typeid] => 17 ) [qty17] => 1 [products_20] => Array ( [name] => 3.0 [cost] => 0 [quantity] => 1 [typeid] => 20 ) [qty20] => 1 [total] => 0 [savecart] => savecart )

Resultant Foreach Loop :

Name:2.7
Cost:0
Quantity:1
type id:17
-----------------why is this in the result?-------------
Name:1
Cost:1
Quantity:1
type id:1
-----------------The latter part is correct again-------------
Name:3.0
Cost:0
Quantity:1
type id:20

If there are 5 products, then there is the numeric result 4 times!

Why? Why? Why?

Regards,
Mihir.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 5 weeks 2 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

Hi mihirc, You must be

Hi mihirc,
You must be accessing the sub array incorrectly.
If you show the PHP foreach loop you are using we might be able to help.

mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 7 years 16 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Hello Tony,Here is the

Hello Tony,

Here is the complete "cart.php" file that I have created. This is the complete code, so please forgive if its too big Smile

// using slice to cut off the rest of the array, to get the list of products. removed details are clientname, total & submit button value (which is used to decide whether we are updating the cart, or submitting it.).
$output = array_slice($_POST, 1, -3);
 
	$products = $output;
	// This query will get the latest orderid first.
		$getorderid = mysql_query("SELECT orderid FROM orders ORDER BY orderid DESC LIMIT 1") or die (mysql_error());
		while ($orderid = mysql_fetch_array($getorderid)) { 
		$rowid = $orderid['orderid'];
		echo'this is the row id:'.$rowid;
		$newrowid = ($rowid + 1);
 
		echo "this will be the order id:".$newrowid;
		$todaysdate = date('Y-m-d');
		};
        // foreach query to get the get product and insert in orders table. Also used to later reduce the available stock.
	foreach ($products as $product) {
 
	echo '<br />Name:'.$product['name'];	
	echo '<br />Cost:'.$product['cost'];
	echo '<br />Quantity:'.$product['quantity'];
	echo '<br />type id:'.$product['typeid'];
	$insertquery = mysql_query("INSERT INTO orders (orderid,clientid,typeid,quantity,date,storeid) VALUES ('".$newrowid."','".$clientid."','".$product['typeid']."', '".$product['quantity']."', '".$todaysdate."', '".$my_contact->store_info($storeid)."')") or die(mysql_error());
 
	if($insertquery) {
		echo "product inserted successfully";
 
		}
 
		$remainingstock = mysql_query("SELECT quantity FROM stocks WHERE typeid = '".$product['typeid']."' AND storeid = '".$my_contact->store_info($storeid)."'") or die(mysql_error());
			while ($quant = mysql_fetch_array($remainingstock)) {
				$remainingquantity = $quant['quantity'] - $product['quantity'];
 
				$updatequery = mysql_query("UPDATE stocks SET quantity = '".$remainingquantity."' WHERE typeid = '".$product['typeid']."' AND storeid = '".$my_contact->store_info($storeid)."'") or die(mysql_error);
				if ($updatequery) {
 
					echo "stocks updated successfully!";
									};	
 
				};	
 
	}
	// inserting the total value into the bills table.
	$insertbill = mysql_query("INSERT INTO bills (orderid,totalamt) VALUES('".$newrowid."', '".$totalamt."')");
	if ($insertbill){
 
		echo "bill generated successfully"; };<php>
 
Regards,
Mihir.
 
// edit - added all the code & moved it to php tags.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 5 weeks 2 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

I think it is due to the

I think it is due to the structure of the array. [qty17] => 1 is outside the product yet you seem to e expecting each item at that level to be an array not an int or string.
Why don't you check that the product you have is actually an array, something like:

foreach ($products as $product) {
  if (is_array($product) ) {

mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 7 years 16 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Tony wrote: I think it is due

Tony wrote:

I think it is due to the structure of the array. [qty17] => 1 is outside the product yet you seem to e expecting each item at that level to be an array not an int or string.
Why don't you check that the product you have is actually an array, something like:

foreach ($products as $product) {
  if (is_array($product) ) {

Hey Tony,
Yup, I agree that is the problem! But how do i then skip inserting that qty17 part of the array? I use that qty17 part while running the update cart, query, so removing it (unfortunately) is not an option.
Thanks!

Regards,
Mihir.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com

mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 7 years 16 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Tony wrote: I think it is due

Tony wrote:

I think it is due to the structure of the array. [qty17] => 1 is outside the product yet you seem to e expecting each item at that level to be an array not an int or string.
Why don't you check that the product you have is actually an array, something like:

foreach ($products as $product) {
  if (is_array($product) ) {

That solved it Tony! and a new thing learned!

Thank you!
Regards,
Mihir.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com