tags:

views:

88

answers:

4

I get the following error.

Fatal error: Cannot increment/decrement overloaded objects nor string offsets

Here is the code.

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC");

if (!$dbc) {
 // There was an error...do something about it here...
 print mysqli_error();
}

while ($obj = mysqli_fetch_assoc($dbc)) {
 if (!empty($obj['parent_id']) == 0) {
  echo $parent_menu . $obj['id']['label'] = $obj['label'];
  echo $parent_menu . $obj['id']['link'] = $obj['link_url'];
 } else {
  echo $sub_menu . $obj['id']['parent'] = $obj['parent_id'];
  echo $sub_menu . $obj['id']['label'] = $obj['label'];
  echo $sub_menu . $obj['id']['link'] = $obj['link_url'];
  echo $parent_menu . $obj['parent_id']['count']++;
 }
}
mysql_free_result($dbc);
A: 
                echo $parent_menu . $obj['parent_id']['count']++;

Just move the increment step to a new line.

Platinum Azure
No. `++` has higher precedence than `.`, so it is evaluated first. See http://www.php.net/manual/en/language.operators.precedence.php
Ferdinand Beyer
Not at all. `$a = 0; $b = "foo" . $a++; echo $a, $b;` will print `1foo0`. There is nothing wrong with that. The problem is that `$obj['parent_id']` is not an array but a string, so the whole expresssion is wrong, whether it is written on a separate line or not.
Ferdinand Beyer
+1  A: 

Your $obj['parent_id'] is a string (containing a number, e.g. "0") - it's not an array with a count entry. This would make more sense:

$obj['parent_id']++

Also on a side note, this is awfully confusing:

if (!empty($obj['parent_id']) == 0) {

This would make more sense:

if (empty($obj['parent_id'])) {
Greg
if the ID is numeric when valid, it would probably be better to use is_numeric. empty returns true when evaluating a 0 so you could run into problems
iddqd
A: 

I think that your code is a bit of mess!

//What did you think here? (!empty() == 0)
while ($obj = mysqli_fetch_assoc($dbc)) {
        if (!empty($obj['parent_id']) == 0) {
                // Echo with assignment trying to save variables?
                echo $parent_menu . $obj['id']['label'] = $obj['label'];
                echo $parent_menu . $obj['id']['link'] = $obj['link_url'];
        } else {
                echo $sub_menu . $obj['id']['parent'] = $obj['parent_id'];
                echo $sub_menu . $obj['id']['label'] = $obj['label'];
                echo $sub_menu . $obj['id']['link'] = $obj['link_url'];
                echo $parent_menu . $obj['parent_id']['count']++;
        }
}

I am not judging your style, but I think that if you change your code style it will be much easier for you to track errors.

The problem is at $obj['parent_id']['count']++; This is not a number and cannot use increment. $obj['parent_id'] is not even an array that has 'count' element. Moreover if you want to create a counter this will probably be deleted in the next loop as you save it on $obj variable which will be overwritten.

I think you need something like this, or not. Can't figure out what exactly criteria you want at the if clause. Or what exactly the counter wants to count? The sum of parent_ids or the quantity of parent_id.

$counter = 0;
while ($obj = mysqli_fetch_assoc($dbc)) {
        if (empty($obj['parent_id'])) {
                echo $parent_menu . $obj['label'];
                echo $parent_menu . $obj['link_url'];
        } else {
                echo $sub_menu . $obj['parent_id'];
                echo $sub_menu . $obj['label'];
                echo $sub_menu . $obj['link_url'];
                echo $parent_menu . ($counter++);
        }
}

Hope it helped

+1  A: 

Greg pointed out your problem: You are trying to increment a string offset count of a string value:

echo $parent_menu . $obj['parent_id']['count']++;

Here is what PHP does:

(1) Evaluate $obj['parent_id'], resulting in a string.

(2) Trying to get the offet "count" from this string. Here, a major flaw in PHP occurs: Since strings have offsets that can be accessed using the bracket notation in order to extract single characters, PHP will silently convert "count" to an integer. Therefore, it will treat the string as 1: $obj['parent_id'][1].

(3) Trying to increment a string offset $string[1]++ will result in your fatal error:

PHP Fatal error: Cannot increment/decrement overloaded objects nor string offsets in ... on line ...

PHP will also point you to the location of your error, namely the file name and line number. You omitted that part from your problem description. Next time please include this information by telling us which line in your code snippet is the source of the error!

Ferdinand Beyer