When I added (Random added) product to cart by different Vendors.
Ex :
Data Tables.
VENDORS TABLE
| vendorid | vendorname |
-------------------------
| 1 | A |
| 2 | B |
| 3 | C |
-------------------------
PRODUCTS TABLE
| productid | productname | postbyvendor(FK) |
----------------------------------------------
| 1 | Shirt1 | 1 |
| 2 | Shirt2 | 1 |
| 3 | Shirt3 | 2 |
| 4 | Shirt4 | 2 |
| 5 | Pants1 | 1 |
| 6 | Pants2 | 3 |
Clicked add to Cart for 6 times.
1st Add Shirt1 from Vendor A to cart
2nd Add Shirt3 from Vendor B to cart
3rd Add Pants2 from Vendor C to cart
4th Add Shirt2 from Vendor A to cart
5th Add Pants1 from Vendor A to cart
6th Add Shirt4 from Vendor B to cart
Cart view displayed like this.
<li>Shirt1</li>
<li>Shirt3</li>
<li>Pants2</li>
<li>Shirt2</li>
<li>Pants1</li>
<li>Shirt4</li>
I want to display check out detail like this.
<li>Vendor Name : A</li>
<li>Shirt1</li>
<li>Shirt2</li>
<li>Pants1</li>
<li>Vendor Name : B</li>
<li>Shirt3</li>
<li>Shirt4</li>
<li>Vendor Name : C</li>
<li>Pants2</li>
My query.
$statement = $mysqli_conn->prepare("SELECT productname, postbyvendor FROM products_tbl WHERE productname = ? order by postbyvendor asc");
Code that i've tried.
foreach($_SESSION["products"] as $product){
$product_name = $product["productname"];
$vendorsid = $product["postbyvendor"];
$cart_box = "<li>$vendorsid $product_name</li>";
}
Displayed like :
<li>1 Shirt1</li>
<li>2 Shirt3</li>
<li>3 Pants2</li>
<li>1 Shirt2</li>
<li>1 Pants1</li>
<li>2 Shirt4</li>
Appreciated.
I think if I were to do this, I would make a function to gather the vendors and their items, then loop through them to display, something like:
function getVendors()
{
foreach($_SESSION["products"] as $product) {
$org[$product["postbyvendor"]][] = $product["productname"];
}
return (!empty($org))? $org : array();
}
foreach(getVendors() as $vendor => $prods) {
echo "<li>{$vendor}</li>";
echo "<li>".implode("</li><li>",$prods)."</li>";
}