Empahros Posted January 14, 2023 Share Posted January 14, 2023 Hi! For my school project I have to code a shopping cart page only with HTML, CSS and PHP (bootstrap, sql js,... are not allowed). But I don't know how to code in php (it's my first time). The cart must display the name and quantity of the chosen product in a list. Here is what I did: <?php $quantity= $_POST['quantity']; $price = $_POST['price']; $name= $_POST['name']; $total = $name* $price; $data= array("$quantity","$price","$total"); $D4 = fopen('cart.txt','a'); fwrite($D4,"$name|$quantity|$price|$total|@"); fclose($D4); ?> The information is written to a text file. I would like this information to be written on an html page (a shopping cart page). I don't know how to do this... Can you help me? Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/ Share on other sites More sharing options...
ginerjm Posted January 14, 2023 Share Posted January 14, 2023 Create your html page and by echoing it <?php // $data= array("$quantity","$price","$total"); $tbl_data = "<table> <tr> <th>Qty.</th> <th>Price</th> <th>Total</th> </tr>"; $tbl_data .= "<tr> <td>$quantity</td> <td>$price</td> <td>$total</td> </tr>"; $tbl_data .= '</table>'; // now build the html page $code=<<<heredocs <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style type='text/css'> #tbl_box { position:relative; float:left; margin:2% 5%; width:60%; font-size:14px; } </style> </head> <body bgcolor='#c0c0c0'> <center> <span>Page Title</span> <br> <div id='tbl_box'> <!-- the table data goes here in this div tag which you can adjust with the css code --> $tbl_data </div> </body> </html> heredocs; echo $code; // display the html page with the tbl_data buried in it. Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/#findComment-1604675 Share on other sites More sharing options...
Empahros Posted January 14, 2023 Author Share Posted January 14, 2023 1 hour ago, ginerjm said: Create your html page and by echoing it <?php // $data= array("$quantity","$price","$total"); $tbl_data = "<table> <tr> <th>Qty.</th> <th>Price</th> <th>Total</th> </tr>"; $tbl_data .= "<tr> <td>$quantity</td> <td>$price</td> <td>$total</td> </tr>"; $tbl_data .= '</table>'; // now build the html page $code=<<<heredocs <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style type='text/css'> #tbl_box { position:relative; float:left; margin:2% 5%; width:60%; font-size:14px; } </style> </head> <body bgcolor='#c0c0c0'> <center> <span>Page Title</span> <br> <div id='tbl_box'> <!-- the table data goes here in this div tag which you can adjust with the css code --> $tbl_data </div> </body> </html> heredocs; echo $code; // display the html page with the tbl_data buried in it. I had to dynamically generate HTML in PHP, I understand! How do I get the data to be added progressively to the table? (if there is more than one item in the cart) Thank you very much for the help! Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/#findComment-1604676 Share on other sites More sharing options...
Barand Posted January 14, 2023 Share Posted January 14, 2023 As you add each item, write name, quantity and price to your cart.txt file. Then read the stored items back from cart.txt and display them all, each in its own row, calculating the price for each PS You may find fgetcsv() useful here. Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/#findComment-1604677 Share on other sites More sharing options...
ginerjm Posted January 14, 2023 Share Posted January 14, 2023 Your first post gave the impression that there was only one record. How are you going to collect multiple records - with repeated form entries? And then run a different script that does as Barand suggests? Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/#findComment-1604678 Share on other sites More sharing options...
Barand Posted January 14, 2023 Share Posted January 14, 2023 I didn't suggest a separate script - the one will suffice. if record is posted append to cart.txt end if open cart.txt for reading foreach record write table row end foreach Quote Link to comment https://forums.phpfreaks.com/topic/315803-cart-page-without-database/#findComment-1604679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.