 |
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5  | This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 25th, 2003, 05:10 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ch8 pg 266
I have a odd problem.
[client 127.0.0.1] PHP Parse error: parse error, unexpected $end in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\menu1.php on line 18, referer: http://localhost/PHP/Beg_PHP4/CH8/
[Tue Nov 25 15:03:31 2003] [error] [client 24.6.21.109] File does not exist: C:/Documents and Settings/Administrator/My Documents/My Work/scripts
I dont know what the first error is but the second one is what is odd.
Here is the first page code:
<html><head><title>Wades Restaurant: Free Delivery with Every Online Order</title></head>
<body>
<?php
$Entrees=array("Steak ($9)", "Pizza ($7)", "Pasta ($6)");
echo "<form method = 'POST' action 'menu2.php'>";
echo "<p>Which of the following would you like as an entree?</p>";
echo "<select name = 'ListBox1'>";
echo "<option selected value = ' '> Select.....</option>";
echo "<option>$Entrees[0]</option>";
echo "<option>$Entrees[1]</option>";
echo "<option>$Entrees[2]</option>";
echo "</select> <br /><br />";
echo "<input type = submit >";
echo "</form>";
?>
</body>
</html>
When I hit submit it reloads the same page again.
Why?
|
|

November 25th, 2003, 05:18 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Forget that I caught it finally
action = menu2.php
DUH!!
Ok.
The script now runs but I get this after I submit to page three:
Your order was for:
Entree: Steak ($9)
Dessert: Pancakes ($3)
Notice: Use of undefined constant ListBox1 - assumed 'ListBox1' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 11
Notice: Undefined index: ListBox1 in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 11
Notice: Use of undefined constant ListBox2 - assumed 'ListBox2' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 11
Warning: ereg(): REG_EBRACK:nbrackets ([ ]) not balanced in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 12
Warning: ereg(): REG_EBRACK:nbrackets ([ ]) not balanced in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 12
TOTAL BILL = $0
I do not understand how to send hidden values and then recall them.
This is the code for each page:
<html><head><title>Wades Restaurant: Free Delivery with Every Online Order</title></head>
<body>
<?php
$Entrees=array("Steak ($9)", "Pizza ($7)", "Pasta ($6)");
echo "<form method = 'POST' action = menu2.php>";
echo "<p>Which of the following would you like as an entree?</p>";
echo "<select name = 'ListBox1'>";
echo "<option selected value = ' '> Select.....</option>";
echo "<option>$Entrees[0]</option>";
echo "<option>$Entrees[1]</option>";
echo "<option>$Entrees[2]</option>";
echo "</select> <br /><br />";
echo "<input type = submit >";
echo "</form>";
?>
</body>
</html>
<html><head><title>Wades Restaurant: Free Delivery with Every Online Order</title></head>
<body>
<?php
$Desserts=array("Apple Pie ($3)", "Pancakes ($3)", "Ice Cream ($3.25)");
echo "<form method = POST action = 'bill.php'>";
echo "Which of the following would you like as a dessert?";
echo "<select name = 'ListBox2'>";
echo "<option selected value = ' '> Select.....</option>";
echo "<option>$Desserts[0]</option>";
echo "<option>$Desserts[1]</option>";
echo "<option>$Desserts[2]</option>";
echo "</select> <br /><br />";
echo "<input type = hidden name = Course1 value = '$_POST[ListBox1]'>";
echo "<input type = 'submit'>";
echo "</form>";
?>
</body></html>
<html><head><title>Wades Restaurant: Free Delivery with Every Online Order</title></head>
<body>
<?php
$total = 0;
echo "Your order was for: <br />";
echo "Entree: $_POST[Course1] <br />";
echo "Dessert: $_POST[ListBox2] <br />";
foreach (array($_POST[ListBox1], $_POST[ListBox2]) as $val) {
if (ereg("[0-9+", $val, $regs)) $total += $Regs[0]; }
echo "TOTAL BILL = $" . $total . "<br />";
?>
</body></html>
|
|

November 25th, 2003, 05:28 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ha HA! I fixed the first problems.
Now I get this:
Your order was for:
Entree: Steak ($9)
Dessert: Pancakes ($3)
TOTAL BILL = $0
It doesnt total the amounts.
The PHP code for page three:
<?php
$total = 0;
echo "Your order was for: <br />";
echo "Entree: $_POST[Course1] <br />";
echo "Dessert: $_POST[ListBox2] <br />";
foreach (array($_POST["Course1"], $_POST["ListBox2"]) as $val) {
if (ereg (" [0-9] +", $val, $regs)) $total += $regs [0] ; }
echo "TOTAL BILL = $" . $total . "<br />";
?>
Did I type something wrong?
|
|

