Jump to content

PHP search


drkidd22

Recommended Posts

Hello,

 

I have a search code which works fine right now. The only problem I'm having is that It will not give me a result if there is not an exact match. I want the code to be able to display a result if it also found the word withing a field or matches any letter in a word, something like that. This is what I have.

 

Form

              <form action = "results.php" method="post">
search <input type="text" name="search"><br>
<select size="1" name="dropdown"> 
<option value="" selected>search By...</option> 
<option value="ID">ID</option> 
<option value="lasersystem">Laser System</option> 
<option value="f_ID">Fault Number</option> 
<option value="p_solution">Description</option> 
</selec>
<input type="Submit" value="Submit" name="Submit">
</form>

 

PHP Code

<?php  
$host = "xxx"; 
$user = "xx"; 
$pass = "xxx"; 
$db = "xxxx"; 
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']); 
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); 
// Open Connection 
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host"); 
//Select Database 
mysql_select_db($db) or die ("Unable to connect to database"); 
//Create Query 
$query = "SELECT * FROM ts_tbl WHERE $dropdown='$search'" or die (mysql_error()); 
$result = mysql_query($query) or die (mysql_error()); 
$num=mysql_numrows($result); 
mysql_close($connect); 
echo "<b><center>Database Output</center></b>"; 
$i=0; 
while ($i < $num) { 
$ID=mysql_result($result,$i,"ID"); 
$lasersystem=mysql_result($result,$i,"lasersystem"); 
$f_ID=mysql_result($result,$i,"f_ID"); 
$thirdlevel=mysql_result($result,$i,"thirdlevel"); 
echo "<br><b>ID: $ID</b><br>Laser System: $lasersystem<br>Fault ID: $f_ID<br>Description: $thirdlevel<br>"; 
echo "<a href=detailsf.php?tag=$ID>Details</td></a>";
$i++; 
} 
?> 

Link to comment
Share on other sites

Thanks for your reply, I tried that code and it gives me an error:

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\searchscript.php on line 22

 

Here is the code from : http://www.designplace.org

<html>
<head>
<title>designplace.org search script</title>
<meta name="author" content="Steve R, http://www.designplace.org/">
</head>
<!-- © http://www.designplace.org/ -->
<body>

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

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var) //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from the_table where 1st_field like \"%$trimmed%\"  
  order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }

// 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");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["1st_field"];

  echo "$count.) $title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// 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++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>

<!-- © http://www.designplace.org/ -->

</body>
</html>

 

Also my form has a selection from a drop down menu.

 

Link to comment
Share on other sites

Ok, So my Form searches in four colums, one dropdown that has 4 selections, one for each comlum. I need the search code to print the matches with what the user types in and what it's found in the colum that was selected in the dropdown. Right now it will only print if the user types in the exact word. It will also only give results with one word typed in not partial or a string or sentence.

 

Here is my code:

 

<?php  
$host = "localhost"; 
$user = "xxx"; 
$pass = "xxx"; 
$db = "xxx"; 
$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']); 
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']); 
// Open Connection 
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host"); 
//Select Database 
mysql_select_db($db) or die ("Unable to connect to database"); 
//Create Query 
$query = "SELECT * FROM ts_tbl WHERE $dropdown='$search'" or die (mysql_error());  
$result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result); 
mysql_close($connect); 
echo "<b><center>Database Output</center></b>"; 
$i=0; 
while ($i < $num) { 
$ID=mysql_result($result,$i,"ID"); 
$lasersystem=mysql_result($result,$i,"lasersystem"); 
$f_ID=mysql_result($result,$i,"f_ID"); 
$p_solution=mysql_result($result,$i,"p_solution"); 
echo "<br><b>ID: $ID</b><br>Laser System: $lasersystem<br>Fault ID: $f_ID<br>Description: $p_solution<br>"; 
echo "<a href=detailsf.php?tag=$ID>Details</td></a>";
$i++; 
} 
?> 

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.