Jump to content

Search Database


j05hr

Recommended Posts

I have this code

 

<?php require_once("includes/connection.php"); ?>
<?php
// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
   
   if (strlen($searchTerms) < 3) {
      $error[] = "Search terms must be longer than 3 characters.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }
   
   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT id, house_price, bedrooms, propType FROM subjects WHERE ";
      
      // grab the search types.
      $types = array();
      $types[] = isset($_GET['house_price'])?"`house_price` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['bedrooms'])?"`bedrooms` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['propType'])?"`propType` LIKE '%{$searchTermDB}%'":'';
      
      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
      
      if (count($types) < 1)
         $types[] = "`bedrooms` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
      
          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `house_price` ASC "; // order by title.

      $searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: {$row['house_price']}<br />{$row['bedrooms']}<br />{$row['propType']}<br /><br />";
            $i++;
         }
      }
   }
}

function removeEmpty($var) {
   return (!empty($var)); 
}
?>
<?php include("includes/header.php"); ?>
   <div id="content">
      <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
      <form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>" />
	Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" /><br />
         Search In:<br />
         Price: <input type="checkbox" name="house_price" value="on" <?php echo isset($_GET['house_price'])?"checked":''; ?> /> | 
	Bedrooms: <input type="checkbox" name="bedrooms" value="on" <?php echo isset($_GET['bedrooms'])?"checked":''; ?> /> | 
         Type of Property: <input type="checkbox" name="propType" value="on" <?php echo isset($_GET['propType'])?"checked":''; ?> /><br />
                 Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?> /> <br /><br />
         <input type="submit" name="submit" value="Search" />
      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>
  </div>
<?php require("includes/footer.php"); ?>

 

And I want it to be so when I return the search, it will come up with the results looking like this www.sampleestateagent.com/buying.php

 

Instead of returning it like this http://sampleestateagent.com/search.php?search=house&propType=on&submit=Search

 

Which part of my code do I need to change so I can put the divs in there of what it will return?

Link to comment
Share on other sites

while ($row = mysql_fetch_assoc($searchResult)) {
   $results[] = "{$i}: {$row['house_price']}<br />{$row['bedrooms']}<br />{$row['propType']}<br /><br />";
   $i++;
}

Simply change the contents of this loop. Example...

 

$div = '<div class="menu-name">Home ' . $i .'</div><div class="buying-text">';
$div .= $row['house_price'] . '<br />' .  $row['bedrooms'] . '<br />' . $row['propType'] . '<br />';
$div .= '</div><div class="image"></div>';

$results[] = $div;
$i++

Link to comment
Share on other sites

I did that but got an error (not sure if I did it right)

 

The error is Parse error: parse error in C:\wamp\www\estateagent\search.php on line 51

 

         while ($row = mysql_fetch_assoc($searchResult)) {
           
	   $div = '<div class="menu-name">Home ' . $i .'</div><div class="buying-text">';
		$div .= $row['house_price'] . '<br />' .  $row['bedrooms'] . '<br />' . $row['propType'] . '<br />';
		$div .= '</div><div class="image"></div>';
		$results[] = $div;
		$i++
         }
      }
   }
}

 

Also, I don't actually want to display bedrooms price or property type as I only made this to make the search function easier,  all of them will be included in the content so i only want it to be for the use of the database to find them.  Is that ok to do?

Link to comment
Share on other sites

I now have this

 

while ($row = mysql_fetch_assoc($searchResult)) {
$div = '<div class="menu-name"> ' . $i .'</div><div class="buying-text">';
$div .= $row['content'];
$div .= '</div><div class="image"></div>'

 

How can I make it so in the menu name I can put ['menu_name'] (like what's happening with buying-text for ['content'])

 

And also on the menu-name it needs to link to the page with this code

 

 echo "<a href=\"buying.php?page=" . urlencode($subject["id"]) . 

 

But that's from this function

 


function public_navigation($sel_subject = null, $sel_page = null) {  
        $subject_set = get_all_subjects();
        // 5. Use returned data
        while ($subject = mysql_fetch_array($subject_set)) { 

/*----*/   
	        
	if(!isset($_GET['page'])) {
        echo "<div class=\"menu-name\">"; echo "<a href=\"buying.php?page=" . urlencode($subject["id"]) .
            "\">{$subject["menu_name"]}</a>";  echo"</div>";
	echo "<div class=\"buying-text\">";  echo "{$subject["content"]}";  echo"</div>"; echo "<div class=\"image\">";     echo"</div>";

	}//

        
        
        }
    } 

Link to comment
Share on other sites

Yeah that's worked thanks.  One last thing, how would I put this link there

 

echo "<div class=\"menu-name\">"; echo "<a href=\"buying.php?page=" . urlencode($subject["id"]) .

 

It's from an include file

function public_navigation($sel_subject = null, $sel_page = null) {  
        $subject_set = get_all_subjects();
        // 5. Use returned data
        while ($subject = mysql_fetch_array($subject_set)) { 

/*----*/   
	        
	if(!isset($_GET['page'])) {
        echo "<div class=\"menu-name\">"; echo "<a href=\"buying.php?page=" . urlencode($subject["id"]) .
            "\">{$subject["menu_name"]}</a>";  echo"</div>";
	echo "<div class=\"buying-text\">";  echo "{$subject["content"]}";  echo"</div>"; echo "<div class=\"image\">";     echo"</div>";

	}//

        
        
        }
    }

Link to comment
Share on other sites

If you can't work that out for yourself I have to say, you are way out of your depth. You are going to do nothing but run into problems and come asking questions here if you attempt to dive right in and do something of the complexity your trying. You need to learn the basics first.

Link to comment
Share on other sites

I am out of my depth and probably from the site you can see I'm more of a designer then a coder, and I've built pretty much the whole CMS following a tutorial, but as a designer you can't really get work unless you show experience of some coding experience which is what I'm trying to do with this project, just to get my foot in the door.

Link to comment
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.