soltek Posted October 16, 2011 Share Posted October 16, 2011 Hey guys, this is what I'm trying to do. I want to print on the member's profile page, a list of the products he has bought so far. I've come with two solutions: A) add a new field to the products' table, with the ID of every clients that bought that product; B) add a new field to the members table, with the ID of every products he bought; These are pretty similar approaches, but I've no idea on how can this be done. It would look like: Bought: 2, 3, 4, 5, I dont know how to write this data or how to read it. Any idea? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/ Share on other sites More sharing options...
TOA Posted October 16, 2011 Share Posted October 16, 2011 This is when you need a relational table. Look into database normalization. Scenario: Member Table, Product Table, MemberProduct Table MemberProduct table would take the member's id from member table and the product id from product table. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279658 Share on other sites More sharing options...
soltek Posted October 16, 2011 Author Share Posted October 16, 2011 This is when you need a relational table. Look into database normalization. Scenario: Member Table, Product Table, MemberProduct Table MemberProduct table would take the member's id from member table and the product id from product table. Hope that helps Thank you, mate. Nice and simple. But I'm wondering... wouldnt that create too many entries? Like 1000 users * 100 purchases = 100.000 entries Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279666 Share on other sites More sharing options...
MasterACE14 Posted October 16, 2011 Share Posted October 16, 2011 But I'm wondering... wouldnt that create too many entries? Like 1000 users * 100 purchases = 100.000 entries that really isn't much at all, but if you're having that many entries inserted into the database say regularly like every 5mins, then that may become problematic if you're not running on a dedicated server with room to breathe. Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279671 Share on other sites More sharing options...
soltek Posted October 16, 2011 Author Share Posted October 16, 2011 Got it! There's one more thing... If I have a list of, lets say, 30 products on a page and I want a special button to appear if the user already bought that product, how could I do that efficiently? The only way I know would be kinda messy: SELECT FROM memberproduct where product=$product AND member = $currentmember_id if there's a match, print the button. Else, do not print it. The thing is this would be done for every single product on that page. I dont think running >30 queries on one page is a good idea lol Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279677 Share on other sites More sharing options...
soltek Posted October 16, 2011 Author Share Posted October 16, 2011 Oh, actually it would be better to use have a sql query to look for all the products bought by the member, storing them in a array and then compare the ID of the product listed on the page, with the ID of the products in the array. Is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279682 Share on other sites More sharing options...
soltek Posted October 16, 2011 Author Share Posted October 16, 2011 I got it, the in_array built-in function is pretty handy. Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279704 Share on other sites More sharing options...
TOA Posted October 16, 2011 Share Posted October 16, 2011 Oh, actually it would be better to use have a sql query to look for all the products bought by the member, storing them in a array and then compare the ID of the product listed on the page, with the ID of the products in the array. Is this correct? Yes I got it, the in_array built-in function is pretty handy. It sure is You're on the right track Quote Link to comment https://forums.phpfreaks.com/topic/249190-hard-to-explain-here-but-its-simple/#findComment-1279735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.