Jump to content

Recommended Posts


Does anybody have experience with coding an advanced search?

 

Like with using multiple fields for the search.

 

Here is my most updated code now the page goes blank when I do a search...I sort of got help last time, but no:

 

Also here is the page that the search menu is on: http://devel.ecountrylifestyle.com/green_thumb_gardener/resources/facilities.php?fac=A

 

<?php
//connect
include('../../includes/PHP/globals_db.inc');
$dbconn = mysql_connect($ecl_database_server, $ecl_username, $ecl_password) or die(mysql_error());
$select = mysql_select_db($ecl_database, $dbconn) or die(mysql_error());
//------------

//first lets make an array of letters A-Z
$alpha = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z");

$links_top = "";
foreach ($alpha as $links) {
$links_top .= "<a class = \"checklist\" href = 'facilities.php?fac=$links'>";

if ($links == "Z") {
	$links_top .= "$links</a>";
} else {
	$links_top .= "$links</a> | ";
} //end if
} //end if

if (!isset($_GET

)) {
$page = 1;
} else {
$page = $_GET

;
} //end if

$max_results = 10;

$from = ($page * $max_results);

if (isset($_POST[submit])) {
session_destroy();
//start the query statement for the search fields-starting with key word for default
$query = "SELECT * FROM pub_gardens WHERE facility LIKE '%$_POST[key_word]%'";

//if the user selects a type of facility from drop down menu, add on to the query string
//also make sure the first value of the drop down for the facility selection isnt the first
if ($_POST[type] != "Type of Facility") {
	$query .= " OR type = '$_POST[type]'";
} //end if

//if the user selects a state from drop down menu, add on to the query string
//also make sure the first value of the drop down for the state selection isnt the first
if ($_POST[state] != "State") {
	$query .= " OR state = '$_POST[state]'";	
} //end if

//if the user types in a city in the city field from the form, add on to the query string
if ($_POST[city] != "") {
	//make sure the default value isnt "City" before adding on to the string
	if ($_POST[city] != "City") {
		$query .= " OR city LIKE '%$_POST[city]'";
	} //end if		
} //end if

//if the user types in a zip code in the zip field from the form, add on to the query string
if ($_POST[zip] != "Zip Code") {
	//make sure the default value isnt "Zip" before adding on to the string
		$query .= " OR zip LIKE '%$_POST[zip]'";
} //end if

//add on last addition to the $query string variable
$query .= " ORDER BY facility LIMIT $from, $max_results";

//set session variables
session_start();
$_SESSION['form_type'] 		= $_POST[type];
$_SESSION['form_state']		= $_POST[state];
$_SESSION['form_city']		= $_POST[city];
$_SESSION['form_zip']		= $_POST[zip];
$_SESSION['form_key_word'] 	= $_POST[key_word];
//query session
$_SESSION['query'] = $query;
} else {
$query = $_SESSION['query'];
} //end if

$result = mysql_query($query) or die(mysql_error() . " : " .__LINE__);

//start table
$display = "<font color = \"#ff0000\">*Types (A = Arboretum, G = Garden, C = Conservatory, P = Park)</font>\n";
$display .= "<table border = 0 width = 500 cellspacing = 0>\n";
$display .= "<tr>\n";
$display .= "<th>Facility</th>\n";
$display .= "<th>State</th>\n";
$display .= "<th>Type</th>\n";
$display .= "</tr>\n\n";

//background color for <tr>
$bgcolor = "#f5fffa";

while ($row = mysql_fetch_array($result)) {
if ($bgcolor == "#f5fffa") {
	$bgcolor = "#ffffff";
} else {
	$bgcolor = "#f5fffa";
} //end if

//display table row data
$display .= "<tr bgcolor = \"".$bgcolor."\">\n";
$display .= "<td><a href = 'view_facility.php?f=$row[iD]'>$row[facility]</a></td>\n";
$display .= "<td>$row[state]</td>\n";
$display .= "<td>$row[type]</td>\n";
$display .= "</tr>\n";
} //end while

$display .= "</table>\n";

// Figure out the total number of results in DB:
$t_query = "SELECT COUNT(*) as Num FROM pub_gardens WHERE facility LIKE '%".$_SESSION['form_key_word']."%'";

$t_query .= " OR type = '".$_SESSION['form_type']."'";

$t_query .= " OR state = '".$_SESSION['form_state']."'";

