Click here to Skip to main content
15,171,993 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am building an MVC cart system. I have the MERCHANTS table and the PRODUCTS table. A merchant (store) can have one or more products, everything works fine but what I want is to display products by store, I mean to say that once a user adds, for example, some products that belong to the same shop, the cart should display like a treeview. I have designed two functions, one that builds the HTML view and the other function which builds the model.

What I have tried:

SQL
CREATE TABLE `cart` (
    `cartId` int(10) UNSIGNED NOT NULL,
    `product_id` int(10) UNSIGNED NOT NULL,
    `user_id` int(10) UNSIGNED DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `merchants` (
    `merchant_id` int(11) UNSIGNED NOT NULL,
    `merchant_name` varchar(255) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `products` (
    `product_id` int(11) UNSIGNED NOT NULL,
    `merchant_id` int(11) UNSIGNED NOT NULL,
    `product_name` varchar(255) NOT NULL,

) ENGINE=InnoDB DEFAULT CHARSET=latin1;


PHP
$userId = 1; // This is just for an example
$itemsInCart= getCart($userId); // Fetch the items in cart from the DB on session
function getCart($userId): array
{
    try {
        $db = createConnection();
        $sql = 'SELECT * FROM cart
                LEFT JOIN products p ON cart.product_id = p.product_id
                LEFT JOIN merchants m ON p.merchant_id = m.merchant_id
                WHERE user_id = :userId ';
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
        $stmt->execute();
        $invInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $stmt->closeCursor();
        return $invInfo;
    } catch (PDOException $e) {
        exit($e->getMessage()) ;
        return [];
    }
}


PHP
$userCartDisplay = buildUserCartDisplay($itemsInCart); // Builds the cart elements' view
function buildUserCartDisplay($itemsInCart)
{
    $html = '';
    foreach($itemsInCart as $cart) {
        $html .= '<ul> Store name goes right here';
        $html .= '<li> store Products name go right here <li> ';
        $html .= '</ul>  ';
    }
    return $html;
}

echo $userCartDisplay; // Display the view
Posted
Updated 8-Nov-21 14:38pm
v3
Comments
Member 15329613 8-Nov-21 11:33am
   
What is your question?
Rellinxe Fyoni 8-Nov-21 19:22pm
   
I just want Things to be displayed this way
MERCHANT_NAME
Product model
MERCHANT_NAME
Product model Product model Product model
Member 15329613 9-Nov-21 7:03am
   
I do not know what you are asking. You know how you want it displayed so write the code to do it. What are you wanting us to do?

1 solution

UPDATES
After some days of brainstorming, this is what I have come out with but doesn't display well HTML ouput

PHP
function buildUserCartDisplay($itemsInCart)
{

// Here's how you track if you need the header
$lastMerchantID = -3 ;

$html = '';
// Now loop

foreach($itemsInCart as $row)
{        $html .= '<div>';

        // Only show merchant header on condition
        if ($lastMerchantID <> $row['merchant_id']) {
            $html .= '<ul>';
            $html .=  "". $row['merchant_name']."";
            // Note that you've shows this header so you don't show it again.
            $lastMerchantID = $row['merchant_id'];
            $html .=  '</ul>';
        }

        // Now output the rest
        $html .=  '<li>';
        $html .=  "".$row['product_model']."";
        $html .=  '</li>';
        $html .=  '</div>';
}

return $html;
}


HTML
<ul>MERCHANT_NAME
    <li>Product model</li>
</ul>
<ul>MERCHANT_NAME
    <li>Product model</li>
</ul>
<ul>
    <li>Product model</li>
</ul>
<ul>
    <li>Product model</li>
</ul>


but this is what I want instead
HTML
<ul>MERCHANT_NAME
    <li>Product model</li>
</ul>
<ul>MERCHANT_NAME
    <li>Product model</li>
    <li>Product model</li>
    <li>Product model</li>
</ul>
   

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