I get this error when I press search "Invalid argument supplied for foreach() in"

<?php
namespace Cart;
class Cart {

	// nico persistent cart
	function updatePersistentCart() {
			$cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
			
			$persistent_cart = array();
			if ($cart_query->rows)
			{
				foreach ($cart_query->rows as $cart) 
				{
					$product['product_id'] = $cart['product_id'];
					$product['quantity'] = $cart['quantity'];
					//$product['option'] = json_decode($cart['option']);
					$product['option'] = $cart['option'];
					$product['recurring_id'] = $cart['recurring_id'];
					
					$persistent_cart[] = $product;
				}
			}

			setcookie('persistent_cart', json_encode($persistent_cart),  strtotime( '+1 year' ), '/');
	}

$cart_query->rows is not an array. foreach loops should only use arrays or objects. It cannot use a string or integer.

Also, you are prone to SQL Injections. You should be using prepared statements.

so ı understand it ı must ($object)
{
foreach ($object as $cart)

In a way yes. But if you are just replacing $object over what you initially had, it still will throw you an error. Variables mean nothing really. It HAS to be an object. So say if I use the variable $object, but threw something like this together.

$object = 'My final string';

Then it really makes no difference at all compared to what you had before. You’ll still get the same error because you don’t understand what an object is. But if you had an array or object passed as a variable as such.

$object = new stdClass();

$object->name = 'Spaceship';
$object->php_user = 'Yes';
...

foreach($object as $cart) {
    ...
}

Then it would work because we are passing an object into the foreach loop. You can even use arrays in foreach loops if you want instead of using objects. strings and integers cannot go in there. So blindly misrepresenting the variable would really be of no real use to you at all.

// nico persistent cart
function updatePersistentCart() {
		$cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
		
		$object = new stdClass();

                      $object->name = 'Spaceship';
                      $object->php_user = 'Yes';
		{
			foreach($object as $cart) 
			{
				$product['product_id'] = $cart['product_id'];
				$product['quantity'] = $cart['quantity'];
				//$product['option'] = json_decode($cart['option']);
				$product['option'] = $cart['option'];
				$product['recurring_id'] = $cart['recurring_id'];
				
				$persistent_cart[] = $product;
			}

I made the file like this, but I got the same error

No, that’s because you don’t use my sample demo. You use your own and reference my demo to better your understanding. The basic point here is that you are running a query and you aren’t really getting any results from what you have queried. All you did was use ->rows which is an invalid object in the first place because you didn’t create the object. And using my sample demo in your snippet is completely wrong since it’s a demo to help you understand that foreach loops use either objects or arrays.

can you edit my cart.php file

Again. You aren’t listening. Whatever I showed you above, it’s a sample demo for you to look at. Not copy&paste to have it working. You are getting the same errors because I didn’t complete the sample demo for a reason. If you don’t understand how something works and just blindly copy&paste something, how would you learn by just copying&pasting the final code?

can you edit file foreach

I will not do your work for you. I am merely pushing you along the right path. If you want me to do your work for you, then hire me. Otherwise, read what I have said above and apply it to what you currently have.

ok thank you

I actually don’t understand what you are trying to achieve here when I look at your first snippet. You have an update function and all you do in the update function is grab some data, assume it’s safe data to use whether you get SQL Injected or not, throw it in a foreach loop, create a multidimensional array that looks to be either 2 or 3 layers deep, then you set a regular cookie and append the data to that cookie. I don’t see where you’re actually updating any data or did I miss something?

ı want to when ı press search which I choose catagory want to see the products in that category

Then all you have to do is return all the products in that category using the product ID. I have no idea why you are jumping through so many hoops and sliding off the main focus and using all these random functions that really have nothing to do with what you are trying to achieve.

$product ID = array();
if ($product ID)
{
foreach (product ID as $cart)

ı try thi but get same erorr

That’s because

foreach(prodcut ID

is still a string. In fact, it’s 2 constants. Looking at this, it looks like it’s from OpenCart from github.

You NEED to pass the category ID into your query. From there, you just basically need to return or print the results.

There should really be no need for the foreach loop unless you want to use objects as your returned value. If you use associative arrays, you can reference them by using their index key.

okey thank you so much

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.