drkidd22 Posted December 6, 2007 Share Posted December 6, 2007 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++; } ?> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 6, 2007 Share Posted December 6, 2007 http://www.designplace.org/scripts.php?page=1&c_id=25 Try the above site. It helped me create my search. The pagination part of the script does not work. But there is a Topic in this forum that talks about it. Quote Link to comment Share on other sites More sharing options...
drkidd22 Posted December 6, 2007 Author Share Posted December 6, 2007 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>  "; } // 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. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 6, 2007 Share Posted December 6, 2007 you forgot a semi colon after this statement: So change this $trimmed = trim($var) To this $trimmed = trim($var); Also change your form method from "get" to "post" and your $var variable should read: $var = @$_POST['q']; Quote Link to comment Share on other sites More sharing options...
drkidd22 Posted December 6, 2007 Author Share Posted December 6, 2007 isn't there a simple change that I can make to my code in $query = "SELECT * FROM ts_tbl WHERE $dropdown='$search'" or die (mysql_error()); so that I can just get a match with a word and they don't to be exactly the same? thanks Quote Link to comment Share on other sites More sharing options...
drkidd22 Posted December 10, 2007 Author Share Posted December 10, 2007 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++; } ?> Quote Link to comment Share on other sites More sharing options...
drkidd22 Posted December 10, 2007 Author Share Posted December 10, 2007 Is this so hard that no one can repply or maybe just no one cares cuz i'm a noob? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.