注意:我意识到这是旧代码,但我希望能够修复它,直到明年我们可以迁移到新系统。
我从以下两行代码中得到了这两个反复出现的错误。
如何修复这些错误?
错误:
PHP警告:in_array()[function.in-array]:第二个参数行120的数据类型错误
PHP致命错误:在非对象行121上调用成员函数add_viewed()
导致错误的代码行:
if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
$viewed->add_viewed($HTTP_GET_VARS['products_id']);
}
以下文件的完整代码
if (((tep_session_is_registered('customer_id')) or (ENABLE_PAGE_CACHE == 'false')) and (!$spider_flag)){
//*******************************************************************************
DEFINE('HIST_ROWS', 7); // number of rows per column on display
DEFINE('HIST_MAX_ROWS',7); // max number of products on display
DEFINE('HIST_MEM_TRIGGER', 1); // number when memory threshold kicks in
//*******************************************************************************
// register the array if not already done so
if (tep_session_is_registered('viewed') && is_object($viewed)) {
} else {
tep_session_register('viewed');
$viewed = new viewed_products;
$viewed->reset();
}
// empty the array if requested by the user
if (isset($HTTP_GET_VARS['action'])) {
if ($HTTP_GET_VARS['action'] == 'viewed_remove') {
$viewed->remove();
}
}
// start shift from line 106 to here
$items_ids_on_display = array();
// end shift
// display the box if we have history
if ($viewed->count_viewed() > 0) { // displaying
?>
<tr>
<td class="prodRowDivide">
<table border="0" width="100%" cellpadding="2" cellspacing="1">
<tr class="header">
<td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<?php
echo '<tr><td nowrap valign="center" class="prodRowHead" height="22">Recently Viewed Products:<br></td></tr>
</table>
</td></tr>
<tr><td valign="top"><table border="0" cellpadding="3" align="left"><tr>';
$info_box_contents = array();
//$info_box_contents[] = array('text' => 'Recently Viewed');
//new infoBoxHeading($info_box_contents, false, false);
$row = 0;
$col = 0;
/* get the products array from the class containing all viewed products */
$items = $viewed->get_viewed_items();
$index = 1;
/* determine the first and last record we want to display*/
$first = sizeof($items)- HIST_MAX_ROWS;
$last = sizeof($items)-1;
if (($last+1) < HIST_MAX_ROWS) {$disp = ($last+1);} else {$disp = HIST_MAX_ROWS;}
if ($first < 0) {$first = 0;}
/* only fetch the info for products on display */
// $items_ids_on_display = array(); // shift to line 67
for ($i=$last, $n=$first; $i>=$n; $i--) {
$viewed_query = tep_db_query("select pd.products_name,
p.products_image_lrg
from " . TABLE_PRODUCTS . " p,
" . TABLE_PRODUCTS_DESCRIPTION . " pd
where p.products_id = '" . $items[$i] . "' and
pd.language_id = '" . $languages_id . "' and
pd.products_id = p.products_id");
if ($viewed_info = tep_db_fetch_array($viewed_query)) {
$items_on_display[$i] = array('id' => $items[$i],
'name' => $viewed_info['products_name'],
'image' => $viewed_info['products_image_lrg']);
$items_ids_on_display[]= $items[$i];
}
}
for ($i=$last, $n=$first; $i>=$n; $i--) {
$currentPage = (int)($HTTP_GET_VARS['products_id']);
if ($currentPage != $items[$i]) {
echo '<td align="left" class="smallText"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_on_display[$i]['id']) . '">'. tep_image_thumb(DIR_WS_IMAGES . $items_on_display[$i]['image'], $items_on_display[$i]['name'], '120', '120') . '</a></center></td>';
$row ++;
$index++;
}
}
?>
</tr></table></td></tr>
</table>
</td>
</tr>
<tr>
<td valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
</tr>
<?php
}
// general condition
}
if (isset($HTTP_GET_VARS['products_id']) and ($HTTP_GET_VARS['action'] != 'viewed_remove')) {
if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
$viewed->add_viewed($HTTP_GET_VARS['products_id']);
}
} ?>
提前感谢您的帮助!
答案 0 :(得分:0)
第一个错误:
PHP警告:in_array()[function.in-array]:第二个参数行120的数据类型错误
您将一个不数组的值传递给in_array
。您可以see from the docs that an array is explicitly required for the second argument.这是因为如果代码顶部的第一个if语句无效,则$items_ids_on_display
未设置。
“如何修复此错误?”
在$item_ids_on_display
尝试使用in_array
之前检查$viewed->add_viewed($HTTP_GET_VARS['products_id']);
是否存在。
该行
$viewed
据报道,尝试将add_viewed()
的值用作$viewed
方法的对象,其中$viewed
的值实际上不是对象(在适用时未设置) if语句不满意。)
“如何修复此错误?”
像之前一样,在尝试将其用作对象之前检查是否存在if (isset($items_ids_on_display) && isset($viewed) && isset($HTTP_GET_VARS['products_id']) and ($HTTP_GET_VARS['action'] != 'viewed_remove')) {
if (!in_array($HTTP_GET_VARS['products_id'], $items_ids_on_display)) {
$viewed->add_viewed($HTTP_GET_VARS['products_id']);
}
}
。
忽略任何其他代码审核并关注报告的问题,底部的结果代码应为:
error_reporting(E_ALL)
您应该使用{{1}}进行编程,这样您就会看到PHP通知以及PHP警告和错误。它们将为您提供有关为什么警告和错误发生的更多信息。