I have a Wordpress website/blog here: [login to view URL]
I am trying to set up a way to search a database. See the “Carb Database” page.
When a food is input, and “Search” is pressed, I want to execute the php code below.
The database is not part of the Wordpress site but is hosted on the same server.
I can send you the database.
"Mcarbs",
"Fiber" => "Mfiber",
"Sugar" => "Msugar",
"Sodium" => "Msodium",
"Saturated Fat" => "Msatfat",
"Total Fat" => "Mlipid",
"Cholesterol" => "Mchol");
// Get the search variable from URL
$var = @$_GET['q'];
//trim whitespace from the stored variable
$trimmed = trim($var);
// rows to return
$limit = 1000;
// check for a search parameter
if (strlen($trimmed) == 0 || strlen($var) == 0) {
// echo "We don't seem to have a search parameter!";
exit;
}
//connect to your database
mysql_connect("localhost", "***", "***") or die("Unable to connect to database"); // host, username, password
mysql_select_db("***") or die("Unable to select database"); // database
//specify database
// DEPRECATED mysql_select_db("carbs") or die("Unable to select database");
// Build SQL Query
$query = "SELECT * FROM `carbs` WHERE `Desc` LIKE '%" . mysql_real_escape_string($trimmed) . "%' ";
// $query = "select * from carbs where Desc like "%$trimmed%"";
// $numresults = $query->num_rows();
// $numresults=mysql_query($query);
// $numrows=mysql_num_rows($numresults);
// $numrows = $query->num_rows(); ????
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s = 0;
}
// get results
$query .= " LIMIT $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// $result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "You searched for: \"" . strip_tags($var) . "\"";
echo "Results by food and serving sizes. "[No data]" means information not in database. Unless otherwise specified, results are in grams of carbs which is what we use to count our [login to view URL] Results KeyCarbs: Total carbsFiber: Total fiberSugar: How many of the total carbs are simple carbsSodium: Milligrams of saltSaturated fat: How much bad fatTotal fat: Good and bad fat combinedCholesterol: Milligrams of cholesterol";
$count = 1 + $s ;
// now you can display the results returned
while ($row = mysql_fetch_array($result)) {
// $food = $row["Desc"];
// $calories = $row["Energy_Kcal"];
// $protein = $row["Protein_(g)"];
echo "";
$count++ ;
echo "" . $row["Desc"] . " ";
echo "Serving size 1: " . $row["GmWt_Desc1"] . ": ";
foreach ($carbs_1 as $key => $value) {
echo $key . ": " . $row[$value . "1"] . ", ";
}
echo "";
echo "Serving size 2: " . $row["GmWt_Desc2"] . ": ";
foreach ($carbs_1 as $key => $value) {
echo $key . ": " . $row[$value . "2"] . ", ";
}
}
echo "";
$currPage = (($s/$limit) + 1);
//break before paging
echo "";
// next we need to do the links to other results
if ($s >= 1) { // bypass PREV link if s is 0
$prevs = ($s-$limit);
print " <
Please provide me with files/instructions. Thank you.