<?php
// Redirect if page was accessed directly
if(!defined('BASE_URL')) {
//Need the base url, defined in the config file
require_once ('./config/config.inc.php');
//Redirect to the index page
$url = BASE_URL . 'index.php?p=view_recipes';
header ("Location: $url");
exit;
}
// This page displays the recipes from the database.
require_once (DB_REC);
$display = 5; //set number of records per page
if (isset($_POST['submitted'])) { // Handle the form.
// Retrieve the recipes for a particular category, if selected.
// Make sure the type is an integer.
if (isset($_POST['type'])) {
$type = (int) $_POST['type'];
} else {
$type = 0;
}
if (isset($_POST['np'])) { // already been determined
$num_pages = $_POST['np'];
} else { // need to determine
// count the number of records
$cntquery = "SELECT COUNT(*) FROM recipes, category_assoc
WHERE recipes.recipe_id = category_assoc.recipe_id AND category_assoc.category_id = $type";
$cntresult = mysql_query ($cntquery);
$rowcnt = mysql_fetch_array ($cntresult, MYSQL_NUM);
$num_records = $rowcnt[0];
// calculate number of pages
if($num_records > $display) { // more than 1 page
$num_pages = ceil($num_records/$display);
} else {
$num_pages = 1;
}
} // end of np if
//mysql_free_result ($cntresult); // free up resources
if(isset($_POST['s'])) {
$start = $_POST['s'];
} else {
$start = 0;
}
if ($type > 0) {
// Get the current category name.
$cnquery = "SELECT category FROM categories WHERE category_id=$type";
$cnresult = mysql_query ($cnquery);
list ($category) = mysql_fetch_array ($cnresult, MYSQL_NUM);
//echo mysql_error();
"<div align=\"left\"><b>$category recipes</b>";
//mysql_free_result ($cnresult); // free up resources
// Query the database.
$mainquery = "SELECT recipes.recipe_id, title, description, method, submitter FROM recipes, category_assoc
WHERE recipes.recipe_id = category_assoc.recipe_id AND category_assoc.category_id = $type ORDER BY title ASC LIMIT $start, $display";
$mainresult = mysql_query ($mainquery) or die(mysql_error());
// table header
echo '<table class="lists">
<tr>
<td align="left" width="140px"><font size="+1">Title</font></td>
<td align="left" width="360px"><font size="+1">Description</font></td>
<td align="left" width="120px"><font size="+1">Submitted by</font></td>
</tr>';
$bg = '#eeeeee'; //set background colour
// Display all the recipes.
while ($row = mysql_fetch_array ($mainresult, MYSQL_ASSOC)) {
// Display each record.
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //switch the background colour
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="index.php?p=view_a_recipe&rid=' . $row['recipe_id'] .'">' . $row['title'] . '</a></td>
<td align=\"left\">' .$row['description'] .'</td>
<td align=\"left\">' .$row['submitter'] .'</td>
</tr>';
}
echo '</table>';
//mysql_free_result ($mainresult); // free up resources
// make links to other pages
if ($num_pages > 1) {
echo '<br /><p>';
//determine which page the script is on
$current_page = ($start/$display) + 1;
//if it's not the first page, make a previous button
if($current_page != 1) {
echo '<a href="index.php?p=view_recipes&s=' . ($start - $display) . '&np= ' . $num_pages . '">Previous </a> ';
}
//make all the numbered pages
for ($i = 1; $i <= $num_pages; $i++) {
if($i != $current_page) {
echo '<a href="index.php?p=view_recipes&s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"> ' . $i . ' </a>';
} else {
echo $i . ' ';
}
}
// if it's not the last page, make a next button
if($current_page != $num_pages) {
echo '<a href="index.php?p=view_recipes&s=' . ($start + $display) . '&np=' . $num_pages . '"> Next</a>';
}
echo '</p>';
}
} // End of while loop.
//echo '<pre' . print_r($_POST,1) . '<pre>';
} else {
// Create a form allowing the user to select a category to view.
echo '<div align="left">
<form action="index.php?p=view_recipes" method="POST">
<select name="type">
<option value="NULL">Choose a category:</option>
';
// Retrieve and display the available categories.
$catquery = 'SELECT * FROM categories ORDER BY category ASC';
$catresult = mysql_query ($catquery);
while ($row = mysql_fetch_array ($catresult, MYSQL_NUM)) {
echo "<option value=\"$row[0]\">$row[1]</option>";
}
//mysql_free_result ($catresult); // free up resources
// Complete the form.
echo '</select>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="submitted" value="TRUE">
</form>
</div>
';
}
mysql_close(); // close database connection
?>