Avatar of Wanda Marston
Wanda Marston
Flag for Canada asked on

How do I add a search function to my website?

How do I add a search function to my website?
PHPJavaScript

Avatar of undefined
Last Comment
Wanda Marston

8/22/2022 - Mon
Dave Baldwin

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.
leakim971

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Wanda Marston

ASKER
leakim971 - I will check out your suggesion. Thanks.
David Favor

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 Marston

ASKER
Thanks for your reply.

URL - www.tiredofbeingrippedoff.com
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
lenamtl

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 Marston

ASKER
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
Dave Baldwin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Wanda Marston

ASKER
Okay I would like to try to write my own. Can I do this with PHP?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wanda Marston

ASKER
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 Baldwin

"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 Marston

ASKER
7.1  I pulled bits of code from other pages that I have running.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wanda Marston

ASKER
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Dave Baldwin

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 Marston

ASKER
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.)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wanda Marston

ASKER
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wanda Marston

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