How can I assign products to other products? Totally Stuck!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How can I assign products to other products? Totally Stuck!!

Post by simonmlewis »

Here's a very difficult problem that I hope someone can see a way around, as I am stuck.

We sell products, and accessories/parts for the products.
So what we have is an EDIT page in Admin where you can go into, for example, a Battery Pack.
You then assign products to that battery pack. Could be 1 product, could be 5 (or more).

You then go to the Accessories page and see all products *that have been assigned to accessories*.
Obviously you only want to show these products once. So you might click on "Ford Truck", and it would display gearknob, wipers, steering wheel..... for example.

The problem is, I am doing this with tokens. So the "productassign" field for each accessory might show like this:
[text]gearknob|wipers|steering wheel[/text]

Previously we only wanted to have ONE product assigned to any accessories, so I just used the DISTINCT term so it dragged out ONE product, whether 5 were assigned to it or not. As the field only had one item in it.

But with multiples, I cannot do this.

The code I have done so far is below, but it's messed up. How can I achieve this effect?

Code: Select all

$result = mysql_query ("SELECT productassign FROM products WHERE (productassign IS NOT NULL OR productassign != '') ORDER BY title");
if (mysql_num_rows($result)==0) { echo "We are building our parts pages right now - they will be available soon.<br/>
You will be able to find your own product, and view their related parts here.";}
while ($row = mysql_fetch_object($result))
      {
      $string = "$row->productassign";
      $token = strtok($string,"|");
      while($token)
        {
        $result2 = mysql_query ("SELECT title, price, photoprimary, id FROM products WHERE title = '$token'");
        while ($row2 = mysql_fetch_object($result2))
          {
          echo "<div class='cat_prodlistbox' style='height: 185px'><a href='index.php?page=productaccs&menu=home&id=$row2->id&title=$row2->title' title='Look at the $row2->title' style='text-decoration: none'>";
			
          if ($row2->photoprimary == NULL) { echo "<img src='images/blank.gif' border='0' />";}
          elseif ($row2->photoprimary != NULL) { echo"
          <img src='images/productphotos/small/$row2->photoprimary' border='0'  style='border: 1px solid #000000'/><img src='images/imageshadow.png' border='0' class='shadowmedium'/><br/>";}
			
          echo "$row2->title<br/>&pound;";
          printf ("%.2f", $row2->price);
          echo "</a></div>";
          }
       }	mysql_free_result($result2);
    }	mysql_free_result($result);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I assign products to other products? Totally Stu

Post by Celauran »

Instead of using strtok(), you could use explode() then loop through the resulting array when building your query.

Code: Select all

SELECT id, name, price, etc
FROM products
WHERE name = 'product 1'
OR name = 'product 2'
OR name = 'product 3'
etc
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How can I assign products to other products? Totally Stu

Post by simonmlewis »

No this won't work, as I don't always know what 'product 1' or 'product 2' would be. It could be anyone or any three of 100 products.
I think the only option is to do this whole thing via a separate table.
accessoryid
producttitle
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I assign products to other products? Totally Stu

Post by Celauran »

You must have some way of knowing what the products will be; what are you storing in the assigned products field? Are you not using a unique identifier such as product ID?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I assign products to other products? Totally Stu

Post by Celauran »

Come to think of it, though, a separate table probably is easier. Two fields; one for the product, one for the product it's assigned to.
Post Reply