$t_query .= " OR city LIKE '%".$_SESSION['form_city']."'";

$t_query .= " OR zip LIKE '%".$_SESSION['form_zip']."'";

//result
$t_result = mysql_query($t_query) or die(mysql_error() . " : " .__LINE__);
$total_results = mysql_result($t_result,0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks

// Build Previous Link
if ($page > 1) {
    $prev = ($page - 1);
    $display .= "<a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$prev\"><< PREVIOUS</a>  ";
} //end if

for ($i = 1; $i <= $total_pages; $i++) {
    if (($page) == $i) {
        $display .= "<strong>$i</strong> ";
    } else {
        $display .= "<a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$i\">$i</a> ";
    } //end if
} //end if

// Build Next Link
if ($page < $total_pages) {
    $next = ($page + 1);
    $display .= "  <a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$next\">NEXT >></a>";
} //end if

//type array
$fac_type = array("Type of Facility" => "None", "Arboretum" => "A", "Arboretum/Garden" => "A, G", "Garden" => "G", "Garden/Arboretum" => "G, A", "Park" => "P", "Park/Garden" => "P, G", "Garden/Park" => "G, P", "Conservatory" => "C");
//states
$fac_state = array("State Select" => "State", "Alabama" => "AL", "Alaska" => "AL", "Arizona" => "AZ", "Arkansas" => "AK", "California" => "CA", "Colorado" => "CO", "Connecticut" => "CT", "Delaware" => "DE", "Dist of Columbia" => "DC", "Florida" => "FL", "Georgia" => "GA", "Hawaii" => "HI", "Idaho" => "ID", "Illinois" => "IL", "Indiana" => "IN", "Iowa" => "IA", "Kansas" => "KS", "Kentucky" => "KY", "Louisiana" => "LA", "Maine" => "ME", "Maryland" => "MD", "Massachusetts" => "MA", "Michigan" => "MI", "Minnesota" => "MN", "Mississippi" => "MS", "Missouri" => "MO", "Montana" => "MT", "Nebraska" => "NE", "Nevada" => "NV", "New Hampshire" => "NH", "New Jersey" => "NJ", "New Mexico" => "NM", "New York" => "NY", "North Carolina" => "NC", "North Dakota" => "ND", "Ohio" => "OH", "Oklahoma" => "OK", "Oregon" => "OR", "Pennsylvania" => "PA", "Rhode Island" => "RI", "South Carolina" => "SC", "South Dakota" => "SD", "Tennessee" => "TN", "Texas" => "TX", "Utah" => "UT", "Vermont" => "VT", "Virginia" => "VA", "Washington" => "WA", "West Virginia" => "WV", "Wisconsin" => "WI", "Wyoming" => "WY");

/*
//put "type" into options
foreach ($fac_type as $type) {
$type_option .= "<option value = \"$type\">$type</option>";
} //end foreach
*/

foreach ($fac_type as $type => $letters) {
$type_option .= "<option value = $letters\n";

if ($_SESSION['form_type'] == $letters) {
	$type_option .= "	SELECTED\n";
} //end if

$type_option .= ">$type</option>\n";
} //end foreach

/*
//put "state" into options
foreach ($fac_state as $state => $ac) {
$state_option .= "<option value = \"$ac\">$state</option>";
} //end foreach
*/

foreach ($fac_state as $state => $ac) {
$state_option .= "<option value = $ac\n";

if ($_POST['form_type'] == $ac) {
	$state_option .= "	SELECTED\n";
} //end if

$state_option .= ">$state</option>\n";
} //end if
} //end foreach
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>eCountryLifestyle: Public Garden Directory</title>


<link href="../../_css/style_popup.css" rel="stylesheet" type="text/css" />
</head>



<body>

