Control Structures in programming languages
The clips on this page refer to the sample
LinkToReports.html and a PHP script, DonationReports.php. It
would be a good idea to get these scripts in your editor now so you
can refer to them. The URLs used in the <a href= tags in the
html file LinkToReports contain GET data after the ?, where the
variable 'WhichReport' has it's value hard coded to indicate which
report the user has clicked for. DonationReports is a php script
that expects WhichReport to contain one of the reports it's been
programmed to provide.
Programming languages support Structure by including
syntax to support structured alternative selections and loops.
All programming languages support 'sequences' naturally -- each
programming statement (line) is executed sequentially unless one of
the language's 'control structures' is encountered. These
control structures provide the other two logical structures of
'alternative selections' and 'loops'.
Alternative selections and Loops are what allow a
program to provide different results each time it runs. These
'branching statements' allow the script to test conditional statements
for logical true or false and take a 'non-sequential' path thru the
script depending on the data encountered.
Most modern languages provide at least three types
of structured alternative selections: if/then; if/then/else; and some
form of if/then/elseif/else (commonly called case
selection).
if/then works where there is only one alternative,
as might be the case when the DBMS is not available when a web browser
requests a report. In PHP the structure, with no 'white space'
is:
if (cond) {action; action;}
if/then/else is used if there are only two
alternatives: one taken if the condition tested is true; the other
taken if the condition is false. It looks like this:
if (cond) {1st action if true; action; action;} else
{action if false;}
Look at how these logical structures are supported
by PHP to provide a web user with the report they select. In the
sample script, this if/then control structure is used to control
production of the reports.
The links on LinkToReports.html
include a 'GET variable and data' after a question mark (?) in the url
for each <a href= link that points to DonationReports.php. If the browser doesn't set $_GET["WhichReport"]
a gentle error message is returned instead of a report. Notice
the URL displayed by Internet Explorer in the screenshot below, where
the mouse-pointer (not shown) is hovering over the 'Donations by
Region' link.

Here is the HTML for this page.
The GET variable after the ? is WhichReport. GET data are placed
after an =. Each link to DonationReports provides a value for
WhichReport that the script will accomodate. Note that quotes or
special 'URL encoding' is not required if the GET data contains no
spaces:

DonationReports.php is the 'resource' referenced in
the URL shown here. Unlike an ordinary, static webpage, this page will be
passed to PHP by the Apache server. Any statements between 'get
into PHP' and 'get out of PHP' tags (<? and ?>, respectively)
get passed to PHP, and the output (mostly from 'print' statements) is
substituted in the html.
It's an ordinary thing for a PHP script for a page
with 'dynamic content' to be made the way DonationReports.php
is: At the beginning of the file there is PHP code that puts
stuff into variables that are shown to the user. Here, the
'long' PHP script between the first <? and ?> makes two
variables, $Links and $Report, that are used in the 'short' html
section at the end. When they're needed in the html, the
programmer can 'jump into PHP' using the PHP tags in the html.
Here, these two PHP lines are 'embedded' in the html at the bottom:
<?
print "$Report";
print "<br><br>$Links";
?>
Let's look at the script's 'control structures'
first, then look at the other syntax.
Here's a part of the script that shows both if/then
and if/then/else logic being discussed...

If/then/else logic is used to control display of the
report: IF the GET variable has not been set (!isset means 'Not Set')
then $Report gets a terse message; ELSE the script goes on to attempt
connecting to the database and look to see what's in $_GET["WhichReport"].
Look at the script in your editor to see this whole
structure. There is only one statement executed, line 6, if (!isset$GET_["WhichReport"])
is true. If it is 'NOT NOT isset' (two NOTs evaluate as TRUE)
lines 8 thru 62 are executed.
Also, notice how the logical structures are
emphasized in the script by accurate punctuation. The
'controlled statements' are uniformly indented within the 'controlling
statements, making it easier to see where logical structures begin and
end.
The if/then statements are associated with 'error
handling'. If a connection to MySQL cannot be established or the
EBUS202 database is not available, there is nothing much that _can_ be
done, so the 'then statements' print an error message and exit the PHP
script prematurely.
if/then/elseif/else is used when there
are more than two alternative and the requirement is that only one of
them should be executed. Without white space it looks like
this:
if (cond1) {stmts1;} elseif (cond2) {stmts
for 2;} else {stmts if not 1 and not 2;}
In the sample script, case logic is
used to examine the GET data from the URL put in the variable $WhichReport.
There is a section of code to provide each of the 4 reports that
DonationReports.php is programmed to provide, and an error message is
included after the 'else' statement to provide a response if for some
reason the URL includes a value for WhichReport which is not accommodated
by the script.

Loops are the third of the structured
logical structures. Modern languages provide at least three
types: while/loop; do/until loop; and for/next loop. We'll look
at the while loop first, since it's probably most often
appropriate. Look to the text for other examples of loops.
Loops are usually used to process
'sequential data structures' like records in a file or results from a
database query. In PHP, it's convenient to process the results
from an SQL query using a while loop that processes the statements
inside itself as long as (while) there are rows in the result.
Here are a MySQL query and the results
returned to the terminal:

In a script, the equivalent is returned
to a 'result set' that the PHP script can 'see', one row at a
time.
$SQLStmt = "select MemberName,
Amount from Donations order by Amount desc limit 10";
$TopTenResults = mysql_query($SQLStmt);
When the mysql query is executed, there
will be one 'row' in the result for each record selected from the
table. This particular SQL statement limits the results to 10
rows since we're interested in seeing the Top Ten Donations if we
click the link for this report.
Here is a the general form of a while
loop in PHP without any much white space:
while ($ARow =
mysql_fetch_assoc($SomeResult)) { statements to handle $ARow;}
Here is the section of script that
prepares the TopTenInTable report.

PHP uses mysql_fetch_assoc to get the
'next' row from $SomeResult and put it in the associative array $ARow.
When there are no more rows in the result set, PHP's control is
transferred to the first statement _after_ the loop, where the table's
end tag is placed on $Report. In the loop, first the extract
statement is used to put data from the 'associative array' $ARow into
'simple variables' named the same as the column names: $MemberName and
$Amount.