PHP和Smarty错误:试图获取非对象的属性

时间:2011-01-08 22:15:52

标签: php mysql smarty

我是这个网站的新手并且学习PHP。我正在使用Darie和Bucica的文本Beginning PHP5和MySQL电子商务从新手到专业来创建一个电子商务网站。我相信到目前为止我遇到的一些错误是由于更新的数据库(MDB2)。除了这个,我已经能够克服每一个错误。该代码应该使用Smarty从我的数据库中提取部门列表。

我在最后一行收到错误“试图获取非对象的属性”。我觉得它与is_array()函数有关。

<?php $_smarty_tpl->tpl_vars["load_departments_list"] = new Smarty_variable("departments_list", null, null);?>
    <table border="0" cellpadding="0" cellspacing="1" width="200">
     <tr>
      <td class="DepartmentListHead"> Choose a Sport </td>
     </tr>
     <tr>
      <td class="DepartmentListContent">
       <?php unset($_smarty_tpl->tpl_vars['smarty']->value['section']['i']);
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['name'] = 'i';
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['loop'] = is_array($_loop=$_smarty_tpl->getVariable('departments_list')->value->mDepartments) ? count($_loop) : max(0, (int)$_loop); unset($_loop);

如果您还需要其他任何帮助,请告诉我们!请尽可能描述,并尽可能使用我的代码显示解决方案。谢谢你的帮助! -DREW

2 个答案:

答案 0 :(得分:4)

您在$_smarty_tpl->getVariable('departments_list')->value->mDepartments功能中使用in_array。确保您已将departments_list分配给智能对象。

或在此之前添加支票

$departments_list = $_smarty_tpl->getVariable('departments_list');
if (is_object($departments_list) && is_object($departments_list->value)
         &&  $departments_list->value->mDepartments) {
    $_smarty_tpl->tpl_vars['smarty']->value['section']['i']['loop'] = is_array($_loop=$_smarty_tpl->getVariable('departments_list')->value->mDepartments) ? count($_loop) : max(0, (int)$_loop); unset($_loop);
}

答案 1 :(得分:0)

尝试使用var_dump()检查每个变量的类型:

var_dump($_smarty_tpl->getVariable('departments_list'), $_smarty_tpl->getVariable('departments_list'))->value,
$_smarty_tpl->getVariable('departments_list'))->value->mDepartments);

这会告诉你什么类型的值。这个问题实际上并不在于is_array函数,但事实上,在$_smarty_tpl->getVariable('departments_list'))->value->mDepartments中,您试图在两种情况下访问对象属性,返回值getVariable()方法和{{1}这两个中的一个让你麻烦。

相关问题