请让我先解释我的问题。我有一个网上商店,其中有很多产品,但也有很多类别。产品图片是从openicecat.biz导入的,它也是一个内容提供商。当某个产品没有可用的描述或图片时,会显示一个noimage.jpg
有人编写了一个代码,从产品列表中选择一个图像,并将其用作该类别的图像。(如果可用)当一个类别有一个包含图像的子类别和一个没有图像的子类别时,问题就来了。将显示noimage.jpg,而不是显示具有图像的子类别中的图像。
例如
catelog -> components -> card readers (no image) -> internal card reader (image)
-> external card reader (no image)代码片段的设计方式是,当内部和外部读卡器中都有图片时,只显示图像,而不是当一个子类别没有图像时显示。
我想要的是,比方说,当子类别内部读卡器有带有图像的产品,而子类别外部读卡器没有图像时,外部读卡器的图像被显示为读卡器的类别图像。
示例
catelog -> components -> card readers (image of internal card reader, instead of no image)
-> internal card reader (image)
-> external card reader (no image)我希望你能理解我的意思。
这是代码片段:
// Start auto fetch category image from product
if($categories['categories_image'] == "") {
$categories_img_query = tep_db_query("select products_image from " . TABLE_PRODUCTS . " p, products_to_categories pc WHERE p.products_id = pc.products_id AND pc.categories_id = '{$categories['categories_id']}' AND p.products_image IS NOT NULL order by p.products_id ASC");
if(tep_db_num_rows($categories_img_query) > 0) {
$categories_img = tep_db_fetch_array($categories_img_query);
$categories['categories_image'] = $categories_img['products_image'];
}
else {
$categories_img_parent_query = tep_db_query("select categories_id from categories WHERE parent_id = '{$categories['categories_id']}'");
while($categories_img_parent = tep_db_fetch_array($categories_img_parent_query)) {
$categories_img_query = tep_db_query("select products_image from " . TABLE_PRODUCTS . " p, products_to_categories pc WHERE p.products_id = pc.products_id AND pc.categories_id = '{$categories_img_parent['categories_id']}' AND p.products_image IS NOT NULL order by p.products_id ASC");
if(tep_db_num_rows($categories_img_query) > 0) {
$categories_img = tep_db_fetch_array($categories_img_query);
$categories['categories_image'] = $categories_img['products_image'];
}
}
}
}
// End auto fetch category image from product有人能帮我完成这段代码吗?
btw1,这是给oscommerce的。
btw2,天哪,我几乎花了40分钟来解释和打字这个问题,这也值得一枚奖牌吗(永恒耐心奖章) ;-)
发布于 2010-10-09 10:09:29
首先,一些批评。WHERE子句应该在联接中。这就是:
WHERE p.products_id = pc.products_id实际上应该是
p JOIN pc USING (products_id)这更快,更清晰。
关于你的具体问题:
我根本不明白为什么会有空白图像(您总是指定products_image不为NULL),因此这意味着noimage.jpg实际上是作为该图像的值存储的。在这种情况下,问题很简单,您添加的是找到的任何图像,即使它是noimage.jpg。不是添加tep_db_fetch_array()中的第一个图像,而是循环遍历它,看看是否找到了非noimage。例如:
while ($row = tep_db_fetch_assoc($categories_img_query)) {
if ($row['products_image'] <> 'noimage.jpg'
or !isset($categories['categories_image'])
) {
$categories['categories_image'] = $row['products_image'];
}
}这还假设您不关心为该类别添加特定的图像,只要有一个非noimage.jpg即可。
https://stackoverflow.com/questions/3894257
复制相似问题