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.
Post History
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 b...
#2: Post edited
- 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
- 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
- ````
#1: Initial revision
How to to make a store tree view cart
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