帮助安排我的 sql 数据(按类别分组的产品

发布于 2024-10-16 10:26:53 字数 471 浏览 5 评论 0原文

我试图在其类别中显示我的所有产品,例如:

类别名称1 产品名称1、产品名称2 ... 类别名称2 产品名称3,产品名称4 ...

我使用的是oscommerce,所以数据库结构不是我的选择。我需要使用的数据库表是

products_to_categories:保存 products_id 和categories_id products_description:保存products_id和products_name(此表中的其他信息并不重要) category_description:保存categories_id和categories_name

我已经尝试了所有方法,但只能将产品(全部放在一起)和类别全部一起回显,但我无法以某种方式获取它们,使类别中的所有产品都位于指定的范围内正如

我所说,我尝试的所有结果都只是简单地呼应了所有类别,然后是所有产品,

我真的希望您能提供帮助,

谢谢

I am tring to display all my products within their category, so something like:

Category Name1
Product Name1, Product Name2 ...
Category Name2
Product Name3, Product Name4 ...

I am using oscommerce, So the database structure is not my choice. The database tables I need to use are

products_to_categories: holds the products_id and categories_id
products_description:holds products_id and products_name (other info in this table are not important)
category_description: holds the categories_id and categories_name

I have tried everything and i only can get to echo the products (all together) and categories all together, but I can't get them in a way that all the products within a category sit under the specified name

As I said everything I tried resulted in a way that simply echoed all the categories AND THEN all the products

I really hope you can help

thanks

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

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

发布评论

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

评论(3

秋千易 2024-10-23 10:26:53

有两种方法可以解决这个问题,佐德已经在这里给出了背后的想法作为答案。

第一种方法是使用category_description获取所有类别(无论是否包含产品),第二种方法是仅提取使用的类别(即不获取完整的类别列表)。我将采用第一种方式;获取所有类别。

运行category_description,并将ID 和类别名称放入数组中。

现在,使用 foreach 运行该数组并从 products_to_categories 中获取所有 Product_ids。 (从 products_to_categories 中选择 products_id,其中categories_id=[类别 ID 位于此处])。

现在您已拥有与并发类别关联的产品 ID,您可以运行 products_description 并获取其名称。本质上,您的代码应该如下所示:

foreach ($categories as $category) {
   ...display category name and run code to get product ids...
   foreach ($products as $product) {
       ...get product name from products_description...
       ...display product name...
   }
}

There are two ways of approaching this, and the idea behind is was already given as an answer here, by Zod.

The first way is using category_description to get all categories (whether it's full of products or not), the second way is to only extract categories that are used (i.e., not getting a complete list of categories). I'll go through the first way; getting all categories.

Run through category_description, and get the IDs and category names into an Array.

Now, run through that array with foreach and grab all product_ids from products_to_categories. (SELECT products_id FROM products_to_categories WHERE categories_id=[CATEGORY ID GOES HERE]).

Now that you have your product IDs that are associated with the concurrent category, you can run through the products_description and get their name. Essentially, your code should look like:

foreach ($categories as $category) {
   ...display category name and run code to get product ids...
   foreach ($products as $product) {
       ...get product name from products_description...
       ...display product name...
   }
}
染火枫林 2024-10-23 10:26:53

我完全不明白你想如何展示。我希望你喜欢像

Category Name1 
        Product Name1
        Product Name2 
Category Name2 
        Product Name3
        Product Name4

我认为的最简单的方式显示,在数组中获取不同的类别列表。
带有猫 ID 和猫名字
循环该数组并打印每个类别的产品。

如果类别列表很大,将其存储在数组中并循环将是一个坏主意。

I dont understand fully how you want to display. I hope you like to display like this

Category Name1 
        Product Name1
        Product Name2 
Category Name2 
        Product Name3
        Product Name4

Easiest way i think, get distinct list of category in an array.
with cat id and cat name
Loop that array and print products for each Category.

If category list is huge storing it in array and looping will be a bad idea.

巡山小妖精 2024-10-23 10:26:53

您可能需要在 SQL 中使用LEFT JOIN,将三个表合并在一起,并按category_name、product_name 对查询进行排序。

像这样:

SELECT p.product_id, p.product_name, c.category_name FROM `products_description` p
LEFT JOIN `products_to_categories` linker ON linker.product_id=p.product_id
LEFT JOIN `category_description` c ON linker.category_id=c.category_id
ORDER BY c.category_name, p.product_name

那么你的 PHP 输出列表将是这样的:

$data = array(); // Data from query above
$cur_category = '';
foreach($data as $product) {
  if ($product['category_name'] != $cur_category) {
    // Category changed; display it
    echo $product['category_name'].": ";
    $cur_category = $product['category_name'];
  }
  echo $product['product_name'].", ";
}

You'll likely need a LEFT JOIN in your SQL, to merge the three tables together, and order the query by category_name, product_name.

Something like:

SELECT p.product_id, p.product_name, c.category_name FROM `products_description` p
LEFT JOIN `products_to_categories` linker ON linker.product_id=p.product_id
LEFT JOIN `category_description` c ON linker.category_id=c.category_id
ORDER BY c.category_name, p.product_name

Then your PHP to output the list would be something like:

$data = array(); // Data from query above
$cur_category = '';
foreach($data as $product) {
  if ($product['category_name'] != $cur_category) {
    // Category changed; display it
    echo $product['category_name'].": ";
    $cur_category = $product['category_name'];
  }
  echo $product['product_name'].", ";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文