We help IT Professionals succeed at work.

How do I add a search function to my website?

Wanda Marston
on
How do I add a search function to my website?
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
There are a number of ways.  But you need to specify what you want the viewers to search for and what you want them to find.
leakim971Multitechnician
Distinguished Expert 2019

Commented:
Wanda MarstonCreative Director

Author

Commented:
My viewers are all members of a website who can post messages for all other members to view. SO I would like them to be able to search through ALL of the messages in the database to find anything that might match up with what they might be looking for such as ALL message from a certain City or a company date or maybe a date range.
Wanda MarstonCreative Director

Author

Commented:
leakim971 - I will check out your suggesion. Thanks.
David FavorFractional CTO
Distinguished Expert 2019

Commented:
Of if you're running a CMS, likely there are many search plugins to choose from.

Suggestion: Post your URL, for additional/specific comments for your site type.
Wanda MarstonCreative Director

Author

Commented:
Thanks for your reply.

URL - www.tiredofbeingrippedoff.com
Hi,

You will need to create a form and get the data from DB based on the form value.
I would use AJAX to post to PHP then create a MySQL query that reflect the posted value.

here some tutorials to help you to start

https://www.webslesson.info/2019/03/php-ajax-live-search-with-multiple-value.html

https://www.qandeelacademy.com/lesson/jquery-advance-tutorial/wCsPAquMNVw

https://learncodeweb.com/php/php-crud-in-bootstrap-4-with-search-functionality/


You can use Datatables with  Yadcf filters (you load all data base on a request, the user can filter the result)

I'm using Datatables for all my project this have everything ready to use for your tables

https://datatables.net/
https://yadcf-showcase.appspot.com/
Wanda MarstonCreative Director

Author

Commented:
The information that my viewers would be searching for at this point is just in ONE database which contains the notices that all the viewers would be posting.

SO they can look for - company name or organization - monetary amount (words or numbers) - currency - timeframe - geographical area - description of the problem.

How hard would it be to switch from one type of search engine to another if the search engine was not performing as expected?

I am using PHP and MSQL.

Thanks
Fixer of Problems
Most Valuable Expert 2014
Commented:
Since it is in your database, outside search engines will not be able to search it.  You will have to either write your own (which isn't that hard) or find a 'plugin' that you can add to your code.
Wanda MarstonCreative Director

Author

Commented:
Okay I would like to try to write my own. Can I do this with PHP?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Yes, it's not that hard.  Basically you write an SQL statement with LIKE like this:
"SELECT ``item1`, `item12, item3`, item4 FROM mytable WHERE `item1` LIKE '%$searchkey%'"

Open in new window

You can expand that out to search the different fields.  The first part is the data that is returned for display.
Wanda MarstonCreative Director

Author

Commented:
Okay Thanks. I have been attempting to do my own code from scratch and as you say it should be simple. This kind of works but I am not getting any kind of return.

    <?php 
    
    $output =  ' ';
   
    if (isset($_POST['search']))   {
        $searchkey = $_POST['search'];
        $searchkey =  preg_replace ("#[^0-9a-z]#i","" , $searchkey);
        $q = mysql_query("SELECT  *  FROM notices WHERE `organization` LIKE '%$searchq%' OR `location` LIKE '%searchq%'");
        $count = mysql_num_rows($q);
    }
        
        if ($count == 0) {
            $output = 'There was no search results!';
        }else{
            while ($row = mysql_fectch_array (q)) {
                $organization = $row['organization'];
                $location = $row['location'];
                $output .=  '<div> '.$location.' '.$organiztion.' <div>';
            }
            }
    ?>  

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
"mysql_query" Warning:  This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.  What version of PHP are you running?

https://www.php.net/manual/en/function.mysql-query
Wanda MarstonCreative Director

Author

Commented:
7.1  I pulled bits of code from other pages that I have running.
Hi,

Test your mysql query directly into PHPMYadmin to see if you have any result or errors.
Check PHP logs too to see if any errors
Wanda MarstonCreative Director

Author

Commented:
When I entered the following Mysql query this is what I received, which kind of makes sense.

SELECT * FROM notices WHERE `organization` LIKE '%$searchq%' OR `location` LIKE '%searchq%'

 MySQL returned an empty result set (i.e. zero rows). (Query took 0.0013 seconds.)

There were no errors in the PHP log.

Thanks,
Retry and replace the $searchq by an existing value
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
lenamtl is right.  When you enter it directly you need to use real values because MySQL knows nothing about PHP variables.  And in your PHP code, PHP replaces the variables with the values passed to the page before sending it to MySQL.
Wanda MarstonCreative Director

Author

Commented:
When I entered

SELECT * FROM notices WHERE  `organization`  LIKE 'men die' OR `location`  LIKE 'Tombstone, Wyoming'

I got the same error message

 MySQL returned an empty result set (i.e. zero rows). (Query took 0.0014 seconds.)
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
You forgot to put the  '%' around the data.  Without them, LIKE acts just '='.
SELECT * FROM notices WHERE  `organization`  LIKE '%men die%' OR `location`  LIKE '%Tombstone, Wyoming%' 

Open in new window

Wanda MarstonCreative Director

Author

Commented:
Okay thanks. I actually thought I had tried that so I don't know what I did BUT when I copied and pasted the above code, MySql returned the whole row of information, which is what I am trying to do.

So my code has to get the information and post it on the page so then the person who did the search can then click on it and  communicate with the person who had posted the information.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
You did it in the PHP version above.  Yes to the rest of what you said.
Wanda MarstonCreative Director

Author

Commented:
Thanks for replying so quickly. Experts-Exchange is my life saver.