Skip to content
Advertisement

How do I filter clothing on sizes with PHP and AJAX?

I am trying to make a e-commerce filter system with PHP and AJAX. I have watched some guy on YouTube and it worked, but my e-commerce website is in the clothing industry and his was with electronics… So he did not explain how to filter sizes which is stored with quantity (for example: S:2, M:3, L:4) with the filter systems. Now if I add a filter for a product, the product shows that size correctly. But if I try it the second time, it does not execute. I think because of the way my column is set up in MySQL. Please help.

Here is my code for the filter system:

JavaScript

See examplehere I would really appreciate the help!

Advertisement

Answer

From the sample code you have shared, I can see you have an issue with your database design. I would potentially have a table that stores shirt information like:

  1. ID
  2. Shirt Name
  3. Shirt Price (Price of lowest SKU).
  4. timestamps (When the item was added). You can add as many time stamps as are relevant to the product.

Another Table that stores shirt sizes:

  1. id
  2. name (e.g. “S”)

Another Table (PIVOT Table) That holds information about a shirt, the sizes it has and associated price and availability:

  1. shirt_id (Foreign Key references Shirts Table)
  2. size_id (Foreign Key references Shirt Size Table)
  3. unit_price (Price of this particular size of the said shirt)
  4. discount_price (If there is a special offer on shirt)

A table to hold images of each shirt:

  1. id
  2. shirt_id (Foreign Key references Shirts Table)
  3. url (Location of shirt image)
  4. timestamps (When the image was added and when it was last updated).

Lastly, order table

  1. id (order id)
  2. shirt_id (Foreign Key references Shirts Table)
  3. shirt_size_id (Foreign Key references Shirt Size Table)
  4. quantity
  5. total
  6. date (Order created, Order fulfilled)

This is just a guide you can use to design your Database more efficiently. I omitted some columns as I figured you might already have them. If you want to implement multiple items within an order, just create a table for order items where you store individual items that have been ordered, then have a table that stores order total.

The link below will shed more light on what I’ve written above.

It’s safe at this point to normalize your database to make querying easier. Refer to Normalization for more understanding.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement