php - Correct MySQL query needed to a "save to favorites" button -
this question should have simple solution im not quit sure is. have 2 mysql tables. first products
, simplicity lets contains field product_id
. second table called favorite_products
, contains fields product_id
, member_id
.
again simplicity sake lets query product table , in while loop echo out following link
<a href="#" data-id="$product_id" class ="save" id= "$button_status" >add favorites<\a>
when user clicks link click function inserts given product favorite_products
table along member_id
.
my question how write query check favorite_products
table against products table matches? want can change of add favorites link based on if user has favorited product.
if understood correctly...
when select products list, can join favorites given member:
select p.*, f.member_id not null is_favorite products p left join favorite_products f on (f.product_id = p.product_id , f.member_id = #member_id#)
just don't forget replace #member_id#
actual number ;)
let's have product_id
, product_name
fields in products
table. result (simplified json-style):
[ {product_id: 15, product_name: "product a", is_favorite: 0}, {product_id: 16, product_name: "product b", is_favorite: 1}, ]
Comments
Post a Comment