[英]Fatal error: Cannot use string offset as an array
Fatal error: Cannot use string offset as an array in C:\\xampp\\htdocs\\includes\\categories\\categories.php on line 12 致命错误:无法在第12行的C:\\ xampp \\ htdocs \\ includes \\ categories \\ categories.php中将字符串偏移量用作数组
$categories[$parent][] = $row;
categories.php 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 ?? 在我插入带有类别的帖子后,将显示此错误?
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. 我看不到$categories
在哪里初始化,但是我敢打赌,当您进入while
循环时,它不是一个数组,这就是为什么会出错的原因。 Try doing this for your while
loop: 尝试为您的while
循环这样做:
// 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;
您不需要显式初始化$categories[$parent]
...这将在您调用$categories[$parent][] = $row;
时自动完成$categories[$parent][] = $row;
. 。 We know that it will start out blank because we started with an empty array before the loop. 我们知道它将开始为空,因为我们在循环之前从一个空数组开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.