0

Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\includes\categories\categories.php on line 12

$categories[$parent][] = $row;

categories.php

    <?php

$sql = "SELECT catid, catname, parentid FROM categories";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
    $parent = intval($row['parentid']);
    if (!isset($categories[$parent])) {
        $categories[$parent] = array();
    }
    $categories[$parent][] = $row;
}
    ?>
    <table border="0" cellpadding="10" cellspacing="0">
    <tr>
        <td valign="top">
    <?php
    $category_string = "";
    function build_categories_options($parent, $categories, $level) {
        global $category_string;
        if (isset($categories[$parent]) && count($categories[$parent])) {
            $level .= " - ";
            foreach ($categories[$parent] as $category) {
                $opt_value = substr($level.$category['catname'],3);
                $category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
                build_categories_options($category['catid'], $categories, $level);
            }
            $level = substr($level, -3);
        }
        return $category_string;
    }
    $category_options = build_categories_options(0, $categories, '');
    $category_options = '<select class="chosen" name="categories" id="categories">'.$category_options.'</select>';
    echo $category_options; 
    ?>
</td>

After I insert post With category This Error Will Show ??

5
  • can you point out line 12 please Commented Sep 27, 2012 at 19:17
  • What is $categories? I bet it's a string...or $categories[$parent] is a string. Commented Sep 27, 2012 at 19:17
  • im thinking the same. stackoverflow.com/questions/2058635/… Commented Sep 27, 2012 at 19:19
  • @Flosculus Thanks Man My Problem Is Fixed Commented Sep 27, 2012 at 19:42
  • @Travesty3 I need Your Help !! Please stackoverflow.com/questions/12650424/… my code working done but i need to use in this line $category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>'; in this Option <option something like that value="$_get['catid']">$_get['catname']</option> like this to display catname from table ? Commented Sep 29, 2012 at 16:09

1 Answer 1

3

I don't see where $categories is initialized, but I'm betting that it's not an array when you enter your while loop, and that's why you're getting an error. Try doing this for your while loop:

// initialize $categories to make sure it is an array
$categories = array();
while ($row = mysql_fetch_assoc($res)) {
    $parent = intval($row['parentid']);
    $categories[$parent][] = $row;
}

You don't need to explicitly initialize $categories[$parent]...this will be done automatically when you call $categories[$parent][] = $row;. We know that it will start out blank because we started with an empty array before the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.