<div id="layoutContainer">

  <div id="headerHome"><img src="../images/public_garden_directory_banner.jpg" alt="Public Garden Directory" width="784" height="182" /></div>

  <div id="contentLayout">

    <div id="searchLayoutChecklist">
      <div class="topLinkTextNormalHeading" >Browse by Facility Name: <span class="topLinkTextNormal"><?php print "$links_top"; ?></span></div>
    </div>

    <div id="content">

      <div id="contents">

        <div id="sidebarLayout">

          <p align="center"><a href="http://www.cotton.org/index.cfm" target="_blank"><img src="../images/associations/national_cotton_counsel.jpg" alt="National Cotton Counsil" width="180" height="90" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.mulchandsoilcouncil.org/index.html" target="_blank"><img src="../images/associations/mulch_and_soil_counsl.jpg" alt="Mulch and Soil Council" width="180" height="157" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.bluediamond.com/" target="_blank"><img src="../images/associations/blue_diamond_almonds.jpg" alt="Blue Diamonds Almonds" width="180" height="127" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.naturalproductsassoc.org/site/PageServer" target="_blank"><img src="../images/associations/natural_pro_assocn.jpg" alt="National Products Association" width="180" height="82" border="0" /></a></p>
        </div>

        <h1 class="subtitle">Public Garden Directory<br />
          <br />
        </h1>
        <?php print "$display"; ?><br><br>
        <table width="525" border="0" cellpadding="2" cellspacing="0" class="table_box">
          <tr>
            <td class="table_row_header1"><strong>Search</strong></td>
          </tr>
          <tr>
            <td class="table_main_content">
            <form class = "form1" action = "advanced_search.php" method = "post">
		<table border = 0>
		<tr>
		<td colspan = 2><select name = "type"><?php echo "$type_option"; ?></select></td>
		</tr>
		<tr>
		<td height = 30 colspan = 2 valign = "bottom"><select name = "state"><?php echo "$state_option"; ?></select></td>
		</tr>
		<tr>
		<td height = 30 valign = "bottom"><input class = "form1" type = "text" name = "zip" size = 6 maxlength = 12 value = "<?=$_SESSION[form_zip];?>"></td>
		<td height = 30 valign = "bottom"><input class = "form1" type = "text" name = "city" size = 10 maxlength = 30 value = "<?=$_SESSION[form_city];?>"></td>
		</tr>
		<tr>
		<td colspan = 2 valign = "bottom">Key Word(s):</td>
		</tr>
		<tr>
		<td><input class = "form1" type = "text" name = "key_word" size = 20 maxlength = 100 value = "<?=$_SESSION[key_word];?>"></td>
		<td><input type = "submit" name = "submit" value = "Search" width = 120></td>
		</tr>
		</table>
		</form>
		</td>
          </tr>
        </table>
        <p> </p>
      </div>
    </div>
  </div>
</div>
</body>
</html>

 

 

Thanks to anyone who reads and posts! ;D

Link to comment
https://forums.phpfreaks.com/topic/68376-advanced-searchagain/
Share on other sites


Here is an update of the code that I've just tried testing again.

 

 

<?php
//connect
include('../../includes/PHP/globals_db.inc');
$dbconn = mysql_connect($ecl_database_server, $ecl_username, $ecl_password) or die(mysql_error());
$select = mysql_select_db($ecl_database, $dbconn) or die(mysql_error());
//------------

//first lets make an array of letters A-Z
$alpha = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z");

$links_top = "";
foreach ($alpha as $links) {
$links_top .= "<a class = \"checklist\" href = 'facilities.php?fac=$links'>";

if ($links == "Z") {
	$links_top .= "$links</a>";
} else {
	$links_top .= "$links</a> | ";
} //end if
} //end if

if (!isset($_GET

)) {
$page = 1;
} else {
$page = $_GET

;
} //end if

$max_results = 10;

$from = (($page * $max_results) - $max_results);

if (isset($_POST[submit])) {
session_destroy();
//start the query statement for the search fields-starting with key word for default
$query = "SELECT * FROM pub_gardens WHERE facility LIKE '%$_POST[key_word]%'";

//if the user selects a type of facility from drop down menu, add on to the query string
//also make sure the first value of the drop down for the facility selection isnt the first
if ($_POST[type] != "Type of Facility") {
	$query .= " OR type = '$_POST[type]'";
} //end if

//if the user selects a state from drop down menu, add on to the query string
//also make sure the first value of the drop down for the state selection isnt the first
if ($_POST[state] != "State") {
	$query .= " OR state = '$_POST[state]'";	
} //end if

//if the user types in a city in the city field from the form, add on to the query string
if (($_POST[city] != "" && $_POST[city] != "City")) {
	$query .= " OR city LIKE '%$_POST[city]'";	
} //end if

//if the user types in a zip code in the zip field from the form, add on to the query string
if (($_POST[zip] != "" && $_POST[zip] != "Zip Code")) {
	//make sure the default value isnt "Zip" before adding on to the string
		$query .= " OR zip LIKE '%$_POST[zip]'";
} //end if

//add on last addition to the $query string variable
$query .= " ORDER BY facility LIMIT $from, $max_results";

//set session variables
session_start();
$_SESSION['form_type'] 		= $_POST[type];
$_SESSION['form_state']		= $_POST[state];
$_SESSION['form_city']		= $_POST[city];
$_SESSION['form_zip']		= $_POST[zip];
$_SESSION['form_key_word'] 	= $_POST[key_word];
//query session
$_SESSION['query'] = $query;
} else {
$query = $_SESSION['query'];
} //end if

