James February 2016

Cannot use string offset as an array - think need to unserialize in foreach maybe?

hope someone could help me out as getting to the end of my tether with it, im new to PHP so struggling some what.

Error in logs:

[Mon Feb 08 22:16:08 2016] [error] [client 2.125.15.96] PHP Fatal error: Cannot use string offset as an array in /home/ambcom/public_html/staging/beanbags/app/addons/abandoned_cart_extended/controllers/backend/ac.php on line 43, referer: /admin.php?dispatch=cart.cart_list

Which i think is $product['product_id']

When i do a print_r on the $products i get this this console: http://pastebin.com/QuTJzMX8

Its the cart data i need which i think if im not mistaken is serialized? I need to be able to use the data i get back from the query and loop through the items. The data in the database field is stored like it returns a:2:{i:1217356819;a:24:{s:10:\"product_id\";i: etc...

PHP

$acId = $_REQUEST['id'];

$products = db_get_fields("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
fn_print_r($products);
//$products = unserialize($products);

if(count($products)>0){

  $shippingCost = db_get_field("SELECT shipping FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
  $tax = db_get_field("SELECT tax FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
  $orderTotal = db_get_field("SELECT order_total FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
  $email = db_get_field("SELECT email FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
  $firstName = db_get_field("SELECT first_name FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
  $LastName = db_get_field("SELECT last_name FROM ?:abandoned_cart WHERE user_id = ?s", $acId);


  $sum=0;
  //echo $products;
  if (!empty($products)) {

    foreach ($products as $product) {
      $text .='
        <tr>
          <td><a  href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id']        

Answers


trincot February 2016

Your $products variable, as presented on pastebin.com has this structure:

array(
    array(
        "serialised data"
    )
)

So, to get to the inner data, add this statement just after you have checked that $products is not empty:

$products = unserialize($products[0][0]);

If I print_r($products) after that, I get:

Array
(
    [1217356819] => Array
        (
            [product_id] => 372
            [product_code] => 16002
            [product] => Acoustic Sofa - Eco Weave
            [amount] => 1
            [product_options] => Array
                (
                    [365] => 732
                )

            [price] => 249
            [stored_price] => N
            [main_pair] => Array
                (
                    [pair_id] => 417
                    [image_id] => 0
                    [detailed_id] => 1513
                    [position] => 0
                    [detailed] => Array
                        (
                            [image_path] => http://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
                            [alt] => 
                            [image_x] => 1500
                            [image_y] => 1500
                            [http_image_path] => http://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
                            [https_image_path] => https://beanbags.ambientlounge.com/images/detailed/1/Acoustic-eco-weave.jpg?t=1454562784
                            [absolute_path] => /home/ambcom/public_html/staging/beanbags/images/detailed/1/Acoustic-eco-weave.jpg
                            [relative_path] => detailed/1/Acoustic-eco-weave.jpg
                        )

                )

            [extra 

Post Status

Asked in February 2016
Viewed 3,278 times
Voted 10
Answered 1 times

Search




Leave an answer