显示子类别问题的类别图像

发布于 2024-09-26 21:12:42 字数 2455 浏览 0 评论 0原文

请让我先解释一下我的问题。我有一个网上商店,里面有很多产品,而且还有很多类别。产品图片是从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

任何人都可以帮我完成这个片段吗?

顺便说一句,它是针对 oscommerce 的。

顺便说一句,天哪,我几乎花了 40 分钟解释和输入这个问题,这也值得一枚奖章(永恒耐心奖章);-)

Please let me explain my question first. I have a webshop which has a lot of products, but also a lot of categories. The product pictures are imported from openicecat.biz, it's a content provider too. When there is no description or image available for a certain product, a noimage.jpg is displayed

Someone made a code which picks a image from the product list and uses it as a image for that category. (when available)
The trouble comes when a category has a sub-category with images and a sub-category without images. The noimage.jpg is displayed instead it shows a image from a sub-category with images.

For example

catelog -> components -> card readers (no image) -> internal card reader (image)
                                                 -> external card reader (no image)

The way the code snippet was designed, only a image will show when there are both pictures in internal and external card reader, not when one sub category does not have images.

What I'd like to have is that when for example the sub-category internal cardreader has products with images, and the sub-category external card reader has no images, a image of a external card reader is displayed as category image for card readers.

Example

catelog -> components -> card readers (image of internal card reader, instead of no image)                                    
                                       -> internal card reader (image)
                                       -> external card reader (no image)

I hope you'll understand what I mean.

This is the code snippet:

// 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

Can anyone help me complete this snippet?

btw1, it's for oscommerce.

btw2, omg, I allmost spend 40 minutes explaining and typing this problem, is this worth a medal too (medal of Eternal Patience) ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-10-03 21:12:42

首先,一些批评。您的 WHERE 子句应该位于您的 JOIN 中。即:

WHERE p.products_id = pc.products_id

其实应该是

p JOIN pc USING (products_id)

这样更快更清晰。

关于您的具体问题:

我根本不明白您如何拥有空白图像(您总是指定 products_image IS NOT 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(如果有)。

First of all, some criticism. Your WHERE clause should be in your JOIN. That is:

WHERE p.products_id = pc.products_id

should actually be

p JOIN pc USING (products_id)

This is faster clearer.

About your specific problem:

I don't understand how you can have blank images at all (you always specify products_image IS NOT NULL) so the implication is that noimage.jpg is actually stored as the value for that image. In that case the problem is simply that you are adding whatever image is found, even if it is noimage.jpg. Instead of adding the first image from tep_db_fetch_array(), loop through it and see if a non-noimage was found. For example:

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'];
  }
}

This also assumes you don't care about adding a SPECIFIC image for the category, just any non-noimage.jpg if one is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文