Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to manage product in db with few

1:product of some category like men / women have
size different for each

2: each product size comes in different colors

3:qty of product will depend on both size and color

like : tshirt >> XL >> red >> 2
shoe >> 19 >> black >> 4

some product my not have dependency on size or colors or both

like : wallet >> red >> 10
: Latop >> 19" inch >> 12

please do help ?
Posted

1 solution

What about something like this? The database structure would be something like the following (in case the array doesn't make sense).

ID | Product | Desc | Attr
---------------------------

ID would hold the product's ID number (unique key, auto increment).

Product would hold the product name.

Desc would hold the description

Att would hold the attributes (like size, color, gender...) in the following format.

Attr: Value;
Size: 9;

PHP
// Example array as it would be from the database
$db = array(
            array('ID' => '1',
                  'Product' => 'Wallet',
                  'Desc' => 'A red wallet',
                  'Attr' => 'Color: Red;'
                  ),
            array('ID' => '2',
                  'Product' => 'Shoe',
                  'Desc' => 'A walking shoe',
                  'Attr' => 'Color: White;
                             Gender: Female;
                             Size: 11.5;'
                  ),
            array('ID' => '3',
                  'Product' => 'Laptop',
                  'Desc' => 'A Dell Inspiring Laptop',
                  'Attr' => 'Color: Silver;
                             Size: 19";'
                  ),
            array('ID' => '4',
                  'Product' => 'Sock',
                  'Desc' => 'Nike Sock',
                  'Attr' => 'Color: White;
                             Size: 9;'
                  ),
            );

// Going through the results
foreach($db as $key => $desc)
{
    if(!empty($desc))
    {
        // The product ID and name
        echo "<strong>Product:</strong> {$desc['ID']} {$desc['Product']}<br />\n";

        // The product description
        echo "<strong>Description:</strong> {$desc['Desc']}<br />\n";

        // Generating the attributes for the product (size, color, gender...)
        echo "<ul>\n";
        $attr = preg_replace("/(\w+(?::))/i", "<li><strong>$1</strong>", $desc['Attr']);
        echo preg_replace("/(\w+(?:;))/i", "$1</li>\n", $attr);
        echo "</ul>\n";
        echo "<hr />\n";
    }
}

?>


It's going to display all that as:

Product: 1 Wallet
Description: A red wallet

    * Color: Red;
_________________________________________________________________

Product: 2 Shoe
Description: A walking shoe

    * Color: White;
    * Gender: Female;
    * Size: 11.5;
_________________________________________________________________

Product: 3 Laptop
Description: A Dell Inspiring Laptop

    * Color: Silver;
    * Size: 19";
_________________________________________________________________

Product: 4 Sock
Description: Nike Sock

    * Color: White;
    * Size: 9;
_________________________________________________________________
 
Share this answer
 
v5
Comments
EZW 28-Mar-14 19:48pm    
I guess this would make it a bit harder to search through the database for a specific attribute. Unless you search something like:

SELECT * FROM products WHERE product = wallet AND Attr LIKE white

Something like that... don't remember how to use LIKEs. or maybe...

SELECT * FROM products WHERE product = wallet AND Attr = %white%

I think that would work too...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900