简体   繁体   中英

How do I match value from one MySql table to column name of another table in PHP?

I want to display all of our products from our "products" table. We currently have six.

ID | description | url

1....Product 1.....product1.php

2....Product 2.....product2.php

...

Some customers may only have two or three of these products. To determine if a customer has a certain product, the "company_profiles" table has a value of true or false.

ID | account_num | product1 | product2

1....0000001.............1................0

2....0000002.............1................1

...

When I display the user's information, they should see all of our products, but I want to display the products that they have first and assign a class so that they appear bolder. The products that they don't have will be listed as well, but in a lighter color and beneath the products that they have.

My thought on how to accomplish this with these two tables is to SELECT the columns in company_profiles WHERE the account_num = '$account_num'.

Then I would SELECT the columns in the products table. From here I would match the description (the unique name of each product) with the column from company_profiles that has the same name. Products that are matched with a column whose value is true would be assigned into a variable with its URL. The other products would be assigned to another variable.

    while($row = mysql_query($query_where_we_match_true)) {
    $trueproducts .= echo '<a href="' 
. $row['url'] . '" class="trueproduct">' . $row['description'] . '</a><br />'; }

    echo $trueproducts;

(REPEAT FOR $falseproducts )

I think that I can figure out the logistics of displaying the results ... you are welcome to offer your ideas, but I really need to know how match the column name ... unless there is a better way to do this.

The solution is not to use one column per product. Instead, use a junction table :

Products

ID   Description    URL
------------------------
1    Product 1      product1.php
2    Product 2      product2.php
...

Companies

ID   Account_num
-----------------
10   23873232
11   32798372
...

CompanyProducts

Company_ID   Product_ID
------------------------
10           1
10           3
11           1
12           2
12           3
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM