• I have a WordPress site/blog set up here: http://dietthatreallyworks.com

    I have a database of foods set up on my server but not as part of the WordPress site.

    I’m trying to search the database and show results on the WordPress site.

    For Carb Database, I set up a search page as explained here: http://codex.wordpress.org/Creating_a_Search_Page#Using_the_page.php

    For Carb Database Alt, I set up a default page with this code:

    <form name=”form” action=”search.php” method=”get”>
    <input type=”text” name=”q” size=”50″ />
    <input type=”submit” name=”Submit” value=”Search” />

    I uploaded searchpage.php and search.php into the theme directory. I also uploaded them into the WordPress directory. The code for search.php is below.

    On a non-Wordpress site, I tested by using a page called index.html with the Carb Database Alt code and another page called search.php with the code below. It worked there. I can’t get it to work in WordPress.

    Questions:

    1. How do I get the search function to work – whether through Carb Database or Carb Database Alt?

    2. How do I keep from having to open another page to get to the search box and get the search results to appear on the search page with the search box so that the user can search again without leaving the page?

    Here is the code for search.php:

    <?php

    $carbs_1 = array(“Carbs” => “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(“***”) 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 carbs.

    <center>Search Results Key</center>

    • Carbs: Total carbs
    • Fiber: Total fiber
    • Sugar: How many of the total carbs are simple carbs
    • Sodium: Milligrams of salt
    • Saturated fat: How much bad fat
    • Total fat: Good and bad fat combined
    • Cholesterol: 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 “<h3>” . $row[“Desc”] . ” </h3>”;
    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 ” <<
    Prev 10 “;
    }

    // calculate number of pages needing links

    $pages = intval($numrows / $limit);

    // $pages now contains int of pages needed unless there is a remainder from division

    if ($numrows % $limit) {
    // has remainder so add one page

    $pages++;
    }

    ?>

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to connect search form with external database’ is closed to new replies.