I'm sorry I'm going a bit fast for you. I'm just trying to expose you to several different methods of solving problems, as well as how they're implemented in PHP.
Let's start with your code:
Quote:
quote:Originally posted by wadesmart
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 ;
}
|
The problem here is simply that you didn't copy the code correctly. In the first case:
return $total (ereg(" [0-9] +", $val, $regs)) $total += $regs[0] : 0 ;
return is a special keyword that ends a function. It expects an expression that evaluates to a single value to be returned from the function.
In your line, you have "return $total (ereg...)...;". See the problem? You don't just have a single expression, you have two. The first is just $total by itself. The variable itself is an expression that evaluates to the value the variable holds.
The second expression on that same line is the ereg part. The ereg part uses the ternary operator that we've been discussing in recent posts on the forums. It's an expression that has THREE parts, rather than two (like addition or subtraction).
The second case is another version of the same -- you didn't copy the code verbatim:
return (ereg(" [0-9] +", $val, $regs)) $total += $regs[0] : 0 ;
This has the same problem as the ereg from the first attempt -- you're missing the question mark. Also, you don't need the $total += part.
The line should simply read:
return ereg(...)? $regs[0] : 0;
It means:
Does ereg(...) return TRUE? If so, return $regs[0]. Otherwise return 0 (zero).
See what I'm doing here? If the ereg expression failed, then the string being processed doesn't have a price in it, so we assume that it evaluates to a total cost of zero for that "item".
Quote:
quote:Originally posted by wadesmart
Tried the new function and I get:
Your order was for:
Entree: Pizza ($7)
Dessert: Pancakes ($3)
Your order was...
Notice: Undefined variable: val in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 23
Notice: Undefined variable: val in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH8\bill.php on line 23
Total Bill = $0.
This is going a tad fast for me to keep up with. Im STILL LEARNING! Ok. so the $val was not defined. Cant we just say $val = 0 and go from there?
|
Again, I think it's an issue with how things are being copied from the forum window to your code editor. The variable, $val, doesn't appear anywhere in my posts in the suggested rewrites of your code. $val was something that you originally used in your foreach() expression, but we've already determined that foreach() was inappropriate to use since you weren't dealing with arrays of data.
The entire script (with built-in handling for array inputs) should read:
<?php
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;
}
function display_items($items, $type)
{
if (is_array($items))
{
$count = count($items);
$word_form = ($count > 1)? "items" : "item";
echo "You ordered {$count} {$type} {$word_form}:\n";
foreach($items as $item)
{
echo " {$item}\n";
}
}
else
{
echo "You ordered 1 item:\n";
echo " {$items}\n";
}
}
if (isset($_POST['Entree']))
{
display_items($_POST['Entree'], 'Entree');
}
if (isset($_POST['Dessert']))
{
display_items($_POST['Dessert'], 'Dessert');
}
$total = 0;
$total += calculate_total($_POST['Entree']);
$total += calculate_total($_POST['Dessert']);
echo "Your total bill is: \${$total}\n";
?>
I embellished a little for the sake of good-style -- I wrote a generic function that displays menu items. You pass it the item(s) that you wish to display and the name of the item type, and it does the rest. That way, you don't need to copy/paste the code to display both the entrees and desserts; you just call the "display_items()" function and tell it "you're displaying Entree items" or "you're displaying Dessert items".
Let me know how that looks to you -- does it make sense?
Take care,
Nik
http://www.bigaction.org/