How do I fix this fatal error in OpenCart? - php

I am using CSV Import Pro to import products into my store using OpenCart.
The other day I was importing products which was going fine. Then after an import the extension keeps giving me this fatal error.
I've tried contacting support for damn near 5 days and I haven't gotten much from them. I really need to get this fixed.
Fatal error: Cannot use object of type stdClass as array in /home/content/71/11151671/html/admin/view/template/tool/csv_import.tpl on line 234
Usually there are tabs at the top that let me navigate to the other pieces of the module.
It would not let me post images

With the CSV Import Pro you want to go to the admin/controller/tool/csv_import.php
Search for this line of code:
$this->data[$key] = json_decode($this->data[$key];
and replace it with:
$this->data[$key] = json_decode($this->data[$key], true);
By adding the true at the end, you are basically telling the script you want your data to be in an array format, rather than an object.
Hope this helps ;)
Peter

Without code sample it's really just guessing, but my first impression is that you're trying to access some members of an object that was processed with json_decode. This will, by default, turn arrays into objects, to be more exact instances of PHP's StdClass. Either try accessing the members using object notation ($obj->member) or use the 2nd optional parameter to json_decode in which case the returned will be an associative array instead of an object.
See PHP doc for json_decode.

If you split a JSON-object, try to add a second parameter to the function json_decode:
$json = json_decode($string, true);

Related

I need to change the php code in the file so instead of the current json output

So i am using this PHP code to create the json output, and I am having an issue where it’s creating an array of array with the info. I would like to get rid of one array and just display the list of API’s thats been used and number that has been used.
Looks as though the difference is you have...
"apis":[{"item_search":"0\n"},{"item_recommended":"0\n"}]
and want
"apis":{"item_search":"0\n","item_recommended":"0\n"}
If this is the case, you need to change the way you build the data from just adding new objects each time to setting the key values directly in the array...
$zone_1 = [];
foreach($zone_1_apis as $api_name ) {
$zone_1[substr($api_name, 0,-5)] = file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name);
}
You also need to do the same for $zone_2 as well.
It may also be good to use trim() round some of the values as they also seem to contain \n characters, so perhaps...
trim(file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name))

Symfony 2 dump on object

when using the dump utility of symfony, there is a nice reference number that identifies the object such as object: Doctrine\ORM\PersistentCollection {#3491, is there any way that one can get this value(3491) without without using the dump function. That is some_function($object) and then get the same number that symfony dump function would return.
Thanks
I believe it is the spl_object_hash() value.

How to return an array instead of object in JSON response?

I've one working REST API developed using Slim PHP framework.
It's working absolutely fine.
The only issue is when there is no error present i.e. an array $errors is empty it comes as an array in JSON response but when the array $errors contains any error like $errors['user_name'] then it comes as an object in JSON response.
Actually I want to return the array when error is present. How should I do this? Can someone please help me in this regard?
Thanks in advance.
When your $errors is not empty, pass it through json_encode and echo it.
It will give you JSON object in return,
then convert JSON object into JavaScript array. (see the following code.)
var o = {"0":"1","1":"2","2":"3","3":"4"}; // your response object here
var arr = Object.keys(o).map(function(k) { return o[k] });
console.log(arr);
Recently got a close issue with Symfony's JsonResponse::create()
Turns out arrays with index not starting at 0 will be encoded into objects, as well as arrays with "holes", and probably any array with at least one non-int key.
In other word, arrays with anything else than consecutive numeric keys starting from index 0 seem to be encoded as object.
I guess this is designed to avoid sending big empty arrays when you map a handful of datas with big indices like [14334, 839493, 246193], and is probably documented somewhere.
Learning if this is a Symfony of json_encode behavior from comments would be welcomed addition :)
Note : Even if you return an array, it seems necessary to wrap it in an object for GET request to prevent some XSSI and JSON-JavaScript Hijacking.

Convert key-value JSON to object and refer individual values directly

I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.

Accessing numerical property in a json object

I have a PHP multidimensional array witch i converted to JSON using JSON_encode().
I am using drupal so for those not familiar with it, drupal array often have keys that looks like that :
$some_array['und']['0']['value']
So my JSON object ends up looking like:
some_array.und.0.value
Now the problem is that when use the above syntaxe to retreive the value i the following JS error in the FB console : "missing name after . operator"
Also this data is meant to be used with a Jquery template, so i alos tried accessing this data directly in my template with:
${field_city.und.0.value}
Witch unfortunately didn't work either..
How would i go to fix that issue ? Can i access somehow this proprety with JS? Or is there a way that i have JSON_encode function replace all 0 by let's say "zero"? Or even replacing '0' when parsing the JSON string?
Suggestions much appreicated.
Try accessing it with some_array.und["0"].value. '0' is not a valid name for a javascript object, which is why accessing it via the . notation is not working.
However, if you access it via the square brackets, you can access keys with any name at all.
As well as using the dot notation, you can use regular array notation to access JSON nodes:
some_array.und['0'].value

Categories