<html><head><title>Rhyming Dictionary</title></head><body>
<h2>Welcome to the Rhyming Dictionary</h2>
<em><small>Exactly what it sounds like.</small></em>
<br><br>
<?php
//.........................................................................
// Program includes:
include("../copyright.php");
include("findRhymes.php");
include("dbConnect.php");


// Program constants:

$rootWord = @$_GET['word'];
?>

<form method=GET action="">
  Enter a word: <input name=word type=text value="<?php echo($rootWord);?>">
  <input type=submit value="Go!">
</form>

<?php
if($rootWord != NULL)
{
   // *** Important security measure: ***
   //
   // Make sure someone evil doesn't type the equivalent of:
   //    word = "\" or id != \"-1";
   //
   // First, escape slashes.  Then, escape quotation marks.  Finally,
   // fix carriage returns.
   $patterns = array("/\\\\/","/\"/","/\\n/");
   $replacements = array("\\\\","\\\"","\\n");
   $rootWord = preg_replace($patterns, $replacements, $rootWord);

   // Attempt to retrieve the phonetic information from the database
   $query = "SELECT phonetics from rhymeDict WHERE word = \"$rootWord\"";
   $result = @mysql_query($query);

   // If there were no sql errors and if there was at least one result...
   if($result && $row = mysql_fetch_row($result))
   {
      // Retrieve the phonetics information for this word.
      $rootPhonemes = getPhonemes($row[0]);

      // Print a helpful status message:
      echo("Results for $rootWord (pronounced \"".join($rootPhonemes,"-").
           "\"):<br>");


      // Here's how this block works internally:
      //
      // 1. Find the total number of sounds (num) in the root word
      // 2. Do a loop that goes that many times:
      //    First time, look for words that match all of the sounds
      //       (homonyms).
      //    Second time, look for words that match the last (num - 1)
      //       sounds.
      //    Third time, look for words that match the last (num - 2)
      //       sounds
      //    Etc.
      // The loop does not print anything if no matches are found.
      $num = count($rootPhonemes);
      for($i = 0; $i < $num; $i++)
      {
         findRhymes($rootPhonemes, $num - $i, 10);
      }
   }
   else
   {
      echo("Sorry, but \"$rootWord\" is not contained in the dictionary.");
   }
}
?>
<br><br>

<small>
Source files:<br>
<a href="index.php.html">index.php</a><br>
<a href="findRhymes.php.html">findRhymes.php</a><br>
<a href="dbConnect.php.html">dbConnect.php</a><br>
<a href="rhymer.php.html">rhymer.php</a>
</small>
<?php copyright("December 2008"); ?>
</body></html>