Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/315803-cart-page-without-database/
Share on other sites

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.

 

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! :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.