I have set the PHP variable $accountnumber to be that of the user who is viewing their profile page. On the page, I have a block with the user’s information populated from the database, and I have a list of all products that we have, and I want to put a check mark next to each one that the customer has by assigning a class to it.
Here are my tables:
products
id | name | url | weight
100 p1 p1.html 1
101 p2 p2.html 2
102 p3 p3.html 3
103 p4 p4.html 4
104 p5 p5.html 5
105 p6 p6.html 6
products_accounts
account_number | product_id
0000001 100
0000001 104
0000001 105
0000002 101
0000002 103
0000002 104
0000002 105
0000003 100
0000003 102
I tried a LEFT OUTER JOIN, but was not able to determine if the $accountnumber matched an account_number in the products_accounts table for a specific product_id. The only way that I was able to accomplish this was to add a WHERE statement like this:
WHERE products_acccounts.account_number = '$accountnumber'
It gave the proper class to the product, but only showed the product that they had instead of all.
Here’s my code:
$sql ="
SELECT
products.id,
products.name,
products.url,
products_accounts.account_number
FROM
products
LEFT OUTER JOIN
products_accounts
ON
products.id = products_accounts.product_id
";
$sql .="
GROUP BY
products.id
ORDER BY
products.weight
";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<span class="'; if($row['account_number'] == '$accountnumber')
{ echo'product_yes">'; } else { echo 'product_no">'; }
echo '<a href="' . $row['url'] . '">' . $row['name'] . '</a><br /></span>';
}
If a customer has all product except P2 and P5, it SHOULD display like this:
✓P1
P2 ✓P3 ✓P4 P5 ✓P6
This might help with performance