<?php
include "phpheader.inc";
?>

<?php
//function for SHOW GLOBALS
function hide_info($secret_key,$secret_value) {
$noyb "NOYB";
            switch(
$secret_key) {
                    case 
DOCUMENT_ROOT:
                    
$secret_value $noyb;
                    return 
$secret_value;
                    case 
PATH:
                    
$secret_value $noyb;
                    return 
$secret_value;
                    case 
SCRIPT_FILENAME:
                    
$secret_value $noyb;
                    return 
$secret_value;
                    case 
PATH_TRANSLATED:
                    
$secret_value $noyb;
                    return 
$secret_value;
                    case 
UNIQUE_ID:
                    
$secret_value $noyb;
                    return 
$secret_value;
                    default:
                    return 
$secret_value;
                    }
//end switch
}//end hide info

function echo_keys_and_values_each($GLOBALS_array) {
    echo(
"<p class='center'>This code iterates the global array and any array within that array.</p>"); 
    echo(
"<table cellpadding='3' cellspacing='3' border='0'><tr><th width='50%'>Key:</th><th width='50%'>Value:</th></tr>");
    
reset($GLOBALS_array);
    while(
$returned_array each($GLOBALS_array)) {
            
$current_value $returned_array['value'];
            
$current_key $returned_array['key'];
            if(
is_array($current_value)) {
                while(
$internal_array each($current_value)) {
                    
$internal_value $internal_array['value'];
                    
$internal_key $internal_array['key'];
                    
$returned hide_info($internal_key$internal_value);
                    echo(
"<tr><td class='key'>($current_key)<br />$internal_key</td><td class='value'>$returned</td></tr>");
                }
//end while for internal arrays
            
}//end if is_array
            
else {
                    
$returned hide_info($current_key$current_value);
                    echo(
"<tr><td class='key'>$current_key</td><td class='value'>$returned</td></tr>");
            }
//end else to rest of program
        
}//end while
    
echo("</table>");
    }
//end function
?>

<div class="center">
<h2>Week 2 - Exercise 1</h2>
<div class="wideborder">
<a href="2exercise1.php?link=1">Show Global Variables and Bonus</a> |
<a href="2exercise1.php?link=2">Show Form - The Countdown</a> |<br />
<a href="2exercise1.php?link=3">Show All Data > than Threshold with % Matching</a> |<br />
<a href="2exercise1.php?link=4">Show Cities Information with % Matching</a> |<br />
<a href="php_content.php"><span class="white">Index of Assignments</span></a>
</div>
</div>
<?php
if($link==1) {
    
echo_keys_and_values_each($GLOBALS);
}
//end first if

if($link==2) { ?>
<div class="center">
<form action="2exercise1.php" name="form1" method="get">
<input type="hidden" name="countdown" value="recursive" />
<h3>Countdown using Recursion</h3>
<input type="text" name="num_arg" value="Enter an Integer" onfocus="this.form.num_arg.value=''" /><br />
<input type="submit" /><input type="reset" /></form></div>

<div class="center">
<form action="2exercise1.php" name="form2" method="get">
<input type="hidden" name="countdown" value="forloop" />
<h3>Countdown using a For Loop</h3>
<input type="text" name="num_arg2" value="Enter an Integer" onfocus="this.form.num_arg2.value=''" /><br />
<input type="submit" /><input type="reset" /></form></div>
<?php
}//end link 2 if
?>


<?php
//COUNTDOWN FUNCTION
function countdown($num_arg){
    if((
$num_arg 0) && ($num_arg 1000)) {
            echo(
"<tr><td class='value'>$num_arg</td></tr>");
            
countdown($num_arg 1);
    }
//end first if num_arg
}//end countdown function

//check to see if the $num_arg is has a value
if (IsSet($num_arg)) {
    if(
$num_arg 999) {
    echo(
"<p class='center'><b>Enter a number less than 1000 please!</b></p>");
    }
//end if
    
else {
    echo(
"<p class='center'>This is an example of a <strong>recursive</strong> function with a <strong>base case (nonrecursive branch)</strong>.</p>");
    echo(
"<p class='center'><table cellpadding='5' cellspacing='5' border='0' align='center'>");
    
countdown($num_arg);
    echo(
"</table></p>");
    }
//end else
}//end if IsSet

