Jump to content

Hard to explain here, but it's simple


soltek

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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  :D

 

You're on the right track

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.