Web Design and Programming Pt 3 Looping

Objective C LoopingIn this web design and programming video tutorial I’ll show you how to use looping statements with PHP. If you missed the first part of this tutorial it is here Web Design and Programming.

I specifically will cover the following topics:

  • Using the $_REQUEST array to pass form data
  • The While Loop
  • The Do While Loop
  • The For Loop
  • How Break and Continue work

The code proceeds the video and you can use it in anyway you see fit. If you have any questions or comments leave them below.

[adsense]

The Code From the Web Design and Programming Tutorial

The HTML Form Code

<html>
<head>
<title><?php echo “Counting”;?></title>
</head>
<body>
<form action=”http://localhost/php/phploop.php” method=”post”>

<p>Print from 1 to:
<input type=”text” name=”countto” size=”30″ value=”” />
</p>

<p>Calculate Fibonacci to:
<input type=”text” name=”fibonacci” size=”30″ value=”” />
</p>

<p>
<input type=”submit” name=”submit” value=”Send” />
</p>

</form>
</body>
</html>

The PHP Code

<html>
<head>
<title><?php echo “Printing Numbers”;?></title>
</head>

<body>

<?php
$countTo = (int) $_REQUEST[‘countto’];
$startNum = 1;

while ( $startNum <= $countTo )
{
if( ($startNum % 2) == 0 )
{
echo $startNum, “, “;
}
elseif ($starNum >= 1000)
{
break;
}
else
{
$startNum++;
continue;
}
$startNum++;
}

echo “<br /><br />”;

$iterator2 = 101;
do {
echo $iterator2. “, “;
$iterator2++;
} while ($iterator2 <= 100);

echo “<br /><br />”;

$maxFib = $_REQUEST[‘fibonacci’];
$firSeqNum = 0;
$secSeqNum = 1;

for ( $iterator = 0; $iterator <= $maxFib; $iterator++)
{
echo $firSeqNum, “, “;
$sum = $firSeqNum + $secSeqNum;
$firSeqNum = $secSeqNum;
$secSeqNum = $sum;
}

?>

</body>
</html>

4 Responses to “Web Design and Programming Pt 3 Looping”

  1. Steve Arthur says:

    My fibonacci series calculates to 0. I’m not sure what it is that I missed. The code looks good and the files all load. Any suggestions?

    Thanks. The tutorials are awesome!

    • admin says:

      I’m not sure. Did you copy and past the code directly from the site? Make sure you replace all back quotes with regular quotes if you copy and paste. That was a security thing that I have gotten rid of in more recent tutorials

  2. Wardak says:

    Hi there,

    Nice tutorial videos; I just have one question: the main condition in the while loop is $startNum = 1000); we know that the $startNum will never pass 100 as it is restrained by the while loop’s main condition.

    • admin says:

      Often when I’m making these tutorials I’m just trying to demonstrate as many things as possible. That doesn’t always allow for perfect code writing. As the tutorials progress I focus on writing good code. I hope that makes sense

Leave a Reply

Your email address will not be published.

Google+