-
1. Re: Search results will only work if one word searched for?
Nancy O. Dec 9, 2014 11:26 AM (in response to Sara.Reese)I don't know how your database table is structured, but I approach this a little differently since I figure people are going to query for all sorts of things.
<?php
$query = $_GET['query'];
// gets value sent over search form
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$query = strip_tags($query);
// strip code injection
$query = trim ($query);
// trim white spaces
$raw_results = mysql_query("SELECT * FROM products_table WHERE (`longdesc` LIKE '%".$query."%') OR (`prodcode` LIKE '%".$query."%') OR (`prodname` LIKE '%".$query."%') OR (`category` LIKE '%".$query."%') OR (`price` LIKE '%".$query."%') ORDER BY `prodcode` ASC") or die(mysql_error());
I won't tell you it's perfect, but it picks up more relevant results.
Nancy O.
-
2. Re: Search results will only work if one word searched for?
bregent Dec 9, 2014 11:37 AM (in response to Sara.Reese)As you've discovered, DW server behavior searching is not very robust. In most real world cases, it is practically useless. To achieve what you want, you will need to parse the separate words of the input string into an array and then expand that into the WHERE clause dynamically so that each individual word is tested. You need to determine whether to combine terms with AND or with OR depending on the type of search you want.
Another idea is to investigate MySQL FULLTEXT searches. This will probably makes things easier and provide better results. But again, you will need to write the PHP and SQL yourself.
-
3. Re: Search results will only work if one word searched for?
osgood_ Dec 9, 2014 1:30 PM (in response to Sara.Reese)Sara.Reese wrote:
Hello, I have almost finished my works new website http://www.reese-test2.co.uk, but while testing it, I have noticed that the search isn't working as great as I would like.
For example if you search for "floodlight" then it produces results, but if you search for "black floodlight" then no results come back, which is strange as both words are in in the same "description" sentence in the mySQL table.
Is there something in Dreamweaver that I have missed so it can search if more than one word is added to the search box?
The recordset is currently set up to see info from the "code" and "description" columns.
Recordset screenshot as follows:
SELECT *
FROM productsearch
WHERE `description` LIKE %colname% OR `code` LIKE %colname%
ORDER BY code ASC
thanks for your help
Sara (I'm using DW CS6)
Use php 'explode'
First get the search terms from your form field - 'search_box':
$searchTerms = $_POST['search_box'];
Then split the terms using explode:
$searchTerms = explode(" ", $searchTerms);
Asign part of the sql query to a variable:
$searchCondition = "description LIKE '%" . implode("%' OR description LIKE '%", $searchTerms) . "%'";
Then make sure the variable is inserted into the sql query string:
"SELECT * FROM productsearch WHERE $searchCondition ORDER BY code ASC ";
-
4. Re: Search results will only work if one word searched for?
bregent Dec 9, 2014 2:01 PM (in response to osgood_)Nice osgood. A very concise, easy to understand way to do what I suggested.
"parse the separate words of the input string into an array and then expand that into the WHERE clause dynamically so that each individual word is tested"
And again, the OP will need to decide whether to combine terms with AND or OR. A radio button on the search form can be added to allow users to "Search for All Terms" or "Search for Any Term" or "Search Phrase as Entered"
-
5. Re: Search results will only work if one word searched for?
osgood_ Dec 9, 2014 2:14 PM (in response to bregent)bregent wrote:
Nice osgood. A very concise, easy to understand way to do what I suggested.
Well must admit it's not all my own - I found a few resources on Google and kind of pieced it together as none worked 'out of the box' so to speak, or looked a bit complex.
It's a simple solution, which most should be able to understand, and a step up from the 'single word' option that the limited DW server behaviours offer BUT as you say there are plenty of other criteria that could be introduced depending on how precise the search needs to be.
-
6. Re: Search results will only work if one word searched for?
Nancy O. Dec 9, 2014 2:44 PM (in response to osgood_)I'm concerned that exploding $search_query_terms into an array and cross checking each term against a large db might be very slow. Have you encountered problems with this?
Nancy O.
-
7. Re: Search results will only work if one word searched for?
osgood_ Dec 9, 2014 3:26 PM (in response to Nancy O.)Nancy O. wrote:
I'm concerned that exploding $search_query_terms into an array and cross checking each term against a large db might be very slow. Have you encountered problems with this?
Nancy O.
Hi Nancy,
I've used explode a couple of times but only on fairly small quanties of data to be searched. It seems to perform smoothly enough for what I need it to do - I don't have any experience of working with large volumes of data to check through unfortuantely.
-
8. Re: Search results will only work if one word searched for?
bregent Dec 9, 2014 4:37 PM (in response to Nancy O.)It could slow things down - how many rows do you consider to be a large table
If you are searching for any term (OR) then it probably wouldn't hurt as much as if you are searching all terms (AND). If you are testing a large number of terms against multiple columns that could also cause performance issues. You'd need to test and add performance indices as needed and possibly optimize the query in other ways.
You also might want to limit the number of search terms in the query; test the number of exploded items and give the user an error if it exceeds a set amount.
At some point, a FULLTEXT search might be more efficient, I don't know.
-
9. Re: Search results will only work if one word searched for?
Sara.Reese Dec 10, 2014 1:41 AM (in response to Sara.Reese)Hi thanks all for your input. That was interesting reading. I have tried to use your suggestions, but my results.php code doesnt look at all like your code (dreamweaver produced my code, whereas you guys probably all have hand written code, so struggling a little bit with what code needs replacing as it seems quite different.
I do understand what you are saying though. Just need a little help with implementing it.
Nancy my database is really basic. There are 500 products and each one has a row consisting of:
1) code (the products code - searchable)2) picture (img_src location code for the pic)
3) description (this just has one sentence describing the product, and what needs to be searchable)
4) go (the rollover button to take you to that products page)
osgood:
"First get the search terms from your form field - 'search_box':"
no where on my results.php page is "$searchTerms" but I do have:
$colname_productsearch = "-1";
if (isset($_POST['search_box'])) {
$colname_productsearch = $_POST['search_box'];
changing it to the below creates a syntax error: i'm assuming i'm doing it wrong!
$colname_productsearch = "-1";
$searchTerms = explode(" ", $searchTerms);
$colname_productsearch = $_POST['search_box'];
I will post the code that dreamweaver has produced below, so you can see what I am dealing with, thanks again for your help
<?php require_once('Connections/searchDB.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_productsearch = "-1"; if (isset($_POST['search_box'])) { $colname_productsearch = $_POST['search_box']; } mysql_select_db($database_searchDB, $searchDB); $query_productsearch = sprintf("SELECT * FROM productsearch WHERE `description` LIKE %s OR `code` LIKE %s ORDER BY code ASC", GetSQLValueString("%" . $colname_productsearch . "%", "text"),GetSQLValueString("%" . $colname_productsearch . "%", "text")); $productsearch = mysql_query($query_productsearch, $searchDB) or die(mysql_error()); $row_productsearch = mysql_fetch_assoc($productsearch); $totalRows_productsearch = mysql_num_rows($productsearch); ?> -
10. Re: Search results will only work if one word searched for?
osgood_ Dec 10, 2014 4:00 AM (in response to Sara.Reese)Can you do a test? Copy the below code and paste into a new Dreamweaver document and save as - search_test.php
Change the text in red below to that of your database connection details.
Then temporarily point the search form action field on one of your pages at the search_test.php page - see if you get any results.
COPY FROM HERE ----->
<?php $conn = new mysqli('server' , 'username' , 'password' , 'database_name'); ?>
<?php
$searchTerms = $_POST['search_box'];
$searchTerms = explode(" ", $searchTerms);
$searchCondition = "description LIKE '%" . implode("%' OR description LIKE '%", $searchTerms) . "%'";
?>
<?php
// get list of products that contain search terms
$sql = "SELECT * FROM description WHERE $searchCondition";
$listProducts = $conn->query($sql) or die($conn->error);
// count no of rows
$numRows = $listProducts->num_rows;
?>
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<meta http-equiv="charset=UTF-8">
<title>Search Test</title>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="4">
<tr align="center" bgcolor="#FF0099" class="mainareaH">
<td width="150" bgcolor="#0099FF">PRODUCT CODE</td>
<td width="90" bgcolor="#0099FF">IMAGE</td>
<td bgcolor="#0099FF">DESCRIPTION</td>
<td width="90" bgcolor="#0099FF">GO</td>
</tr>
<?php while($row = $listProducts->fetch_assoc()) { ?>
<tr align="center">
<td width="150" bgcolor="#333333" class="mainareaH"><?php echo $row['code']; ?></td>
<td width="90" bgcolor="#333333"><?php echo $row['picture']; ?></td>
<td bgcolor="#333333" class="mainareaP"><?php echo $row['description']; ?></td>
<td width="90" bgcolor="#333333"><?php echo $row['go']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
-
11. Re: Search results will only work if one word searched for?
Sara.Reese Dec 10, 2014 5:05 AM (in response to osgood_)Hi Osgood, I did as above, I copy & pasted the above into a new doc called search_test.php
and saved another page with the search_box on and called it test.php - pointing the search_box form to search_test.php
I'm however getting a blank page saying:
Table 'cl44-localhost.description' doesn't existmy details
server: localhost
username: cl44-localhost
password: passwordgoeshere (not my real password)
database name: cl44-localhost
below is a screenshot of my info (password is changed), shouldn't searchDB go somewhere?
also the table on my database is called productsearch so doesnt this need to also go somewhere?
I.E in the SELECT * FROM bit somehow?
thanks
Sara
-
12. Re: Search results will only work if one word searched for?
osgood_ Dec 10, 2014 6:06 AM (in response to Sara.Reese)Sara.Reese wrote:
Hi Osgood, I did as above, I copy & pasted the above into a new doc called search_test.php
and saved another page with the search_box on and called it test.php - pointing the search_box form to search_test.php
I'm however getting a blank page saying:
Table 'cl44-localhost.description' doesn't existmy details
server: localhost
username: cl44-localhost
password: passwordgoeshere (not my real password)
database name: cl44-localhost
below is a screenshot of my info (password is changed), shouldn't searchDB go somewhere?
also the table on my database is called productsearch so doesnt this need to also go somewhere?
I.E in the SELECT * FROM bit somehow?
thanks
Sara
Change this line:
$sql = "SELECT * FROM description WHERE $searchCondition";
To:
$sql = "SELECT * FROM productsearch WHERE $searchCondition";
-
13. Re: Search results will only work if one word searched for?
Sara.Reese Dec 10, 2014 6:57 AM (in response to osgood_)Hi I changed it and that now sort-of worked thank you. (although we've lost the option to also search from the code column)
Also now when you search for "black floodlight" it is bringing up hundreds of results now of products that have the word "black" in them, even though there is no word "floodlight" in those products specific descriptions.
is there something I can change so if say those 2 words are searched together then it only brings back the results where black and floodlight are both in the description?
I may be asking for something super complicated and not understanding how searches work, but is that possible?
Thanks for your help
-
14. Re: Search results will only work if one word searched for?
osgood_ Dec 10, 2014 7:29 AM (in response to Sara.Reese)Try changing OR in the line below to AND - see if that narrows down the results.
$searchCondition = "description LIKE '%" . implode("%' OR description LIKE '%", $searchTerms) . "%'";
-
15. Re: Search results will only work if one word searched for?
Sara.Reese Dec 10, 2014 7:51 AM (in response to osgood_)oooooh yeah thats good! almost perfect now...
just need to also be able to search from the code column AS WELL as the description column as above.
I tried adding LIKE %s OR `code` in various places to the below line, but am getting syntax errors, i'm assuming that is wrong with this new way of writing code?
$searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'";
thanks
Sara
-
16. Re: Search results will only work if one word searched for?
bregent Dec 10, 2014 7:53 AM (in response to Sara.Reese)Sara, that's what I was saying about adding a radio button on the search form to allow the user to choose between searching for any words in the search term or all words. Depending on which they selected you would combine the terms with AND or OR
Return results that contain any term
$searchCondition = "description LIKE '%" . implode("%' OR description LIKE '%", $searchTerms) . "%'";
Return results that contain all terms
$searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'";
-
17. Re: Search results will only work if one word searched for?
bregent Dec 10, 2014 8:05 AM (in response to Sara.Reese)Try this:
$searchCondition = "(description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')";
-
18. Re: Search results will only work if one word searched for?
Sara.Reese Dec 10, 2014 8:53 AM (in response to bregent)Hi Bregent, yeah I do get that and will look at something more complicated later, one learning step at a time.... but for now the above is ideal for the minimal amount it actually has to search for.
I tried your above options so the code column is also included in the results, but I am getting a syntax error.
originally my results would get info from the code and description columns and then display the results in code order (alphabetical), which is what i'm after, but using the above fancy new code from osgood
BEFORE (my old DW code):
("SELECT * FROM productsearch WHERE `description` LIKE %s OR `code` LIKE %s ORDER BY code ASC",
AFTER (osgoods code):
$searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'";
the code from osgood needs the "code column" and "order by code" adding, but I keep getting syntax errors on any way I try to add it in. so confused. thanks for the sugestions though, they are still very helpful in trying to understand the logic behind it.
-
19. Re: Search results will only work if one word searched for?
bregent Dec 10, 2014 9:15 AM (in response to Sara.Reese)Probably a slight error in the code I provided. Can you add an echo statement after the code I provided so we can see how it is being concatenated:
$searchCondition = "(description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')";
echo $searchCondition
also give us details about the syntax error.
-
20. Re: Search results will only work if one word searched for?
osgood_ Dec 10, 2014 10:21 AM (in response to bregent)bregent wrote:
Probably a slight error in the code I provided. Can you add an echo statement after the code I provided so we can see how it is being concatenated:
$searchCondition = "(description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%')";
$searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')";
echo $searchCondition
also give us details about the syntax error.
The above works fine for me Bregent.
description LIKE '%FL10SMD%' AND description LIKE '%%' OR (code LIKE '%FL10SMD%' AND code LIKE '%%')
description LIKE '%Green%' AND description LIKE '%Frog%' AND description LIKE '%%' OR (code LIKE '%Green%' AND code LIKE '%Frog%' AND code LIKE '%%')
EDITED:
Apart from only the exact code number is returned when I assume what is needed is anything like the number hence ORDER BY code
It seems it returns an exact match once it finds a correct number - like searching for FL10 will only return the product assigned the code number FL10SMD NOT FL20SMD, FL30SMD etc BUT if you just type in FL the results will widen by returning more options.
Seems to work pretty good to me though.
-
21. Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 1:07 AM (in response to bregent)Hi Bregent, that is now working now, I can now search a code name and get results. I replaced
$searchTerms) . "%'";
with
echo $searchCondition
for it to work.
Another step forward, thank you!
Now is there a way for it to list by code order? "ORDER BY code ASC" when you search for "black floodlight" it still lists the results in a random order?
thank you
-
22. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 2:06 AM (in response to Sara.Reese)Sara.Reese wrote:
Hi Bregent, that is now working now, I can now search a code name and get results. I replaced
$searchTerms) . "%'";
with
echo $searchCondition
for it to work.
I dont understand what you have done here. Replacing $searchTerms) . "%'"; with $searchConditions isn't correct.
Your code should look like below:
$searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'";
$searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')";
$searchCondition .= "ORDER BY code ASC";
-
23. Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 2:38 AM (in response to osgood_)thanks osgood. ah thats weird! but ok I have replaced my code with the above and perfect!
I now have a search that returns results from code and description columns and now in alphabetical order!
thanks for your help!
I have copied the new code now into my results.php so it is in my website template, and all is good apart from a couple of issues, i'm sure they are minor issues, but while you guys are on a roll, if you could help with these last 2 issues that would be awesome!:
1) I am getting a warning when I preview in browser with this line of code:
$searchTerms = $_POST['search_box'];See warning error box as follows:
2) on my original results.php (still on my website until I get the new fixed one uploaded), I had it set up to hide the table and say "Sorry no items matched your search criteria" when no results could be found. (set up using DW server behaviours) but this no longer works with the fancy new code. is it still possible to do this with the new way? its not a major thing, but was a nice message so people can see that they should try and search for something else.
thanks
Sara
-
24. Re: Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 2:48 AM (in response to Sara.Reese)Also just to be clear what i've pasted in from you guys so you can see all the elements all together - my code is as follows:
at the top of the page:
<?php $conn = new mysqli('localhost' , 'cl44-localhost' , 'et3na' , 'cl44-localhost'); ?> <?php $searchTerms = $_POST['search_box']; $searchTerms = explode(" ", $searchTerms); $searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'"; $searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')"; $searchCondition .= "ORDER BY code ASC"; ?> <?php // get list of products that contain search terms $sql = "SELECT * FROM productsearch WHERE $searchCondition"; $listProducts = $conn->query($sql) or die($conn->error); // count no of rows $numRows = $listProducts->num_rows; ?>and then around line 175-192 my code is as follows:
<p class="mainareaP">If your search results are not what you were looking for <a href="contact.php" target="_self" class="mainareaP">CONTACT US</a> for help</p> <body> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="4"> <tr align="center" bgcolor="#FF0099" class="mainareaH"> <td width="150" bgcolor="#0099FF">PRODUCT CODE</td> <td width="90" bgcolor="#0099FF">IMAGE</td> <td bgcolor="#0099FF">DESCRIPTION</td> <td width="90" bgcolor="#0099FF">GO</td> </tr> <?php while($row = $listProducts->fetch_assoc()) { ?> <tr align="center"> <td width="150" bgcolor="#333333" class="mainareaH"><?php echo $row['code']; ?></td> <td width="90" bgcolor="#333333"><?php echo $row['picture']; ?></td> <td bgcolor="#333333" class="mainareaP"><?php echo $row['description']; ?></td> <td width="90" bgcolor="#333333"><?php echo $row['go']; ?></td> </tr> <?php } ?> </table> -
25. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 2:48 AM (in response to Sara.Reese)No 2. Surround the table with a show/hide php region (see in red below) and check if any rows were returned from the mysqli query string.
This is what this piece of code does:
// count no of rows
$numRows = $listProducts->num_rows;
This displays the table IF a record/s is returned.
<?php if($numRows > 0) { ?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="4">
<tr align="center" bgcolor="#FF0099" class="mainareaH">
<td width="150" bgcolor="#0099FF">PRODUCT CODE</td>
<td width="90" bgcolor="#0099FF">IMAGE</td>
<td bgcolor="#0099FF">DESCRIPTION</td>
<td width="90" bgcolor="#0099FF">GO</td>
</tr>
<?php while($row = $listProducts->fetch_assoc()) { ?>
<tr align="center">
<td width="150" bgcolor="#333333" class="mainareaH"><?php echo $row['code']; ?></td>
<td width="90" bgcolor="#333333"><?php echo $row['picture']; ?></td>
<td bgcolor="#333333" class="mainareaP"><?php echo $row['description']; ?></td>
<td width="90" bgcolor="#333333"><?php echo $row['go']; ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
Then add the below after the table code. If no records are found the message 'No records found' will be viewable
<?php if($numRows <= 0) { ?>
<h1>No records found</h1>
<?php } ?>
-
26. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 3:01 AM (in response to Sara.Reese)1. Shouldn't be happening if you are inserting something into the search box on another page which points to the results page.
Does this happen if you open the results.php page directly in a browser or is this happening coming from another page where something has been inserted the search box?
-
27. Re: Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 3:07 AM (in response to osgood_)ah yeah the error only appears when i preview in browser from the results.php page, it goes away as soon as you search for something or go to another page. please ignore that warning message.
that other code for the "no records shown" also worked perfectly, thank you.
I have now uploaded the new results.php page to my website, but I get a blank white page when I use the search now, whereas it worked perfectly on my local browser, any clues?
thanks
Sara
-
28. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 3:32 AM (in response to Sara.Reese)This could be one of 2 things I can think of:
1. Have you got the right REMOTE server details in the connections string:
<?php $conn = new mysqli('server' , 'username' , 'password' , 'database_name'); ?>
2. Is the remote server set up for mysqli? mysqli is the (improved) version of mysql - most servers are compatible with both mysql and mysqli
To test this insert the below into a blank DW document, upload to server and browse to the file. It should return all of the php details. Somewhere in the list it should mention 'mysqli'
<?php
phpinfo();
?>
-
29. Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 3:36 AM (in response to osgood_)Hi Osgood, yeah i'm pretty sure they are correct, but for just in cases I have messaged the hosting company to see if there are any issues, seeing as it works perfectly until I upload to their server!
I copied the above text and saved it - you can see the page here:
-
30. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 3:45 AM (in response to Sara.Reese)Sara.Reese wrote:
Hi Osgood, yeah i'm pretty sure they are correct, but for just in cases I have messaged the hosting company to see if there are any issues, seeing as it works perfectly until I upload to their server!
I copied the above text and saved it - you can see the page here:
mysqli is enabled so it's not that.
Is there anything else on that page at all that uses any DW server behaviours - mysql and mysqli dont mix?
Also can you post the COMPLETE page code here so I can test it? MINUS the connection details of course.
-
31. Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 3:57 AM (in response to osgood_)Hi Osgood, I just redid the results.php from scratch, so there is defiantly no old DW code in there. Its probably something to do with the hosting company (i've trouble with them before). As soon as I hear back from them I will let you know.
thanks for all your help
Sara
-
32. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 4:02 AM (in response to Sara.Reese)Sara.Reese wrote:
Hi Osgood, I just redid the results.php from scratch, so there is defiantly no old DW code in there. Its probably something to do with the hosting company (i've trouble with them before). As soon as I hear back from them I will let you know.
thanks for all your help
Sara
The best way to tell for sure is if you have access to other remote hosting where you can test it.
-
33. Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 4:21 AM (in response to osgood_)its working!!!! damn hosting company problems! They've fixed a problem on their server.
Thank you everyone esp Osgood and Bregent, really really helpful, i've learnt a lot. I shall be learning up on mysqli now as its clear the old DW server behaviours way was not ideal.
thanks guys awesome support!
Sara
-
34. Re: Re: Search results will only work if one word searched for?
Sara.Reese Dec 11, 2014 5:03 AM (in response to osgood_)Sorry osgood, i just noticed you asked for the complete final code for testing. Please find this below:
very top of the page:
<?php $conn = new mysqli('localhost' , 'cl44-localhost' , 'passwordhere' , 'cl44-localhost'); ?> <?php $searchTerms = $_POST['search_box']; $searchTerms = explode(" ", $searchTerms); $searchCondition = "description LIKE '%" . implode("%' AND description LIKE '%", $searchTerms) . "%'"; $searchCondition .= " OR (code LIKE '%" . implode("%' AND code LIKE '%", $searchTerms) . "%')"; $searchCondition .= "ORDER BY code ASC"; ?> <?php // get list of products that contain search terms $sql = "SELECT * FROM productsearch WHERE $searchCondition"; $listProducts = $conn->query($sql) or die($conn->error); // count no of rows $numRows = $listProducts->num_rows; ?>then the rest of it lower down the page:
<p class="mainareaP">If your search results are not what you were looking for <a href="contact.php" target="_self" class="mainareaP">CONTACT US</a> for help</p> <body> <?php if($numRows > 0) { ?> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="4"> <tr align="center" bgcolor="#FF0099" class="mainareaH"> <td width="150" bgcolor="#0099FF">PRODUCT CODE</td> <td width="90" bgcolor="#0099FF">IMAGE</td> <td bgcolor="#0099FF">DESCRIPTION</td> <td width="90" bgcolor="#0099FF">GO</td> </tr> <?php while($row = $listProducts->fetch_assoc()) { ?> <tr align="center"> <td width="150" bgcolor="#333333" class="mainareaH"><?php echo $row['code']; ?></td> <td width="90" bgcolor="#333333"><?php echo $row['picture']; ?></td> <td bgcolor="#333333" class="mainareaP"><?php echo $row['description']; ?></td> <td width="90" bgcolor="#333333"><?php echo $row['go']; ?></td> </tr> <?php } ?> </table> <?php } ?> <?php if($numRows <= 0) { ?> <h1 class="mainareaH">No records found</h1> <?php } ?>thanks again for your help. Very useful in understanding this new coding.
Sara
-
35. Re: Search results will only work if one word searched for?
osgood_ Dec 11, 2014 5:33 AM (in response to Sara.Reese)As it's working now....no need to test. Seems it was a hosting issue, which happens.










