The assignment is to extract data out of another file in the following format and present it in a report. Each line contains 4 values. The serial# of the equipment, the number equivalent of the month, the day of the month and the amount days the equipment was rented.
<?php
$rentals =
"Equipment01 1 3 13
Equipment99 11 17 22"
?>
Where we are at in the class does not use html tables, but I thought it better to learn this than the pre tags. I was able to loop the first set of tables, but could not get the second set to loop so I have too much code there for sure.
Anyway, I welcome comments to help me code this better. Also if this is not appropriate to post here since it is a class assignment, no hard feelings if it is deleted.
Brian
Code: Select all
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Rental Report</title>
</head>
<body>
<?php
//Call in the data and set up the delimiters for strtok
include 'data.php';
$delimiter = " \n"; //A space and a carriage return
$company = "Rental Company";
//Initialize counters and accumulators
$total = 0.0;
$A1Count = 0;
$A2Count = 0;
$A3Count = 0;
$A4Count = 0;
$A5Count = 0;
$A6Count = 0;
$A7Count = 0;
$A8Count = 0;
$A9Count = 0;
$A10Count = 0;
$A1subTotal = 0.0;
$A2subTotal = 0.0;
$A3subTotal = 0.0;
$A4subTotal = 0.0;
$A5subTotal = 0.0;
$A6subTotal = 0.0;
$A7subTotal = 0.0;
$A8subTotal = 0.0;
$A9subTotal = 0.0;
$A10subTotal = 0.0;
echo "<table border=\"1\" cellpadding=\"1\" cellspacing=\"2\" align=\"center\" bgcolor=\"silver\"> \n";
echo "\t<tr> \n";
echo "\t\t<td colspan=\"4\" align=\"center\">$company</td> \n";
echo "\t</tr> \n";
echo "\t<tr> \n";
echo "\t\t<td width=\"100px\" align=\"left\">SERIAL#</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">Month</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">Day</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">Days Rented</td> \n";
echo "\t</tr> \n";
//Input the first person's data
$serial = rtrim(strtok ($rentals, $delimiter));
$month = rtrim(strtok ($delimiter));
$day = rtrim(strtok ($delimiter));
$daysout = rtrim(strtok ($delimiter));
//Loop to name months from a number
while ($serial)
{
if ($month == 1){
$monthName = "Jan";
}
elseif ($month == "2"){
$monthName = "Feb";
}
elseif ($month == "3"){
$monthName = "Mar";
}
elseif ($month == "4"){
$monthName = "Apr";
}
elseif ($month == "5"){
$monthName = "May";
}
elseif ($month == "6"){
$monthName = "Jun";
}
elseif ($month == "7"){
$monthName = "Jul";
}
elseif ($month == "8"){
$monthName = "Aug";
}
elseif ($month == "9"){
$monthName = "Sep";
}
elseif ($month == "10"){
$monthName = "Oct";
}
elseif ($month == "11"){
$monthName = "Nov";
}
elseif ($month == "12"){
$monthName = "Dec";
}
else
printf ("ERROR\n", $month);
if ($serial == "Equipment #1"){
$rate = "109.95";
$subtotal = $rate * $daysout;
$A1Count += $daysout;
$A1subTotal += $subtotal;
}
elseif ($serial == "Equipment #2"){
$rate = "185.95";
$subtotal = $rate * $daysout;
$A2Count += $daysout;
$A2subTotal += $subtotal;
}
elseif ($serial == "Equipment #3"){
$rate = "160.25";
$subtotal = $rate * $daysout;
$A3Count += $daysout;
$A3subTotal += $subtotal;
}
elseif ($serial == "Equipment #4"){
$rate = "37.95";
$subtotal = $rate * $daysout;
$A4Count += $daysout;
$A4subTotal += $subtotal;
}
elseif ($serial == "Equipment #5"){
$rate = "85.50";
$subtotal = $rate * $daysout;
$A5Count += $daysout;
$A5subTotal += $subtotal;
}
elseif ($serial == "Equipment #6"){
$rate = "79.90";
$subtotal = $rate * $daysout;
$A6Count += $daysout;
$A6subTotal += $subtotal;
}
elseif ($serial == "Equipment #7"){
$rate = "298.00";
$subtotal = $rate * $daysout;
$A7Count += $daysout;
$A7subTotal += $subtotal;
}
elseif ($serial == "Equipment #8"){
$rate = "75.50";
$subtotal = $rate * $daysout;
$A8Count += $daysout;
$A8subTotal += $subtotal;
}
elseif ($serial == "Equipment #9"){
$rate = "32.50";
$subtotal = $rate * $daysout;
$A9Count += $daysout;
$A9subTotal += $subtotal;
}
elseif ($serial == "Equipment #10"){
$rate = "42.75";
$subtotal = $rate * $daysout;
$A10Count += $daysout;
$A10subTotal += $subtotal;
}
else
printf ("ERROR\n", $serial);
//Compute equipment rental income
$subTotal = $rate * $daysout;
//Accumulate total
$total += $subTotal;
echo "\t<tr> \n";
echo "\t\t<td width=\"100px\" align=\"left\">$serial</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">$monthName</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">$day</td> \n";
echo "\t\t<td width=\"100px\" align=\"center\">$daysout</td> \n";
echo "\t</tr> \n";
//We're done processing this person--read the next person's data
$serial = rtrim(strtok ($delimiter));
$month = rtrim(strtok ($delimiter));
$day = rtrim(strtok ($delimiter));
$daysout = rtrim(strtok ($delimiter));
} //END WHILE
echo "</table> \n";
?>
<br />
<table border="1" cellpadding="1" cellspacing="2" align="center" bgcolor="silver">
<tr>
<td colspan="3" align="center">Summary Report for <?php echo($company);?></td>
</tr>
<tr>
<td width="133px" align="left">Serial#</td>
<td width="133px" align="right">Total Days Rented</td>
<td width="133px" align="right">Total Revenue</td>
</tr>
<tr>
<td width="133px" align="left">Equipment #1</td>
<td width="133px" align="right"><?php echo($A1Count);?></td>
<td width="133px" align="right"><?php echo number_format($A1subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #2</td>
<td width="133px" align="right"><?php echo($A2Count);?></td>
<td width="133px" align="right"><?php echo number_format($A2subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #3</td>
<td width="133px" align="right"><?php echo($A3Count);?></td>
<td width="133px" align="right"><?php echo number_format($A3subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #4</td>
<td width="133px" align="right"><?php echo($A4Count);?></td>
<td width="133px" align="right"><?php echo number_format($A4subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #5</td>
<td width="133px" align="right"><?php echo($A5Count);?></td>
<td width="133px" align="right"><?php echo number_format($A5subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #6</td>
<td width="133px" align="right"><?php echo($A6Count);?></td>
<td width="133px" align="right"><?php echo number_format($A6subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #7</td>
<td width="133px" align="right"><?php echo($A7Count);?></td>
<td width="133px" align="right"><?php echo number_format($A7subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #8</td>
<td width="133px" align="right"><?php echo($A8Count);?></td>
<td width="133px" align="right"><?php echo number_format($A8subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #9</td>
<td width="133px" align="right"><?php echo($A9Count);?></td>
<td width="133px" align="right"><?php echo number_format($A9subTotal, 2);?></td>
</tr>
<td width="133px" align="left">Equipment #10</td>
<td width="133px" align="right"><?php echo($A10Count);?></td>
<td width="133px" align="right"><?php echo number_format($A10subTotal, 2);?></td>
</tr>
<tr>
<td colspan="3" align="right">Total: $<?php echo number_format($total, 2);?></td>
</tr>
</table>
<!--
Extra Credit(10 pts) After the primary report, identify the piece of equipment that was rented the maximum
number of days: display the equipment name and the number of days rented. Of course, you need to use
appropriate logic to determine the answer. If you just pull the answer out of the file, don’t expect to
see any points awarded! (An array would make this much easier, but you haven’t studied them yet.)
-->
</body>
</html>