//here is the countdown using a for loop
if($num_arg2 0) {
    echo(
"<p class='center'>This is an example of doing the countdown using a <strong>for loop</strong> instead of <strong>recursion</strong>.</p>");
    echo(
"<p class='center'><table cellpadding='5' cellspacing='5' border='0' align='center'>");
    for(
$x $num_arg2$x 0$x--) {
        echo(
"<tr><td class='value'>$num_arg2</td></tr>");
        
$num_arg2--;
    }
//end for loop
    
echo("</table></p>");
}
//end countdown using a for loop
?>
        
<?php
if($link==3) {
?>
<div class="center">
<form action="2exercise1.php" method="get">
<input type="hidden" name="table" value="1" />
<h3>Temperature Threshold</h3>
<p>Enter a <strong>Threshold Temperature</strong> and view the city and country information:</p>
<input type="text" name="threshold" size="10" /><br />
<input type="hidden" name="tbg" value="#000080" />
<input type="hidden" name="bg1" value="#ffffff" />
<input type="hidden" name="bg2" value="#c0c0c0" />
<input type="submit" /> <input type="reset" /></form></div>
<?php
}//end if link 3
?>

<?php
if($link==4) {
?>
<div class="center">
<form action="2exercise1.php" method="get">
<input type="hidden" name="table" value="2" />
<input type="hidden" name="tbg" value="#000080" />
<input type="hidden" name="bg1" value="#ffffff" />
<input type="hidden" name="bg2" value="#c0c0c0" />
<h3>Show Cities</h3>
<p>Enter the country for which you would like to see city and temperature information.<br />Your choices are FR, UK, CA, US, GR, FI, AU, NL, TK.</p>
<p><strong>Enter the Country:</strong></p>
<p><small>Not case sensitive</small></p>
<input type="text" name="threshold" size="10" /><br /><br />
<input type="submit" /> <input type="reset" /></form></div>
<?php
}//end if link 4
?>

<?php
if(($table==1) || ($table==2)) {
include 
"2arraydata.inc";

function 
easy_func2($arr,$tbg,$bg1,$bg2,$threshold,$table) {
if(
$threshold == "") {
echo(
"<div class='center'><p>You did not enter any value so you get the whole table back</p></div>");
}
//end if empty string
echo "<div align='center'><br /><br /><table cellspacing='2' cellpadding='4' border='0' bgcolor=$tbg>"; echo "<tr><th class='white'>City</th><th class='white'>Temperature</th><th class='white'>Country</th><tr>";
    
$record_cnt=1;
    
$sizeof_array sizeof($arr);
    
$matched 0;
    while(list(
$key,$record)=each($arr)) {
            
extract($record);
            if(
$threshold == "") {
                
$bg = ($record_cnt++%2) ? $bg1 $bg2;
                
$matched++;
                echo 
"<tr bgcolor=$bg><td>$city</td><td>$temperature</td><td>$country</td></tr>";
            }
//end if false to print entire table if no values entered
            
else {
                if(
$table == 2) {
                    
$threshold strtoupper($threshold);
                }
//end to upper string if
                
if(($temperature $threshold) && ($table == 1)) {
                    
$bg = ($record_cnt++%2) ? $bg1 $bg2;
                    
$matched++;
                    echo 
"<tr bgcolor=$bg><td>$city</td><td>$temperature</td><td>$country</td></tr>";
                }
//end if threshold is a temperature
                 
else if(($threshold == $country) && ($table == 2)) {
                    
$bg = ($record_cnt++%2) ? $bg1 $bg2;
                    
$matched++;
                    echo 
"<tr bgcolor=$bg><td>$city</td><td>$temperature</td><td>$country</td></tr>";
                }
//end else if threshold is a country
            
}//end else
             
        
}//end while
    
if($table == 1) {
        
$percentage_over=sprintf('%6.4f', ($matched/$sizeof_array)*100);
        echo 
"<tr bgcolor=$bg><td colspan='2' bgcolor='$tbg' class='white'>Records Matched: $matched</td><td bgcolor='$tbg' class='white'>$percentage_over percentage</td></tr>";
    }
//end if table 1
    
else if($table == 2) {
        
$percentage_over=sprintf('%6.4f', ($matched/$sizeof_array)*100);
        echo 
"<tr bgcolor=$bg><td colspan='2' bgcolor='$tbg' class='white'>Records Matched: $matched</td><td bgcolor='$tbg' class='white'>$percentage_over percentage</td></tr>";
    }
//end else if table 2
echo "</table></div>";
    }
//end function for multiple array
    
easy_func2($data,$tbg,$bg1,$bg2,$threshold,$table);
}
//end if table 1 or table 2
?>



</body>
</html>