November 25th, 2003, 05:34 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
That's because in your foreach() conditional expression, you're not putting your array indexes in quotes. Unquoted tokens are interpreted as defined constants. If that constant is undefined, PHP issues a NOTICE level warning and assumes that the token was a string instead. It's a form of error-recovery.
Also, you're including two arrays, separated by commas, in your foreach() array expression. I'm not sure this will work how you expect; I've never used this syntax before. You should use a foreach() to traverse all the items of a single array. If you need to traverse two arrays, use two foreach() statements. It adds clarity.
Since the foreach loop does the same thing for each array, just wrap it in a function:
function calculate_total($menu_items)
{
$total = 0;
foreach ($menu_items as $val)
{
if (ereg(...)) $total += $Regs[0];
}
return $total;
}
$total = 0;
echo "Your order was...";
$total += calculate_total($_POST['ListBox1']);
$total += calculate_total($_POST['ListBox2']);
echo "TOTAL BILL = \${$total}.<br />\n";
you might want to rename your form inputs to "EntreeItems" and "DessertItems", it improves readability in the PHP code...
Take care,
Nik
http://www.bigaction.org/
|
|

November 25th, 2003, 05:59 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Also, I don't think your ereg expression will work quite the way you expect. It doesn't seem to me that it will calculate the cents part of values like "3.25".
I think you're much better off having a list of available desserts and entrees, with their prices associated with them, and using that to determine the value of the item ordered.
This is the perfect application for database tables:
item_type
id name
1 Entree
2 Dessert
3 Drink
4 Appetizer
5 Salad
etc..
menu_items
id type_id name price
1 1 Steak 9
2 1 Pizza 7
3 1 Pasta 6
4 2 Apple Pie 4
etc...
The value of each option will be that items unique id from the menu_items table. The text displayed for each option will be the name from the menu_items table. The prices can be queried from the table ("SELECT price FROM menu_items where id='{$_POST['entree']}'")
and so forth.
Take care,
Nik
http://www.bigaction.org/
|
|

November 25th, 2003, 06:31 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok. I tried what you gave in your first reply and got
Warning: Invalid argument supplied for foreach() in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 14
The code:
<?php
$total = 0;
echo "Your order was for: <br />";
echo "Entree: $_POST[Course1] <br />";
echo "Dessert: $_POST[ListBox2] <br />";
function calculate_total ($menu_items)
{
$total = 0;
foreach ($menu_items as $val)
{
if (ereg(" [0-9] +", $val, $regs)) $total += $regs[0] ;
}
return $total;
}
echo "Your order was...";
$total += calculate_total($_POST['Course1']);
$total += calculate_total($_POST['ListBox2']);
echo "Total Bill = $" . $total . ".";
?>
|
|

November 25th, 2003, 06:37 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yeah, that's my fault -- I didn't look closely enough at your code to begin with. I had assumed that $_POST['ListBox1'] and $_POST['ListBox2'] were ARRAYS, but they're just scalar values. Strings, to be precise.
foreach() is used to iterate over all the items of an array; your code originally used it to iterate over two totally separate strings.
The fix? Ditch foreach, since you're not dealing with an array:
function calculate_total($menu_item)
{
return (ereg(...))? $regs[0] : 0;
}
Lemme know how that works out for ya.
Take care,
Nik
http://www.bigaction.org/
|
|

November 25th, 2003, 06:43 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
For the sake of completeness, I should add that you can (and probably should) modify calculate_total() to handle both arrays and scalar values. Here's one way of doing it:
function calculate_total($item)
{
$total = 0;
if (is_array($item))
{
foreach($item as $itm)
{
if(ereg(...)) $total += $Regs[0];
}
}
else
{
if(ereg(...)) $total += $Regs[0];
}
return $total;
}
Notice that the ereg(...) line is repeated; once in the array context and once for strings. We can clean up this function a little and give a nifty example of recursion:
function calculate_total($item)
{
$total = 0;
if (is_array($item))
{
foreach($item as $itm)
{
$total += calculate_total($itm); // oooo recursion!!
}
}
else
{
if(ereg(...)) $total += $Regs[0];
}
return $total;
}
See how this works? For each item in the array, you call calculate_total() on that item. Recursion usually takes most people a little while before they figure it out, but when they do, it's like a light just turns on and you don't realize why you didn't see it before.
Take care,
Nik
http://www.bigaction.org/
|
|

November 25th, 2003, 06:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oh, I didn't finish my thought.
The reason you'd want calculate_total() to handle both array and scalar types is because you might eventually want your application to handle selecting more than one entree. What happens when you add a "MULTIPLE" to your entree select box?
You'll have to change the name from
<select name="entree">
to
<select name="entree[]" multiple="yes">
And voila -- your user can select mutiple entrees. You'll need to modify how you display the order to the user, but calculate_total() should handle the transition just fine.
Take care,
Nik
http://www.bigaction.org/
|
|

November 25th, 2003, 06:48 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok. I get:
Parse error: parse error, unexpected T_VARIABLE in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 13
with
function calculate_total ($menu_items)
{
return $total (ereg(" [0-9] +", $val, $regs)) $total += $regs[0] : 0 ;
}
or
function calculate_total ($menu_items)
{
return (ereg(" [0-9] +", $val, $regs)) $total += $regs[0] : 0 ;
}
What is the $regs[0] : 0; mean - the last part?
|
|
 |