dsfasdfsda234234 2013-02-28 21:44
浏览 20

PHP / MYSQL中的类别? [关闭]

I try to create a menu system and within that menu show whatever there is in MySQL based on one column!

Example

Currently, I can add products from my admin panel and it will be added into MySQL database. When I add a product, I can give this product a brand name i.e. diesel, levi, etc.

Question 1

Now I know how to show the contents of the database in MySQL in a PHP file by using SELECT brand FROM products ..., but this will show all the duplicated brands in my menu (e.g. 3 levi's, 6 diesels) and it will show them like:

levi
levi
levi
Diesel
Diesel
Diesel
Diesel

But I only 1 need of each. How can I do that?

Question number 2

How can I link the diesel category only to products that have that brand? For instance, if someone clicks on the diesel category, php will only show products that have the diesel brand.

  • 写回答

3条回答 默认 最新

  • duanlou7910 2013-02-28 21:46
    关注

    These are fairly simple relational database questions.

    1; Use DISTINCT with your query like

    SELECT DISTINCT `brand` FROM `products`;
    

    2; You would need to use a WHERE clause, based on a parameter that you pass into category.php somehow. We can't really help you without existing code, but this should probably be what a file like category.php would be doing anyway.

    That said, your brands should really be contained within a separate table and linked to from your products (probably one-many relation, one product can have one brand but a brand can have many products). You should really look up some database design concepts before continuing with your project, in my opinion.


    Here's a very simple diagram of how your relationship would work: Simple relational

    Rather than querying products for your brands, you would simply SELECT name FROM brand to get a list of your brands.

    Then, passing in your brand_id to category.php, you can get the products like so SELECT * FROM product WHERE brand_id = 1.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值