Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/michaelbeeson_old/recipes/
Upload File :
Current File : /usr/home/beeson/public_html/michaelbeeson_old/recipes/DisplayRecipe.php

<?php 
session_start();
 require_once('writeAsHTML.php');
$Username = $_GET['Username'];   // possibly passed in from referring page
$RecipeId = $_GET['RecipeId'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php 
function getName($FirstName, $LastName)
{  $Ans = $FirstName . " " . $LastName;
    return $Ans;
}
function writeRed($errmsg)
{ echo 	"<P> <em><font color=\"#FF0000\">";
  echo $errmsg;
  echo "</font></em></P>";
}
function normalizeUnit($str)
// return Tbsp for tbsp and T, etc.
{ if(strtolower($str) == 'tbsp') return 'Tbsp.';
  if($str =='T') return 'Tbsp.';
  if($str =="C") return $str;
  return $str . ".";
}  
  
function Unit($str)
// is $str a unit acceptable in a recipe?  If so return the standard form.  If not return "".
{ $units = array('c','cup', 'cups', 't', 'tsp', 'tbsp', 'oz', 'ounce', 'ounces',
                 'lb', 'pound', 'pounds', 'pint', 'pints', 'pinch', 'pinches',
				 'handful', 'handfuls', 'bunch', 'bunches', 'gram', 'g', 'grams',
				 'kg', 'kgs', 'kilogram', 'kilograms', 'ml', 'cc', 'gallon', 'gallons', 'quart',
				 'quarts', 'qt', 'qts','gal', 'gals');
  // first strip off a terminating period if there is one
  $str = trim($str);
  $k = strlen($str);
  $lc = strtolower($str);	 
  if(in_array($lc,$units))
     return  normalizeUnit($str);
  $k = strpos($lc,".");
  if($k)
     $s = substr($lc,0,$k);
  if(in_array($s,$units))
     return normalizeUnit($s);
  return "";
}  

function isInteger($str)
// return true if $str represents a positive integer
{ $n = strlen($str);
  for($i=0;$i<$n;$i++)
     if($str[i] < '0' || $str[i] > '9')
	      return false;
  return true;
}  		  
 
function isNumber($str)
// test if $str represents an integer or a rational number, p/q with spaces allowed near /.
// return true if so, false if not.
{  $a = explode("/",$str);
   if(count($a) == 1 && isInteger($a[0]))
      return true;
   if(count($a) == 2 && isInteger($a[0]) && isInteger($a[1]))
      return true;
   return false;
}

function isFraction($str)
// test if $str has the form p/q 
{ $a = explode("/", $str);
  if(count($a) == 2 && isInteger($a[0]) && isInteger($a[1]))
      return true;
   return false;
 }
	
function formatIngredients($Ingredients)
// format the ingredients of a recipe in a table or group of tables.
// lines beginning with a non-number mark a new group of ingredients
// lines that begin with a number are in the format number [unit] text
// These three pieces are put in the columns of a table and the table is echoed.
{  $rows = explode("\n",$Ingredients ); 
   $group = 0;
   $nextrow = 0;
   $breakers  = array('', 'sauce', 'filling', 'marinade', 'icing', 'frosting', 'crust'); 
   foreach($rows as $row)
	  { $newgroup = false;
	    $first = strtok($row," ");
	    $lfirst = strtolower($first);
		if(in_array($lfirst,$breakers))
		   { if($group > 0)
		       echo "</table></P>";
		     echo "<P><em><h4>$row<h4><\em></P>";
		     ++$group; 
			 $nextrow = 0;
			 continue;
		   }
		if($nextrow == 0)
		    echo "\n<P><table border = \"1\"  cellpadding=\"5\">\n";
        if(isNumber($first))
   		   { echo "\n<tr>\n   <td>$first";
		     $second = strtok(" ");
			 if($second == '/' || $second == '-')  // - in Recipes means "to" as in 2-4  apples
			    { $third = strtok(" ");
				  if(isNumber($third))
    			      { echo $second . $third;
					    $second = strtok(" ");
				      }
	            }
		     if(isFraction($second))  // as in 2 1/2 C flour
			    { echo " $second";
				  $second = strtok(" ");				  
				}				
		     echo "</td>\n";  // end of this column
		   }
		else 
		   { echo "\n<tr>\n   <td> </td>\n";  // make an empty number column
             $second = $first;
		   }
	    if($second != FALSE)
		   { $unit = Unit($second);
		      echo "   <td> $unit </td>\n";  // $unit can be an empty string, in which case this column is blank
			 if($unit == "")
			    $third = $second;
	         else
            	$third = strtok(" ");
		   }
		else 
           $third = $second;
	    if($third != FALSE)
		   $rest = substr($row,strpos($row,$third));
		else
		   $rest = "";
        echo "   <td> $rest </td>\n";
        echo "</tr>";
		++$nextrow;

	  }
	echo "</table></P>\n\n";
}	
		
		
require_once('query.php');
$sql = "SELECT * FROM Recipes WHERE RecipeId='$RecipeId';";
$q = Query($sql);
if($q->numRows() == 0)
    // got here from Back button after deleting this recipe
     $deleteflag = true;	
else	 
   { $deleteflag = false;
     $q->fetchInto($rows);
     $PersonId = $rows[1];
     $RecipeName = $rows[2];
     $Ingredients = $rows[3];
     $Directions = $rows[4];
     $RecipeType = $rows[5];
     $Nationality = $rows[6];
     $PhotoFile = $rows[7]; 
     $sql = "SELECT * FROM People WHERE PersonId='$PersonId';";
     $q = Query($sql);
     $q->fetchInto($rows);
     $FirstName = $rows[2];
     $LastName = $rows[1];
     $Name = getName($FirstName, $LastName); 
  }
?>
<html>
<head>
<title><?php echo  $RecipeName ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body BGCOLOR="#ffffcc">
<?php 
if($deleteflag)
   writeRed("Recipe not found.  Probably you just deleted it.");
else 
   { echo "<h3>"; writeAsHTML($RecipeName); echo"</h3>";
     if ($Nationality == "None")	 
        $Nationality = "";
     if ($RecipeType == "Other")
        $RecipeType == "";
     else if($RecipeType == "MainDish")
        $RecipeType = "Main dish";
     else if($RecipeType == "BeansAndTofu")
        $RecipeType = "Beans and tofu";   
     else if($Nationality != "")
        $RecipeType = strtolower($RecipeType);   
     echo "<P><em> $Nationality $RecipeType <br>";
     echo "posted by $Name </EM></P>";
?>
    <P><h3>Ingredients</h3></P>
<?php formatIngredients($Ingredients); ?>
    <P><h3>Directions</h3></P>
    <?php writeAsHTML($Directions); 
    if(!is_null($Username))
       {    // offer options to edit or delete this recipe
   ?>   
        <form name="EditForm" method="post" action="UploadRecipe.php?<?php echo "Username=$Username&OldRecipeId=$RecipeId";?>">
            <input type="submit" name="EditRecipe" value="Edit this recipe">
        </form><BR>
        <form name="DeleteForm" method="post" action="DeleteRecipe.php?<?php echo "Username=$Username&DeleteRecipe=$RecipeId";?>">
            <input type="submit" name="DeleteRecipe" value="Delete this recipe">
        </form>
<?php 
       }  //if 
  } // else 
?>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists