Brian Grayless
| |
Samantha,
Try this out...
// make database connection
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
// if displaying a recipe of the week, you'll have to have
// some way of setting the row ID from the database to the
// variable below. You can do it using programming, or store it
// somewhere else in the database. Same with doing it random.
// Here, I just set it.
// this is the ID of the recipe in your database
$id_to_use = 23;
$query = "SELECT * FROM recipe_table WHERE id = '" . $id_to_use . "'";
$result = mysql_query($query);
// if you were expecting more than one result, you can loop
// through the result using
//while($row = mysql_fetch_array($result))
//{
// set database info to local variables
// $recipe_name = $row["name"];
//}
// otherwise, use
$row = mysql_fetch_array($result);
// set database info to local variables
$recipe_name = $row["name"];
$recipe_details = $row["details"];
$output_html = "
" . $recipe_name . " |
" . $recipe_details . " |
";
echo $output_html;
// searching can be a bit more complicated. You would first
// build your HTML form that would send the criteria you want
// to search on, probably a keyword field and maybe a category.
// Then, on your PHP script that received the variables you
// would do something like:
// LIKE %XXX% searches for words containing XXX
$query = "SELECT * FROM recipe_table WHERE name LIKE '%" . $_REQUEST["keyword"]. "%' AND category = '" . $_REQUEST["category"] . "'";
$result = mysql_query($query);
$output_html = "
";
while($row = mysql_fetch_array($result))
{
// set database info to local variables
$recipe_name = $row["name"];
$recipe_details = $row["details"];
$output_html .= "
" . $recipe_name . " |
" . $recipe_details . " |
";
}
$output_html .= "
";
echo $output_html;
// it's good practice to close the database link at the end
// of your script.
mysql_close($link);
I didn't test this, it's all from memory, so it may have a few bugs in it. The search is also a very simple search, but it should give you the idea of what needs to happen.
I believe there is also an opensource recipe program that you can look at on freshmeat.net, just do a search for recipe under PHP programs.
Let me know if you continue to have problems and good luck!
Brian Grayless
>What I am wanting to build is a page that displays either a recipe of the week or a random recipe-with a search function to search the database for a particular recipePrivate Reply to Brian Grayless (new win) |