I am on a class assignment that is past due, I've been at this problem for several days straight and can't figure it out.
Here is the webpage for my assignment I created, play with it and you'll know what I mean:
http://alvinsanchez.com/Assignment4.php
Here is my orders.txt file exactly as it is in the text file (the file that has my menu elements delimited by tabs):
1Nachos Grande8.99
2Meano Burrito7.99
3El Nino's Pasta8.49
4Horchata Tea1.99
5Le Creme Pie3.49
6Champ's Meal9.99
7Veggie's Stew4.99
Assignment: Have the script read the menu from your data file. Ready the file into an array, then use the array to generate the menu on the HTML form. (I've got this part done).
below is my script and in ALL CAPS, BOLD AND RED are my comments in problem places:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; //must be at the start of code to open/read text files
$id = $POST['id'];
$foodquality = $POST['foodquality'];
$servicequality = $POST['servicequality'];
$comments = $POST['comments'];
$email = $_POST['email'];
$foodquality = trim($foodquality);
$servicequality = trim($servicequality);
$comments = trim($comments);
$email = trim($email);
$offcolor = array('#$!', '#$!', 'bitch', '#$!', '#$!', 'dick', 'damn');
$comments = str_replace($offcolor, '%@&*#', $comments);
$toaddress = 'sanchez.alvin@gmail.com';
$subject = 'Feedback from web site';
$mailcontent = 'Food Quality: '.$foodquality."\n"
.'Service Quality: '.$servicequality."\n"
.'Customer Comments: '.$comments."\n"
.'Customer Email: '.$email."\n";
$fromaddress = 'From: Customer - alvinsanchez.com';
mail($toaddress, $subject, $mailcontent, $fromaddress);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Assignment 4</title>
<link href="Assignment2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
//Read in the entire file.
//Each order becomes an element in the array
$orders= file("$DOCUMENT_ROOT/../public_html/orders.txt"); //MY orders.txt FILE STORED INTO THE $orders VARIABLE
// count the number of orders in the array
$number_of_orders = count($orders);
if (empty($id)) {
echo "<table width=\"120\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo '<tr><th bgcolor="#CC0000" width=\"50\">Item ID</th>
<th bgcolor="#CC0000">Menu Item</th>
<th bgcolor="#CC0000">Price</th></tr>
<tr><td><strong>Top of Form </strong></td></tr>
<tr><td>Select the food you ordered from the list below:</td></tr>
<tr><td><form id="mealForm" name="form1" method="post" action="Assignment4.php"></td></tr>';
for ($i=0; $i<$number_of_orders; $i++) { //IT ALL STARTS HERE WITH THIS FOR LOOP
//I SPLIT UP EACH LINE IN MY orders.txt FILE AND STORE IT IN THE ARRAY $line
$line = explode( "\t", $orders[$i] );
// keep only the number of items ordered
$line[0] = intval( $line[0] );
$line[2] = floatval( $line[2] ); //FOR MY MENU PRICES WITH DECIMAL POINTS
//ON EVERY LOOP PASS, I WANT TO ASSIGN THE CHECKBOX NAME TO id[] WHICH I HAVE DONE, HOWEVER, THE VALUE FIELD WILL NOT ACCEPT $line[0] AS DEMONSTRATED BELOW WHEN I ECHO THE $id[] values AND PRESS THE SUBMIT BUTTON, ALL IT OUTPUTS IS "$line[0]" ACCORDING TO HOW MANY BOXES I CHECKED OFF, BUT NO $line[0] ELEMENT VALUES, (WHICH SHOULD BE 1,2,3,4,5,6,7) ACCORDING TO WHICH BOXES WERE CHECKED...(more below)
echo "<tr><td>".'<input type="checkbox" name="id[]" value="$line[0]" />'."$line[0]</td>
<td>$line[1]</td>
<td>$line[2]</td>
</tr>";
}
echo '<tr><td>How would you rate the food quality?</td></tr>
<tr><td><select name="foodquality"><option>High</option>
<option>Average</option>
<option>Low</option>
</select></td></tr>
<tr><td>How would you rate the service quality?</td></tr>
<tr><td><select name="servicequality"><option>High</option>
<option>Average</option>
<option>Low</option>
</select></td></tr>
<tr><td>Please enter any comments (optional).</td></tr>
<tr><td><textarea name="comments"></textarea></td></tr>
<tr><td>Please enter your email address (optional).</td></tr>
<tr><td><input name="email" type="text" /></td></tr>
<tr><td><input name="submit" type="submit" /></td></tr></form>';
echo '</table>';
} else { //ASSUMING WE HAVE PRESSED THE SUBMIT BUTTON...
echo "<table style=\"margin: 0 auto; padding: 5px; border: 1px solid #000;\" width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo '<tr><td><h1>Demo Cafe</h1></td></tr>
<tr><td><p>Come back again!</p><p> </p></td>
</tr><tr><td>Here is a copy of your bill:<br /></td></tr>';
echo "$id[0]"; //I WOULD LIKE TO ECHO OUT EVERY $line[0] ELEMENT CREATED, I SHOULD BE ABLE TO SEE ONLY THE ID NUMBERS CHECKED, BUT I DON'T, ALL I SEE ON MY RESULTS PAGE IS "$line[0]" (the value field) 2,3 OR MORE TIMES DEPENDING ON HOW MANY BOXES I CHECKED. ANY HELP WOULD BE APPRECIATED, THANKS IN ADVANCE.
echo "$id[1]";
echo "$id[2]";
echo "$id[3]";
echo "$id[4]";
echo "$id[5]";
echo "$id[6]";
$order_number = count($id);
echo '<tr><td>'.$order_number.'</td></tr>';
$mealTax = 0.078375;
$taxedMeal = 0;
$taxedMeal = $mealtotal * $mealTax;
echo '<tr><td>Tax (7.8375%): $'.number_format($taxedMeal,2).'</td></tr>';
$gratMealTax = 0.00;
$grat1tip = 0;
$grat2tip = 0;
$gratuities = array('grat1' => $foodquality, 'grat2' => $servicequality);
if ($gratuities['grat1'] == 'High') {
$gratMealTax += 0.10;
$grat1tip = 10;
} else if ($gratuities['grat1'] == 'Average') {
$gratMealTax += 0.05;
$grat1tip = 5;
} else if ($gratuities['grat1'] == 'Low') {
$grat1tip = 0;
}
if ($gratuities['grat2'] == 'High') {
$gratMealTax += 0.10;
$grat2tip = 10;
} else if ($gratuities['grat2'] == 'Average') {
$gratMealTax += 0.05;
$grat2tip = 5;
} else if ($gratuities['grat2'] == 'Low') {
$grat2tip = 0;
}
$tip = $grat1tip + $grat2tip;
$gratMeal = 0;
$gratMeal = $mealtotal * $gratMealTax;
echo '<tr><td>Gratuity ('.intval($tip,1).'%):'.' $'.number_format($gratMeal,2).'</td></tr>';
$grandTotal = $mealtotal + $taxedMeal + $gratMeal;
echo '<tr><td>Total: $'.number_format($grandTotal,2).'</td></tr>';
echo '<tr><td>Your Comments:</td></tr>
<tr><td>'.nl2br($comments).'</td></tr>
<tr><td>Your Email:</td></tr>';
if (!eregi('[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $email)) {
echo '<tr><td>no email provided</td></tr>';
} else {
echo '<tr><td>'.$email.'</td></tr>';
}
}
echo '</table>';
?>
</body>
</html>