Wrox Programmer Forums
|
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
 
Old November 25th, 2003, 06:53 PM
Authorized User
 
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?



 
Old November 25th, 2003, 07:12 PM
Authorized User
 
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got rid of all the errors with this:

function calculate_total($item)
    {
            $total = 0;
        $val = 0;
                if (is_array($item) )
                {
                        foreach($item as $itm)
                        {
                    $total += calculate_total($itm); // oooo recursion!!
                        }
                }
                else
                {
                       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 . ".";
    ?>


but the output is

 Your order was for:
Entree: Pizza ($7)
Dessert: Pancakes ($3)
Your order was...Total Bill = $0.

 
Old November 25th, 2003, 07:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old November 25th, 2003, 07:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by wadesmart
 I got rid of all the errors with this:

function calculate_total($item)
    {
            $total = 0;
        $val = 0;
                if (is_array($item) )
                {
                        foreach($item as $itm)
                        {
                    $total += calculate_total($itm); // oooo recursion!!
                        }
                }
                else
                {
                     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 . ".";
    ?>


but the output is

Your order was for:
Entree: Pizza ($7)
Dessert: Pancakes ($3)
Your order was...Total Bill = $0.


Okay, again, where does $val fit in anywhere in that function? The only place it appears is as the 2nd parameter to ereg(), which is the SOURCE STRING in which to look for your regular expression.

If you just set it to zero, then it makes sense that ereg() will ONLY find a single zero when you ask it to look for sequences of digits in a string.


It's my fault, though -- I never explicitly wrote out the ereg function parameters for you when I rewrote your code:


if(ereg("[0-9]+(\.[0-9]{2})?", $val, $regs))

I modified your regular expression pattern to also match the case when a decimal point appears followed by two digits. That should cover the case for your $3.25 item.



Take care,

Nik
http://www.bigaction.org/
 
Old November 28th, 2003, 02:41 PM
Authorized User
 
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

11282003 1240

I didnt look at the code after your last post and I got lost.
I have reread everything that has been said and I think I wrote it down correctly:

<html><head><title>Wades Restaurant: Free Delivery with Every Online Order</title></head>
<body>


<?php

    function calculate_total($item)
    {
            $total = 0;
            if (is_array($item))
            {
                    foreach($item as $itm)
                    {
                        $total += calculate_total($itm); // oooo recursion!!
                    }
            }
            else
            {
                   if(ereg("[0-9] + (\.[0-9] {2})?", $val, $regs)) $total += $regs[0]: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";

    ?>
</body></html>

I get only a Parse error on line 19 where the [0] : 0;
stating that ":" is the problem.

Wade

 
Old November 28th, 2003, 03:33 PM
Authorized User
 
Join Date: Oct 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

11282003 1329 GMT-6

I went on to the next exercise and had to deal with the HTTP_POST_VARS and that was much easier. Doing that exercise I discovered an error I had originally.

This is the code for the bill.php on the HTTP_POST_VARS exercise:

<?php
        $total = 0;

        echo "Your order was: <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 />";
    ?>

On the if (ereg("[0-9]...) I had a space between (" [0-... and that is what caused the problem. I didnt put it in while writing the second exercise and it worked right off. I thought - WHY! After checking it over I saw that space.

Changing that in the original code made all the different.

But, that is not to say that I didnt immensely appreciate learning a different way of writing the code. It helps me to see different ways of doing the same problem.

Thanks again Nik.








Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL conversions - ch8 - Database Abstraction binne BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 December 10th, 2007 11:56 PM
Ch8 - TIO#4 (Other List Controls) jecii BOOK: Beginning ASP.NET 2.0 and Databases 0 December 7th, 2007 07:57 PM
Ch8 DB_Migration_Utility robodent BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 December 5th, 2006 07:47 PM
ch8 problem cylo JSP Basics 3 July 19th, 2004 02:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.