Control Structures
Control structures are the parts of your code that allow you to control the flow of execution of a program or script. There are to basic types; conditionals (or branching structures), and loops (or repetetive structures).
Conditionals
Conditionals are used to make descisions. The main conditional is the if statement. The if statement tests a condition and proceeds to execute the next statement if the condition is true. e.g.
if( $total_qty == 0 )
echo 'You did not order anything';
If this statement is added into you processOrder page, the if statement will be tested. If $total_qty (the total of the numbers typed into the textfields) then the echo statement will be executed. If the staement is false (i.e. not equal to 0), then the statement will be ignored. Note; if people pput words into the number fields rather than numerals they will be evaluated to 0 and the error message will be triggered
If you want to add more than one statement to your if conditional, then enclose the statements in curly braces {}. e.g.
if( $total_qty == 0 )
echo 'You did not order anything <br />';
echo 'you must fill in at least one category <br />';
echo 'Please try again<br />';
}
Here, three echo statements are executed if the condition is true.
If the condition tested is false you can use the else statement to provide an altrnative to be executed. If you rearrange your existing code, you can make it display either the error message, or the list pf items and prices.
if( $total_qty == 0 )
{
echo '<strong>You did not order anything</strong> <br />';
echo 'you must fill in at least one category <br />';
echo 'Please try again<br />';
}
else
{
echo '<p> Your order is as follows: </p>';
echo $hd_qty.' Hard Drives<br />';
echo $mon_qty.' Monitors<br />';
echo $pr_qty.' Printers<br />';
echo 'Number of items ordered: '.$total_qty.'<br />';
$total_amount = ($hd_qty * hd_price) + ($mon_qty * mon_price) + ($pr_qty * pr_price);
echo 'Subtotal: $'.number_format($total_amount,2).'<br />';
$tax_rate = 0.10; //GST = 10%
$total_amount = $total_amount * (1 + $tax_rate);
echo 'Total including tax: $'.number_format($total_amount,2).'<br />';
}
notice how all the code we have written so far to display the order has been put between the curly braces of the else staement. Only one of these two blocks of code will get executed, either the error messages, or the listing of the order.
Loops
The simplest type of loop in PHP is the while loop. Its basic structure is
while( condition) expression; for example;
$num = 1;
while ($num = 5 )
{
echo $num.'<br />';
$num++;
}
this code will list the numbers from 1 to 5. This is a typical way to use a loop. You set a counter and test the counter each time you run through the loop. You can write this style of loop by using a for loop. The initial value, final value and increment of the counter are set at the beginning, then the block of code is written inside curly braces. The example above would be written as follows.
for( $num=1; $num<=5; $num++ )
{
echo $num.'<br />';
}
