Link to home
Start Free TrialLog in
Avatar of Wanda Marston
Wanda MarstonFlag for Canada

asked on

How do I add a search function to my website?

How do I add a search function to my website?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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.
Avatar of Wanda Marston

ASKER

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.
leakim971 - I will check out your suggesion. Thanks.
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.
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/
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
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay I would like to try to write my own. Can I do this with PHP?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

"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
7.1  I pulled bits of code from other pages that I have running.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for replying so quickly. Experts-Exchange is my life saver.