Proprietary Shopping Cart Question.

Hello. Thank you for taking the time to read my questions.

I am in a state of confusion at this point on a certain function I wish to implement in the shopping cart I am programming. I have looked everywhere on Google for the answer but have found none.

I have a basic database table that looks like this,

Products

[table]
[tr]
[td]ID[/td]
[td]Name[/td]
[td]Categories[/td]
[/tr]
[tr]
[td]1[/td]
[td]Demo Product[/td]
[td]1,2[/td]
[/tr]
[tr]
[td]2[/td]
[td]Demo Product #2[/td]
[td]3,4[/td]
[/tr]
[tr]
[td]3[/td]
[td]Demo Product #3[/td]
[td]3,4[/td][/tr]
[tr]
[td]4[/td]
[td]Demo Product #4[/td]
[td]1,2,3[/td][/tr]
[tr][td]

[php]$CatID = $_GET[‘catid’];[/php]

The column I am looking at is the categories column. Basically I need to get the category id’s based on the $CatID variable. Example,

Demo Product #4
Http://www.Example.com/category/?CatID=1
Http://www.Example.com/category/?CatID=2
Http://www.Example.com/category/?CatID=3

Each link display the product within that category, because Demo Product #4 CategoryID’s are 1,2,3.

So to clarify exactly what needs to happen. When the user goes to a certain category page regardless of the CategoryID he or she will be able to view the products with the CategoryID’s associated with that category.

I hope someone can help me with this issue. If you have any questions regarding any additional information please let me know, but all I am trying to achieve is to gather the product CategoryID’s and display the products to the correct $CatID when presented.

  • Travis[/td][/tr][/table]

I found a solution that seems a bit easier I am just having a bit trouble with some things in this solution.

We have 3 Tables,

categories
products
products_categories

These tables look like,

[size=12pt]Categories[/size]

[table]
[tr]
[td]ID[/td]
[td]Category_ID[/td]
[td]Category_Name[/td]
[/tr]
[tr]
[td]1[/td]
[td]100[/td]
[td]Test Category #1[/td]
[/tr]
[tr]
[td]2[/td]
[td]101[/td]
[td]Test Category #2[/td]
[/tr]
[tr]
[td]3[/td]
[td]102[/td]
[td]Test Category #3[/td]
[/tr]
[tr]
[td]4[/td]
[td]103[/td]
[td]Test Category #4[/td]
[/tr]
[/table]

[size=12pt]Products[/size]

[table]
[tr]
[td]ID[/td]
[td]Name[/td]
[td]Price[/td]
[td]Description[/td]
[/tr]
[tr]
[td]1[/td]
[td]Test[/td]
[td]1.99[/td]
[td]Test #1[/td]
[/tr]
[tr]
[td]2[/td]
[td]Test 2[/td]
[td]1.99[/td]
[td]Test #2[/td]
[/tr]
[tr]
[td]3[/td]
[td]Test 3[/td]
[td]1.99[/td]
[td]Test #3[/td]
[/tr]
[tr]
[td]4[/td]
[td]Test 4[/td]
[td]1.99[/td]
[td]Test #4[/td]
[/tr]
[/table]

[size=12pt]Products Categories[/size]

[table]
[tr]
[td]ID[/td]
[td]Category_ID[/td]
[td]Product_ID[/td]
[/tr]
[tr]
[td]1[/td]
[td]103[/td]
[td]3[/td]
[/tr]
[tr]
[td]2[/td]
[td]102[/td]
[td]3[/td]
[/tr]
[tr]
[td]3[/td]
[td]103[/td]
[td]4[/td]
[/tr]
[tr]
[td]4[/td]
[td]101[/td]
[td]2[/td]
[/tr]
[/table]

Now when you want to add a product you have the options to enter the basic product info in and then you are able to select multiple categories through a Select Form Field with Multiple enabled.

What I want to do is print out the categories table in a select form field described above and if the said product being edited or added has selected its category id’s, I want those categories to be selected.

The code below will have its categories printed through a PHP function.

[code]

Test Category #1 Test Category #1 Test Category #1 Test Category #1 [/code]

So now you see how the code will be outputted like and you noticed that categories 101-103 had the selected=‘selected’ value added to them. This was because when comparing the tables products_categories with categories it found that those were being used.

How is it you could compare the tables in such a manner? Would it need to be done through array’s and and finding a LIKE match in a SQL statement? Or how would you guys do it.

Any help would be appreciated. Please post any questions about this if you need help and I will get back to you with an answer.

Thank you.

I this something too complex to ask for? I know it might be. But any help is appreciated. If it is too hard for some peeps on this forum please let me know as I can ask on a different forum if needed. Thank you.

I think I found the code that is working correctly for the above. It’s messy but I will clean it up in the final revised version. This version is directly comparing the product_categories & categories array together. In the final version the product_categories will be based on the product_id in the header and only gather the selected categories that way.

i.e.,

Http://www.YourDomain.com/admin/products.php?product_id=107

something along those lines. But anyways here is the code tell me what you think,

[php]$category_sql = “SELECT *
FROM categories”;
$category_result = mysql_query($category_sql) or die(mysql_error());
$product_sql = “SELECT *
FROM product_categories”;
$product_result = mysql_query($product_sql) or die(mysql_error());
$products = array();
$categories = array();
while($f_pro = mysql_fetch_assoc($product_result)){
$products[] = $f_pro[‘cat_id’];
}

while($f_cat = mysql_fetch_assoc($category_result)){
$categories[] = $f_cat[‘id’];
if(array_diff($products, $categories)){
echo $f_cat[‘name’] . " SELECTED
";
} else {
echo $f_cat[‘name’] . “
”;
}
}[/php]

Never mind. That code I posted is not functioning completely how I would like it.

But you get the idea,

I want to take one group of data (product_categories) and compare it to (categories) if it finds a match I want that output to include a “SELECTED” text nearby it. Just to show that it is selected, but I am having a bit of trouble with it. I will continue to experiment and conduct trial & error.

But any help would be appreciated.

Thank you.

Sponsor our Newsletter | Privacy Policy | Terms of Service