<?php

//Connecting to DB
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect to the Krecipes database-server!');
mysql_select_db($dbname);
function unescape_and_decode($string)
{
    $result = str_replace(";@", ";", $string);
    $string = utf8_decode($string);
    return $string;
}
//Versions for footer
$query = "SELECT ver,generated_by FROM db_info";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$ver = unescape_and_decode($row[0]);
$generated_by = unescape_and_decode($row[1]);
?>
</h2>

<?php 
// Get recipes listed by category
$query = "SELECT id,name FROM categories WHERE parent_id=-1 ORDER BY name";
$result = mysql_query($query);
$number = mysql_num_rows($result);
$i = 0;
if ($number == 0) {
    print "{$no_db_listings}";
} elseif ($number > 0) {
    while ($i < $number) {
        $id = mysql_result($result, $i, "id");
        $name = mysql_result($result, $i, "name");
        $name = unescape_and_decode($name);
        $uppercase_begin = strtoupper($name[0]);
        if ($uppercase_begin != $at) {
            $cat_list .= "<hr />\n";
            $cat_list .= "<a name=\"{$uppercase_begin}\">\n";
            $cat_list .= "<p class=\"index_header\">{$uppercase_begin}</p>\n";
            $cat_index .= " [ <a href=\"#{$uppercase_begin}\">{$uppercase_begin}</a> ] ";
        }
        $at = $uppercase_begin;
        $cat_list .= "<a href=\"view_cat.php?id={$id}&cat_name=" . urlencode($name) . "\">{$name}</a><p></p>";
        $i++;
    }
}
print $cat_index;
print $cat_list;
?>
    $name = mysql_result($result, $i, "name");
    $name = unescape_and_decode($name);
    print "<a href=\"view_cat.php?id={$id}&cat_name=" . urlencode($cat_name . "->" . $name) . "\">{$name}</a><p></p>";
    $i++;
}
?>

<hr />

<h2><?php 
echo "{$lng_cat_recipes}";
?>
:</h2>

<?php 
$query = "SELECT r.id,r.title,cl.category_id,cl.recipe_id FROM recipes r,\n     category_list cl WHERE cl.category_id={$cat_id} AND cl.recipe_id=r.id";
$result = mysql_query($query);
$number = mysql_num_rows($result);
$i = 0;
if ($number == 0) {
    print "{$no_cat_listings}";
} elseif ($number > 0) {
    while ($i < $number) {
        $id = mysql_result($result, $i, "r.id");
        $title = mysql_result($result, $i, "r.title");
        $title = unescape_and_decode($title);
        print "<a href=\"view_recipe.php?id={$id}\">{$title}</a><p></p>";
        $i++;
    }
}
require 'footer.php';
 if ($ingredient_group_id != -1) {
     $command = "SELECT name FROM ingredient_groups WHERE id={$ingredient_group_id}";
     $g_result = mysql_query($command);
     $ingredient_group = mysql_result($g_result, 0, "name");
 }
 $ingredient_amount = mysql_result($result, $i, "il.amount");
 $ingredient_amount_offset = mysql_result($result, $i, "il.amount_offset");
 $ingredient_amount_display = $ingredient_amount;
 if ($ingredient_amount_offset > 0) {
     $ingredient_amount_display .= "-" . ($ingredient_amount_offset + $ingredient_amount);
 }
 $ingredient_list_id = mysql_result($result, $i, "il.id");
 $ingredient_unit_id = mysql_result($result, $i, "u.id");
 $ingredient_units = mysql_result($result, $i, "u.name");
 $ingredient_name = unescape_and_decode($ingredient_name);
 $ingredient_units = unescape_and_decode($ingredient_units);
 $command = "SELECT pl.prep_method_id,p.name FROM prep_methods p, prep_method_list pl WHERE pl.ingredient_list_id={$ingredient_list_id} AND p.id=pl.prep_method_id ORDER BY pl.order_index";
 $prep_method = "";
 $p_result = mysql_query($command);
 $p_number = mysql_num_rows($p_result);
 $j = 0;
 while ($j < $p_number) {
     if ($j != 0) {
         $prep_method .= ", ";
     }
     $prep_method .= mysql_result($p_result, $j, "p.name");
     $j++;
 }
 if ($prep_method != "") {
     $prep_method = "; " . $prep_method;
 }
<?php

// Read title, yield, instructions and prep_time
$query = "SELECT instructions,yield_amount,yield_amount_offset,yield_type_id,title,prep_time FROM recipes WHERE\nid={$recipe_id}";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$recipe_instructions = str_replace("\n", "<br />", unescape_and_decode($row[0]));
$recipe_yield_amount = unescape_and_decode($row[1]);
$recipe_yield_amount_offset = unescape_and_decode($row[2]);
$recipe_yield_type_id = unescape_and_decode($row[3]);
$recipe_title = unescape_and_decode($row[4]);
$recipe_preptime = unescape_and_decode($row[5]);
mysql_free_result($result);
//Simple check to see if recipe exists
if ($row == 0) {
    die("{$no_exist_recipe}");
}
// Read yield type
$query = "SELECT name FROM yield_types WHERE id={$recipe_yield_type_id}";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$recipe_yield_type = $row[0];
$recipe_yield = $recipe_yield_amount;
if ($recipe_yield_amount_offset > 0) {
    $recipe_yield .= "-" . ($recipe_yield_amount_offset + $recipe_yield_amount);
}
$recipe_yield .= " {$recipe_yield_type}";
mysql_free_result($result);
// Read authors
$command = "SELECT al.author_id,a.name FROM author_list al, authors a WHERE recipe_id={$recipe_id} AND al.author_id=a.id";
$result = mysql_query($command);