CSCI 2006 - Spring 2024 - Server-Side ProgrammingAssignment #1 - Shopping CartSolution

Solution

index.php

<?php

function getItem($id) {
	$library = [
		106020=>[
			'id'=>106020,
			'name'=>'Girl with a Pearl Earring',
			'artist'=>'Johannes Vermeer',
			'date'=>1665,
			'desc'=>'Girl with a Pearl Earring is an oil painting by Dutch Golden Age painter Johannes Vermeer, dated c. 1665. Going by various names over the centuries, it became known by its present title towards the end of the 20th century after the earring worn by the girl portrayed there.',
			'variations'=>[
				0=>['name'=>'4x5 Reprint','price'=>40],
				1=>['name'=>'8x10 Reprint','price'=>80],
				2=>['name'=>'12x15 Reprint','price'=>120],
			],
		],
		116010=>[
			'id'=>116010,
			'name'=>'Artist Holding a Thistle',
			'artist'=>'Albrecht Dürer',
			'date'=>1493,
			'desc'=>'Portrait of the Artist Holding a Thistle is an oil painting on parchment pasted on canvas by German artist Albrecht Dürer. Painted in 1493, it is the earliest of Dürer\'s painted self-portraits and has been identified as one of the first self-portraits painted by a Northern artist.',
			'variations'=>[
				0=>['name'=>'4x5 Reprint','price'=>65],
				1=>['name'=>'8x10 Reprint','price'=>125],
				2=>['name'=>'12x15 Reprint','price'=>180],
			],
		],
		120010=>[
			'id'=>120010,
			'name'=>'Portrait of Eleanor of Toledo',
			'artist'=>'Angolo di Cosimo',
			'date'=>1545,
			'desc'=>'The Portrait of Eleanor of Toledo and Her Son is a painting by the Italian artist Agnolo di Cosimo, known as Bronzino, finished ca. 1545. One of his most famous works, it is housed in the Uffizi Gallery of Florence, Italy and is considered one of the preeminent examples of Mannerist portraiture',
			'variations'=>[
				0=>['name'=>'4x5 Reprint','price'=>50],
				1=>['name'=>'8x10 Reprint','price'=>75],
				2=>['name'=>'12x15 Reprint','price'=>125],
			],
		],
	];
	return $library[$id]??[];
}

function getCart($id) {
	switch ($id) {
	case -1:
		return [
			['id'=>106020,'var'=>1,'qty'=>3],
			['id'=>116010,'var'=>1,'qty'=>1],
			['id'=>120010,'var'=>1,'qty'=>2],
		];
	case 5:
		return [
			['id'=>106020,'var'=>0,'qty'=>2],
			['id'=>120010,'var'=>2,'qty'=>3],
		];
	case 7:
		return [
			['id'=>120010,'var'=>0,'qty'=>1],
			['id'=>116010,'var'=>2,'qty'=>2],
			['id'=>120010,'var'=>2,'qty'=>2],
		];
	default: 
		return [];
	}
}

function printCart($cartId) {
	$cart = getCart($cartId);

	echo <<<__HTML__
<div class="title">
    <h1>Shopping Cart</h1>
</div>
<table class="table-fill">
    <thead>
        <tr>
	    <th colspan="2">Product</th>
            <th>#</th>
            <th>Price</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
__HTML__;

	$subTotal = 0;
	foreach ($cart as $item) {
		$itemInfo = getItem($item['id']);
		$unit = number_format($itemInfo['variations'][$item['var']]['price'],2);
		$total = number_format($item['qty']*$itemInfo['variations'][$item['var']]['price']);
		$subTotal += $item['qty']*$itemInfo['variations'][$item['var']]['price'];

		echo <<<__HTML__
        <tr>
	    <td><img src="/resources/csci2006/{$item['id']}.jpg"></td>
	    <td>{$itemInfo['name']} - {$itemInfo['variations'][$item['var']]['name']}</td>
            <td>{$item['qty']}</td>
	    <td class="right">\${$unit}</td>
            <td class="right">\${$total}</td>
        </tr>
__HTML__;
	}

	$grand = $subTotal;
	$st = number_format($subTotal,2);

	$grand += $subTotal*0.1;
	$tax = number_format($subTotal*0.1,2);

	$grand += ($subTotal>500)?0:40;
	$ship = number_format(($subTotal>500)?0:40,2);
	
	$total = number_format($grand,2);

	echo <<<__HTML__
	<tr class="totals">
            <td colspan="4">Subtotal</td>
            <td>\${$st}</td>
        </tr>
	<tr class="totals">
            <td colspan="4">Tax</td>
            <td>\${$tax}</td>
        </tr>
	<tr class="totals">
            <td colspan="4">Shipping</td>
            <td>\${$ship}</td>
	</tr>
        <tr class="totals">
            <td colspan="4">Grand Total</td>
            <td>\${$total}</td>
	</tr>
__HTML__;

	echo '</tbody></table>';
}

function printHeader($title) {
	echo <<<__HTML__
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,width=device-width">

	<title>General Store - {$title}</title>
	<link href="?get=css" rel="stylesheet" />
</head>
<body>
__HTML__;
}


function printFooter() { 
	echo <<<__HTML__
</body>
</html>
__HTML__;
}

function printCSS() {
	echo <<<__CSS__
	/*  CSS inspired by http://codepen.io/alassetter/full/cyrfB/ */

	@import url(http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100);

	body {
	  background-color: #F5F5F5;
	  font-family: "Roboto", helvetica, arial, sans-serif;
	  font-size: 14px;
	  font-weight: 400;
	}

	div.title {
	  margin: auto;
	  max-width: 900px;
	  padding:5px;
	  width: 100%;
	}

	.title h1 {
	   color: black;
	   font-size: 26px;
	   font-weight: 500;
	   font-family: "Roboto", helvetica, arial, sans-serif;
	   text-transform:uppercase;
	}


	/*** Table Styles **/

	.table-fill {
	  background: white;
	  border-radius:3px;
	  border-collapse: collapse;
	  height: 320px;
	  margin: auto;
	  max-width: 900px;
	  padding:5px;
	  width: 100%;
	  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
	}
	 
	th {
	  color: white;
	  background: #EF6C00;
	  border-bottom:4px solid #9ea7af;
	  border-right: 1px solid #343a45;
	  font-size:20px;
	  font-weight: 400;
	  padding:24px;
	  text-align:left;
	  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
	  vertical-align:middle;
	}

	th:first-child {
	  border-top-left-radius:3px;
	}
	 
	th:last-child {
	  border-top-right-radius:3px;
	  border-right:none;
	}
	  
	tr {
	  border-top: 1px solid #C1C3D1;
	  border-bottom-: 1px solid #C1C3D1;
	  color:#666B85;
	  font-size:16px;
	  font-weight:normal;
	  text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
	}
	 
	 
	tr:first-child {
	  border-top:none;
	}
	tr:last-child {
	  border-bottom:none;
	}
	tr:last-child td:first-child {
	  border-bottom-left-radius:3px;
	}
	 
	tr:last-child td:last-child {
	  border-bottom-right-radius:3px;
	}

	td {
	  background:#FFFFFF;
	  padding:10px;
	  text-align:left;
	  vertical-align:middle;
	  font-weight:300;
	  font-size:18px;
	  text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
	  border-right: 1px solid #C1C3D1;
	}

	td:last-child {
	  border-right: 0px;
	}

	tr.totals td {
	    background:#FFF3E0;
	    text-align: right;
	}

	td.right {
	  text-align: right;
	}
	td.center {
	  text-align: center;
	}

	.focus td {
	  color: #E65100;  
	  font-weight: 500;
	}
__CSS__;
}

if (isset($_GET['get'])) {
	switch ($_GET['get']) {
		case 'css':
			header("Content-type: text/css");
			printCSS();
			break;
		case 'cart':
			printHeader('Shopping Cart');
			printCart($_GET['cart']??-1);
			printFooter();
		default:
			break;
	}
	die();
}
/* This space is currently not defined -- future task */
header("Location: ?get=cart");


?>
Go To Live Site