$result = mysql_query($query) or die(mysql_error() . " : " .__LINE__);

//start table
$display = "<font color = \"#ff0000\">*Types (A = Arboretum, G = Garden, C = Conservatory, P = Park)</font>\n";
$display .= "<table border = 0 width = 500 cellspacing = 0>\n";
$display .= "<tr>\n";
$display .= "<th>Facility</th>\n";
$display .= "<th>State</th>\n";
$display .= "<th>Type</th>\n";
$display .= "</tr>\n\n";

//background color for <tr>
$bgcolor = "#f5fffa";

while ($row = mysql_fetch_array($result)) {
if ($bgcolor == "#f5fffa") {
	$bgcolor = "#ffffff";
} else {
	$bgcolor = "#f5fffa";
} //end if

//display table row data
$display .= "<tr bgcolor = \"".$bgcolor."\">\n";
$display .= "<td><a href = 'view_facility.php?f=$row[iD]'>$row[facility]</a></td>\n";
$display .= "<td>$row[state]</td>\n";
$display .= "<td>$row[type]</td>\n";
$display .= "</tr>\n";
} //end while

$display .= "</table>\n";

// Figure out the total number of results in DB:
$t_query = "SELECT COUNT(*) as Num FROM pub_gardens WHERE facility LIKE '%".$_SESSION['form_key_word']."%'";

$t_query .= " OR type = '".$_SESSION['form_type']."'";

$t_query .= " OR state = '".$_SESSION['form_state']."'";

$t_query .= " OR city LIKE '%".$_SESSION['form_city']."'";

$t_query .= " OR zip LIKE '%".$_SESSION['form_zip']."'";

//result
$t_result = mysql_query($t_query) or die(mysql_error() . " : " .__LINE__);
$total_results = mysql_result($t_result,0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks

// Build Previous Link
if ($page > 1) {
    $prev = ($page - 1);
    $display .= "<a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$prev\"><< PREVIOUS</a>  ";
} //end if

for ($i = 1; $i <= $total_pages; $i++) {
    if (($page) == $i) {
        $display .= "<strong>$i</strong> ";
    } else {
        $display .= "<a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$i\">$i</a> ";
    } //end if
} //end if

// Build Next Link
if ($page < $total_pages) {
    $next = ($page + 1);
    $display .= "  <a class = \"checklist\" href=\"".$_SERVER[php_SELF]."?page=$next\">NEXT >></a>";
} //end if

//type array
$fac_type = array("Type of Facility" => "None", "Arboretum" => "A", "Arboretum/Garden" => "A, G", "Garden" => "G", "Garden/Arboretum" => "G, A", "Park" => "P", "Park/Garden" => "P, G", "Garden/Park" => "G, P", "Conservatory" => "C");
//states
$fac_state = array("State Select" => "State", "Alabama" => "AL", "Alaska" => "AL", "Arizona" => "AZ", "Arkansas" => "AK", "California" => "CA", "Colorado" => "CO", "Connecticut" => "CT", "Delaware" => "DE", "Dist of Columbia" => "DC", "Florida" => "FL", "Georgia" => "GA", "Hawaii" => "HI", "Idaho" => "ID", "Illinois" => "IL", "Indiana" => "IN", "Iowa" => "IA", "Kansas" => "KS", "Kentucky" => "KY", "Louisiana" => "LA", "Maine" => "ME", "Maryland" => "MD", "Massachusetts" => "MA", "Michigan" => "MI", "Minnesota" => "MN", "Mississippi" => "MS", "Missouri" => "MO", "Montana" => "MT", "Nebraska" => "NE", "Nevada" => "NV", "New Hampshire" => "NH", "New Jersey" => "NJ", "New Mexico" => "NM", "New York" => "NY", "North Carolina" => "NC", "North Dakota" => "ND", "Ohio" => "OH", "Oklahoma" => "OK", "Oregon" => "OR", "Pennsylvania" => "PA", "Rhode Island" => "RI", "South Carolina" => "SC", "South Dakota" => "SD", "Tennessee" => "TN", "Texas" => "TX", "Utah" => "UT", "Vermont" => "VT", "Virginia" => "VA", "Washington" => "WA", "West Virginia" => "WV", "Wisconsin" => "WI", "Wyoming" => "WY");

/*
//put "type" into options
foreach ($fac_type as $type) {
$type_option .= "<option value = \"$type\">$type</option>";
} //end foreach
*/

foreach ($fac_type as $type => $letters) {
$type_option .= "<option value = $letters\n";

if ($_SESSION['form_type'] == $letters) {
	$type_option .= "	SELECTED\n";
} //end if

$type_option .= ">$type</option>\n";
} //end foreach

/*
//put "state" into options
foreach ($fac_state as $state => $ac) {
$state_option .= "<option value = \"$ac\">$state</option>";
} //end foreach
*/

foreach ($fac_state as $state => $ac) {
$state_option .= "<option value = $ac\n";

if ($_POST['form_type'] == $ac) {
	$state_option .= "	SELECTED\n";
} //end if

$state_option .= ">$state</option>\n";
} //end if
} //end foreach
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>eCountryLifestyle: Public Garden Directory</title>


