<?php
  if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>Functions - Old MacDonald</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">
        <link rel="stylesheet" href="../css/form.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>
<body>
    
<h2 class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  $urlPath=dirname($_SERVER['SCRIPT_URL']).'/'; 
  $labsPath=$urlPath.'labs/'; 
?>     

    <a href="../index.php">CS 130A</a> → Lab 8
<a href="<?=$urlPath?>index.php?source" target="labs">Source code</a>

</h2>   

    <section>
<h4>Old MacDonald</h4>
    <p class="nudge in">Pick an animal, or multiple animals, to place into Old MacDonald's Farm. You'll get back a nifty old folk song.</p>

 <form action="index.php" method=post name="sing" id="sing"> 
<div class="flex-center">
<div class="nudge farm-left">  

<!--    setting name="nee[]" makes the $_POST"nee" return the values as an indexed array    

values are sent over in pairs of animal:sound, with the : exploded to return a 2D Array     -->
    
<label for="pick">Farm Animals: </label> <br />   
    <label><input type="checkbox" name="pick[]" value="Cows:Moo" id="cow" /> Cows </label><br />
    
    <label><input type="checkbox" name="pick[]" value="Pigs:Oink" id="pig" /> Pigs </label><br />
    
    <label><input type="checkbox" name="pick[]" value="Horses:Neigh" id="horse" /> Horses </label><br />
    
    <label><input type="checkbox" name="pick[]" value="Chickens:Cluck" id="chicken" /> Chickens </label><br />
    

 
</div>   

<div class="nudge farm-right"> 
    
<label for="pick">  </label>    <br />
    <label><input type="checkbox" name="pick[]" value="Cats:Meow" id="cat" /> Cats </label><br />
    
    <label><input type="checkbox" name="pick[]" value="Dogs:Woof" id="dog" /> Dogs </label><br />
    
    <label><input type="checkbox" name="pick[]" value="Sheep:Baa" id="sheep" /> Sheep </label><br />
    
  
    <label><input type="checkbox" name="pick[]" value="Ducks:Quack" id="duck" /> Ducks </label>

    
</div>
 </div>    
<div class="pad-top flex-center">     
<input type="reset" value="Reset" class="button">    
     
<input type="submit" name="sing" value="Sing!" class="space button">
</div>     
      
</form>
       
</section>
    
<!--<pre>-->

<?php   
$pick = $print = $count = "";   //  defining nulls to prevent error on fresh pageload
    
$countP = 0;    //  count of animal:sound pairs in the $pick array
$countV = 0;    //  count of number of sung verses
$pair = [];     //  set to an empty array to populate with exploded $pick
    
    //  these things happen when the submit button is pressed
if ($_SERVER["REQUEST_METHOD"] == "POST") 
    {
    if (empty($_POST["pick"]))      # error msg if empty form is sent
        {
        $print = "<p class=\"pad-top\">You forgot to pick an animal. An Empty farm can't sing.</p>";
        }
    else        # if $_POST contains pick values
        {
        $pick = $_POST["pick"];     # define/populate the $pick array with form submitted values
        $count = count($pick);      // human count of the $pick array
        
        for ($countP = 0; $countP < $count; $countP++)  
            { 
            array_push($pair, explode(":", $pick[intval($countP)]));
            }
        
//        $pair = [animal][sound]   for personal reference
        
        
        # below is a failed attempt to put the main verse into a heredoc or function that can be called
        
        # I've completed the program for this assignment and it works as intended with a very long print statement, however, I did not get to implement a user defined function printOneVerse (here I called it simply $verse). I'm starting to think the $ in front of the function name it was what was causing the error.
        
        # In the previous week's assignment, Lab 7: Multiplication table, I've implemented a user defined function called function row($a), so is that acceptable?
        
//        function $verse($a, $s)  
//            {
//        $verse = <<< heredoc "<p class=\"txt-center\">
//        Old MacDonald had a farm<br />\n
//        E-I-E-I-O<br />\n
//        And on his farm he had some <span class=\"fancy\">$a<span><br />\n
//        E-I-E-I-O<br />\n
//        With a <span class=\"fancy\">$s-$s<span> here<br />\n
//        And a <span class=\"fancy\">$s-$s<span> there<br />\n
//        Here a <span class=\"fancy\">$s<span>, there a <span class=\"fancy\">$s<span><br />\n
//        Everywhere a <span class=\"fancy\">$s-$s<span><br />\n
//        Old MacDonald had a farm<br />\n
//        E-I-E-I-O<p> \n"
//        
//heredoc;    }
        
        
//        
//        for ($countV = 0; $countV < $count; ++$countV)
//            {
//            printf ($verse, $pair[intval($countV)][0], $pair[intval($countV)][1])
//            ;
//            }
        
        
        
    //  initiated when there are 0 printed verses; keep printing as long as the number of verses < number of animal/sound pairs; increment the verse count before initiating the print because $count starts counting at 1 instead of 0
            for ($countV = 0; $countV < $count; ++$countV)
            {
print "<p class=\"txt-center pad-top\">
        Old MacDonald had a farm<br />\n
        E-I-E-I-O<br />\n
        And on his farm he had some <span class=\"fancy\">{$pair[intval($countV)][0]}</span><br />\n
        E-I-E-I-O<br />\n
        With a <span class=\"fancy\">{$pair[intval($countV)][1]}-{$pair[intval($countV)][1]}</span> here<br />\n
        And a <span class=\"fancy\">{$pair[intval($countV)][1]}-{$pair[intval($countV)][1]}</span> there<br />\n
        Here a <span class=\"fancy\">{$pair[intval($countV)][1]}</span>, there a <span class=\"fancy\">{$pair[intval($countV)][1]}</span><br />\n
        Everywhere a <span class=\"fancy\">{$pair[intval($countV)][1]}-{$pair[intval($countV)][1]}</span><br />\n
        Old MacDonald had a farm<br />\n
        E-I-E-I-O<p> \n";
            }
            
        
        }
    
    }
   
   
   //   used for debugging and making sure the arrays are properly constructed
//   print_r ($pick); echo "\n";
//   print_r ($pair);
   
    #   $print is used to print an error message if no animals are selected, $count was used for debugging
?>     
  <?=$print?>   
<!--</pre>   -->

    
<!--
  
    <?=$count?>
    
-->
    
    <p class="hide">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
</p>

    <div class="stretch"></div>
</body>
</html>