Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to to make a store tree view cart

+0
−0

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 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.

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;

Model

$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 [];
    }
}

View

$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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Check your query ... (2 comments)
Post Feedback (1 comment)
Clarify the question (1 comment)

0 answers

Sign up to answer this question »