<link href="../../_css/style_popup.css" rel="stylesheet" type="text/css" />
</head>



<body>

<div id="layoutContainer">

  <div id="headerHome"><img src="../images/public_garden_directory_banner.jpg" alt="Public Garden Directory" width="784" height="182" /></div>

  <div id="contentLayout">

    <div id="searchLayoutChecklist">
      <div class="topLinkTextNormalHeading" >Browse by Facility Name: <span class="topLinkTextNormal"><?php print "$links_top"; ?></span></div>
    </div>

    <div id="content">

      <div id="contents">

        <div id="sidebarLayout">

          <p align="center"><a href="http://www.cotton.org/index.cfm" target="_blank"><img src="../images/associations/national_cotton_counsel.jpg" alt="National Cotton Counsil" width="180" height="90" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.mulchandsoilcouncil.org/index.html" target="_blank"><img src="../images/associations/mulch_and_soil_counsl.jpg" alt="Mulch and Soil Council" width="180" height="157" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.bluediamond.com/" target="_blank"><img src="../images/associations/blue_diamond_almonds.jpg" alt="Blue Diamonds Almonds" width="180" height="127" border="0" /></a></p>

          <p> </p>

          <p align="center"><a href="http://www.naturalproductsassoc.org/site/PageServer" target="_blank"><img src="../images/associations/natural_pro_assocn.jpg" alt="National Products Association" width="180" height="82" border="0" /></a></p>
        </div>

        <h1 class="subtitle">Public Garden Directory<br />
          <br />
        </h1>
        <?php print "$display"; ?><br><br>
        <table width="525" border="0" cellpadding="2" cellspacing="0" class="table_box">
          <tr>
            <td class="table_row_header1"><strong>Search</strong></td>
          </tr>
          <tr>
            <td class="table_main_content">
            <form class = "form1" action = "advanced_search.php" method = "post">
		<table border = 0>
		<tr>
		<td colspan = 2><select name = "type"><?php echo "$type_option"; ?></select></td>
		</tr>
		<tr>
		<td height = 30 colspan = 2 valign = "bottom"><select name = "state"><?php echo "$state_option"; ?></select></td>
		</tr>
		<tr>
		<td height = 30 valign = "bottom"><input class = "form1" type = "text" name = "zip" size = 6 maxlength = 12 value = "<?=$_SESSION[form_zip];?>"></td>
		<td height = 30 valign = "bottom"><input class = "form1" type = "text" name = "city" size = 10 maxlength = 30 value = "<?=$_SESSION[form_city];?>"></td>
		</tr>
		<tr>
		<td colspan = 2 valign = "bottom">Key Word(s):</td>
		</tr>
		<tr>
		<td><input class = "form1" type = "text" name = "key_word" size = 20 maxlength = 100 value = "<?=$_SESSION[key_word];?>"></td>
		<td><input type = "submit" name = "submit" value = "Search" width = 120></td>
		</tr>
		</table>
		</form>
		</td>
          </tr>
        </table>
        <p> </p>
      </div>
    </div>
  </div>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/68376-advanced-searchagain/#findComment-343780
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.