SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    153
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How to sort/display products with multiple attributes?

    Hi guys

    I am trying to figure out how to add information to each product I am displaying. I have this query to start with:

    Code PHP:
    $result = mysqli_query($link, "SELECT products.*, company.companyname FROM products INNER JOIN company ON products.companyid=company.id WHERE product.availability='In Stock' ORDER BY company.companyname, products.productname");
    	if (!$result)
    		{
    			$error = 'Error.';
    			$link = '.';
    			include 'error.php';
    			exit();
    		}

    And I am displaying the data with this:

    Code PHP:
    $groups = array();
    	while ($row = mysqli_fetch_assoc($result)) {
    		$groups[ $row['companyname'] ][] = $row;
    	}
     
    foreach ($groups as $companyname => $productnames) {
        echo "<strong>"; echo "$companyname\n"; echo "</strong>";
    	echo "</br>";
    		foreach ($productnames as $productname) {
    			echo "$productname[productname]\n";
    			echo "</br>";
    		}
    	echo "</br>";
    	}

    This works and is giving me the following output:

    COMPANY
    productname_1
    productname_2

    ANOTHER COMPANY
    productname_1
    productname_2

    But my question is, how can I, on top of that, have each product as a "sub group" - showing additional information on each product, such as colour/size etc (all attributes from the same 'products' table I am using already).

    My goal is to have the layout like this (each product a separate div for styling):

    COMPANY
    [Product 1 thumbnail] | product name, colour, any other attributes etc |


    [Product 2 thumbnail] | product name, colour, any other attributes etc |


    NEXT COMPANY
    [Product 1 thumbnail] | product name, colour, any other attributes etc |


    [Product 2 thumbnail] | product name, colour, any other attributes etc |

    Appreciate any tips.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    153
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I am extremely daft and this was very easy to fix. Simply add more echos.

    Code PHP
    :
    foreach ($groups as $companyname => $productnames) {
        echo "<strong>"; echo "$companyname\n"; echo "</strong>";
    	echo "</br>";
    		foreach ($productnames as $productname) {
    			echo "$productname[productname]\n";
                            echo "$productname[attribute_1]\n"
                            echo "$productname[attribute_2]\n"
    			echo "</br>";
    		}
    	echo "</br>";
    	}

    I will go and hang my head in shame now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •