PHP ticket generator


Posted on 3 August 2011 | Last updated: 12 January 2012 at 03:16 GMT

PHP offers endless possibilities to make real-world web applications. In this example, a ticket generator is created in three simple steps.

Step one: setting up global configuration (settings.php file)

<html>
<head>
<title>
Settings form
</title>
</head>
<body>
<h1>
Step one: setting up global configuration.
</h1>
<form action="items.php" method="get">
<table>
<tr>
<td>
Number of items:
</td>
<td>
<input type="text" name="numofitems" size="3" />
</td>
</tr>
<tr>
<td>
Value Added Tax:
</td>
<td>
<input type="text" name="vat" size="3" />
</td>
</tr>
<tr>
<td>
Prices with tax included:
</td>
<td>
<input type="checkbox" name="taxincluded" size="3" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Continue" />
</td>
</tr>
</table>
</form>
</body>
</html>

Step two: typing the items, prices and quantities (items.php file)

<html>
<head>
<title>
Items form
</title>
</head>
<body>
<h1>
Step two: typing the items, prices and quantities.
</h1>
<form action="receipt.php" method="get">
<table>
<tr>
<th>
Item
</th>
<th>
Price
</th>
<th>
Quantity
</th>
</tr>
<?php
for($i = 1; $i <= $_GET[numofitems]; $i++){
?>
<tr>
<td>
<input type="text" name="item<?=$i;?>" size="30" />
</td>
<td>
<input type="text" name="priceitem<?=$i;?>" size="3" />
</td>
<td>
<input type="text" name="quantityitem<?=$i;?>" size="3" />
</td>
</tr>
<?php
}
?>
<tr>
<td>
<input type="hidden" name="numofitems" value="<?=$_GET[numofitems]?>" />
</td>
<td>
<input type="hidden" name="vat" value="<?=$_GET[vat]?>" />
</td>
<td>
<input type="hidden" name="taxincluded" value="<?=$_GET[taxincluded]?>" />
</td>
</tr>
<tr>
<td colspan="3">
<?php
if($_GET[taxincluded] == 'on' && $_GET[numofitems] > 0){
echo "* Prices include " . $_GET[vat] . "% Value Added Tax.";
}else{
echo "* Prices do not include " . $_GET[vat] . "% Value Added Tax.";
}
?>
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value=Generate ticket />
<td>
</tr>
</table>
</form>
</body>
</html>

Step three: getting the receipt (receipt.php file)

<html>
<head>
<title>
Receipt form
</title>
</head>
<body>
<h1>
Step three: getting the receipt.
</h1>
<table>
<tr>
<th>
Item
</th>
<th>
Price
</th>
<th>
Quantity
</th>
</tr>
<?php
for($i = 1; $i <= $_GET[numofitems]; $i++){
?>
<tr>
<td>
<?php
$getitem = "item" . $i ;
echo $_GET[$getitem];
?>
</td>
<td>
<?php
$getpriceitem = "priceitem" . $i ;
echo $_GET[$getpriceitem];
?>
</td>
<td>
<?php
$getqtyitem = "quantityitem" . $i ;
echo $_GET[$getqtyitem];
?>
</td>
</tr>
<?php
$getquantity = "quantityitem" . $i ;
$totalquantity += $_GET[$getquantity];
$subtotal += $_GET[$getquantity] * $_GET[$getpriceitem];
}
?>
</table>
<?php
if($_GET[taxincluded] == 'on' && $_GET[numofitems] > 0){
echo "* Prices above include " . $_GET[vat] . "% Value Added Tax.";
}else{
echo "* Prices above do not include " . $_GET[vat] . "% Value Added Tax.";
}
?>
<h2>
Order results
</h2>
<?php
echo "Order processed at " . date('H:i, jS F, Y') . ".";
echo "<br />Items ordered: " . $totalquantity;
if($_GET[taxincluded] == 'on' && $_GET[numofitems] > 0){
$tax = $subtotal * $_GET[vat] / 100;
$subtotal -= ($subtotal * $_GET[vat] / 100);
}else{
$tax = $subtotal * $_GET[vat] / 100;
}
echo "<br />Subtotal: $" . number_format($subtotal, 2);
echo "<br />Sales tax amount: $" . number_format($tax, 2);
$total = $subtotal + $tax;
echo "<br />Total including tax: $" . number_format($total, 2);
?>
</body>
</html>

In order to start the execution of the code from the first step, just click the file settings.php.

Home Design Programming Archives Contact About