2011-01-08 86 views
0

我對這個站點和學習PHP都很陌生。我使用Darie和Bucica開始的PHP5和MySQL電子商務從新手到專業創建一個電子商務網站。我相信迄今爲止我遇到的一些錯誤是由於更新的數據庫(MDB2)。除了這個之外,我已經能夠通過每一個錯誤。該代碼應該使用Smarty從我的數據庫中提取部門列表。PHP和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

+0

這是一本書的代碼? `unset($ _ smarty_tpl-> tpl_vars ['smarty'] - > value ['section'] ['i']);`移除您在最後兩行嘗試訪問的變量。 – thetaiko 2011-01-08 22:21:37

+0

我認爲這部分是從.tpl文件編譯而不是直接在本書中編譯的。無論如何,它並不是我個人添加的。感謝您指出了這一點。我會看看那個! – DrewInPB 2011-01-09 01:10:32

回答

3

您在in_array函數中使用$_smarty_tpl->getVariable('departments_list')->value->mDepartments。確保您將departments_list分配給smarty對象。

或之前

$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); 
} 
+0

Danke!這似乎奏效了。談到下一個錯誤!感謝您的快速回復! – DrewInPB 2011-01-08 23:23:36

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()方法,getVariable()->value,因此其中一個問題會影響您的工作。

相關問題