Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 3rd, 2003, 03:28 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH5 pg 150 While Loops

I am getting errors:
"PHP Fatal error: Maximum execution time of 30 seconds exceeded in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\loan2.php on line 25, referer: http://localhost/PHP/Beg_PHP4/CH5/loan2.htm"

Actually, it is lines 21 through 30. On the following code, the <?php is line 4.

<?php
        $Duration = 0;
        switch ($_POST["loan"])
        {
            case 1000: $Interest = 5;
            break;

            case 5000: $Interest = 6.5;
            break;

            case 1000: $Interest = 8;
            break;

            default:
            echo "You didnt chose a loan package.";
            exit;
        }

        while ($_POST["loan"] >= 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $NLoan = $_POST["loan"] + $Monthly;
        }

            echo "This would take you $Duration months to pay this off at the interest rate of $Interest percent." ;
    ?>


Reply With Quote
  #2 (permalink)  
Old November 3rd, 2003, 04:29 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The reason that you are getting that error is because you have an infinite loop. You're saying execute this code while $_POST["loan"] is greater than or equal to zero, and since you are doing nothing with the $_POST["loan"] variable to decrease its value, that condition isn't ever being met.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old November 3rd, 2003, 04:32 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Or rather, that condition is *always* being met! And the loop never terminates.

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old November 3rd, 2003, 07:28 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. will this work.

while ($_POST["loan"] > 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $_POST["loan"] = $_POST["loan"] + $Monthly;
        }



Reply With Quote
  #5 (permalink)  
Old November 3rd, 2003, 07:37 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well no, you're still increasing the value of $_POST["loan"]

In plain English you're saying:

While $_POST["loan"] is greater than zero *always* execute this code.
Meanwhile you are increasing, not decreasing the value of $_POST["loan"], so it is *always* going to be greater than zero and therefore will make an infinite loop.

I'm going to have a look at your code and get back to you in a minute.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old November 3rd, 2003, 07:43 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The skinny of it is I think you want to be subtracting, not adding the $Monthly variable.

$_POST["loan"] = $_POST["loan"] - $Monthly;

Perhaps for clarity this would be easier:

$Principle = $_POST["loan"];

First you calculate accrued interest on the $Principle, then you would be subtracting your $Monthly payment from the $Principle to get the $Duration.

Make sense?
: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #7 (permalink)  
Old November 3rd, 2003, 07:50 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So,

<?php
        $Principle = $_POST["loan"];
        $Duration = 0;
        switch ($Principle)
        {
            case 1000: $Interest = 5;
            break;

            case 5000: $Interest = 6.5;
            break;

            case 1000: $Interest = 8;
            break;

            default:
            echo "You didnt chose a loan package.";
            exit;
        }

        while ($Principle > 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $Principle = $Principle + $Monthly;
        }

            echo "This would take you $Duration months to pay this off at the interest rate of $Interest percent." ;
    ?>


Reply With Quote
  #8 (permalink)  
Old November 3rd, 2003, 08:05 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You still need to subtract the $Monthly from the $Principle. I assume $Monthly is the calculated monthly payment amount.

On this line:
$Principle = $Principle - $Monthly;

I'm looking at the book now and that appears to be the only error.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #9 (permalink)  
Old November 4th, 2003, 01:10 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, Wade -- you seriously need to step back and just look at your code. Make up some numbers, grab a pencil and paper (or use a whiteboard, as I prefer), and trace through your code. Take a look for yourself what's happening. Come up with some sample values for your variables, write them down, and keep track of each variable value as your code runs.

When I read the code, I wonder why you have a while() loop at all. I mean, Rich is correct in diagnosing your problem -- an infinite loop... but why are you looping at all? What is it your loop is meant to do?


The way I read it, a user submits a loan amount and a monthly payment, and you calculate how long it'll take to pay off that loan at that monthly rate.

The reason you're using a loop is because each iteration of the loop represents a month's payment. After each iteration, the loan balance *should* go down -- that implies that you WILL approach a zero balance sooner or later.

If the balance does NOT change, or worse, it rises, then you've got a problem -- your monthly payment either matches or is less than the interest charge for that month. Your script does test for this, but it tests for it within the body of the loop and doesn't exit the loop if the test fails, which will also cause an infinite loop.

Here's my suggested rewrite of your code:

<?php

switch ($_POST["loan"])
{
    case '1000': // $_POST['loan'] is a STRING, not an integer.
    {
        $interest = .05; // 5%
    }
    break;

    case '5000':
    {
        $interest = .065; //6.5%
    }
    break;

    case '10000':
    {
        $interest = .08; // 8%
    }
    break;

    default:
    {
        echo "You didn't choose a loan package.\n";
        exit;
    }
}

$balance = round((float)$_POST['loan'], 2); // convert to float
$monthly = round((float)$_POST['month'], 2); // ditto


// Before we start the loop, test whether the monthly
// payment is enough to cover the interest:
$first_month_interest = round(($balance * $interest), 2);

if (($first_month_interest) >= $montly)
{
    echo "You will never pay off the loan. "
       . "Your monthly payment is too low.\n";
    exit;
}

for ($duration = 0; $balance > 0; ++$duration)
{
    $balance = round($balance * (1 + $interest) - $monthly, 2);
}


echo "It would take {$duration} months to pay off this loan at ";
printf("%.1f", ($interest * 100)); // nicely formats float
echo "%.\n";

?>


I hope this version makes a little more sense...


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #10 (permalink)  
Old November 8th, 2003, 04:04 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by Nikolai
When I read the code, I wonder why you have a while() loop at all. I mean, Rich is correct in diagnosing your problem -- an infinite loop... but why are you looping at all? What is it your loop is meant to do?
Well as far as that goes this code is exactly what appears in the book. So that's not exactly Wade's fault. Yes it really is that screwy.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
More than 150 Ajax links ajax JSP Basics 2 January 20th, 2007 02:46 AM
Ch5 : Form4.php pg 156-8 ... Not stepping iamdaniel BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 27th, 2006 12:58 PM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Chapter 5 page 150 - Wondering why.... potassium Beginning PHP 3 July 27th, 2003 12:05 AM



All times are GMT -4. The time now is 11:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.