Array display PHP/.txt form

Posted on 2007-10-11
Last Modified: 2012-06-27
The else portion won't display with correct values:

<body bgcolor="#999966" text="#000000">
<h1><font face="Arial, Helvetica, sans-serif">SADA Restaurant</font></h1>

//create short variables
$meal_item = $_REQUEST['meal_item'];
$foodqlty = $_REQUEST['foodqlty'];
$servqlty = $_REQUEST['servqlty'];

if ($meal_item == 0 && $email == 0 && $foodqlty == 0 && $servqlty == 0)

//read array in text file
$menu = file("menu.txt");
$number_of_menu_items = count($menu);

echo '<p>What did you order?</p';
echo "<form name='mealform', method='post', action ='Menu.php'>";
echo "<p><input type='checkbox' name='meal_item[]' value ='$menu[$i]'>{$menu[0]}</input><br />";
echo "<input type='checkbox' name='meal_item[]' value ='$menu[$i]'>$menu[1]</input><br />";
echo "<input type='checkbox' name='meal_item[]' value ='$menu[$i]'>$menu[2]</input><br />";
echo "<input type='checkbox' name='meal_item[]' value ='$menu[$i]'>$menu[3]</input><br />";
echo "<input type='checkbox' name='meal_item[]' value ='$menu[$i]'>$menu[4]</input></p>";

echo "<p>How would you rate the food quality?</p>";
echo "<p><input name='foodqlty' type='radio' value='high'>High</input><br />
<input name='foodqlty' type='radio' value='average'>Average</input><br />
<input name='foodqlty' type='radio' value='low'>Low</input></p>";

echo "<p>How would you rate the service quality?</p>";
echo "<p><select name='servqlty'>
<option value='high'>High</option>
<option value='average' selected>Average</option>
<option value='low'>Low</option></select></p>";

echo "<p><input type='Submit' name='Submit' value='Submit'</input>";

echo "<p><i>Thanks for dining with us! Below is your receipt.</i></p>";

//display item(s) ordered

for ($i=0; $i<$number_of_items_selected; $i++)
            //split up each line of items
            $meal_price = explode( "\t", $meal_item[$i]);
            //output each order
            echo "$meal_price[0]
      $meal_total += $meal_price[2];
      $tax_rate = 0.078375;
      $tax_total = $meal_total * $tax_rate;
      $grat_array = array( "high" => .20, "average" => .15, "low" => .10 );
      $grat_percent = $grat_array[$foodqlty] + $grat_array[$servqlty];
      $grat_total = $grat_percent * $meal_total;
      $grat_nopercent = $grat_percent * 100;
      $total_amount = $meal_total + $grat_total + $tax_total;
      printf("<p>Meal: $%.2f</p>\n", $meal_total);
      printf("<p>Gratuity ($grat_nopercent%%): $%.2f</p>\n", $grat_total);
      printf("<p>Tax (7.8375%%): $%.2f</p>\n", $tax_total);
      printf("<p>Total amount: $%.2f</p>\n", $total_amount);


Question by:shareea
    LVL 50

    Expert Comment

    by:Steve Bink
    Will it display with incorrect values?
    LVL 4

    Accepted Solution

    Add quotes around the zeros.
    if ($meal_item == '0' && $email == '0' && $foodqlty == '0' && $servqlty == '0')

    I'm guessing your variable contains a string and the expression is saying something like $menu_item = 'blah'... 'blah' is not an integer so it must be zero.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now