Tips On Creating Your Own Search - PHP/MySQL Integrated search on your site

free web hosting
Open Discussion > CONTRIBUTE > The Internet > Web Design

Tips On Creating Your Own Search - PHP/MySQL Integrated search on your site

vistal
I'm not a professional web developer but I've learned a lot since I used Macromedia Dreamweaver. The latest version 8 supports PHP 5. If you select PHP as your working language, you can use this software to automatically add dynamic content to your website with MySQL as a requisite.

If you have a MySQL database containing records like a songs information database, then you might probably not be able to add a good search page to your website which will search records in the database. I faced the same problem, but now I'm able to create one sophisticated database search. I would like to share my knowledge with you. But it's not possible for me to start with a certain point here. I would like you people (interested in creating a search page) to post your problems here & I'll definitely help you.

You can see the search which I created on my past website as a sample of what I'm talking about. Click here to visit that. One thing I used to look for was a search script which could also bold the search terms in the search results, but couldn't find that. Now you can see on my past website that it also bolds the search terms in the search results.

As a demo, put the keyword The in the search box & you'll find out how cool search results page you'll experience there.

I would welcome any queries & suggestions regarding this discussion.

I think nobody's getting time to initiate. Ok then! I start it my self.

Well! of course the search will be implemented on an existing database, so the first thing you need is a database. It may contain any sort of data, but lemme make it a music database.

You must be familiar with MySQL & its implementation to understand what I am going to say below. And if you aren't, then it's better to jump to any other topic. (You may ask any question as well...)

Designing database is not any task. It would become easy if you mess up all the data in a single table, but of course, it would heavily increase the size of your database. Try to divide your data in different tables for optimum performance. For instance, if you are creating a database which would be used for displaying songs available to be downloaded, you need at least 5 tables in my point of view; artists, album, genre, songs, links.
The tables must be related to each other by ID keys, like albums table would contain a column of artistID whose value will be the value in the ID column of artists table, which will show that a particular album belongs to a particular artist.. And same applies to other tables likewise.

Uptil this point, it was an overview of how the database should be sorted out. Now the records should be created. In the next reply to this post, I'll post a sample code to let you understand how the things should go.

Below is a sample MySQL querying performed in PHP to a pre-defined MySQL database. Just look at the code below to get an idea for fetching the results independently from the tables.

CODE
[font=Courier]  // SQL Query


 // ***Songs***  
 // Any keyword Songs (Songs) using $originquery
 mysql_select_db($database_connMembers, $connMembers);
 $query_rsSongs = sprintf("SELECT * FROM songs WHERE title LIKE '%%%s%%' ORDER BY title", $titlevalue);
 $rsSongs = mysql_query($query_rsSongs, $connMembers) or die(mysql_error());
 $row_rsSongs = mysql_fetch_assoc($rsSongs);
 $totalRows_rsSongs = mysql_num_rows($rsSongs);

 // ***Artists***
 // Any keyword Artists (Artists) using $originquery
 mysql_select_db($database_connMembers, $connMembers);
 $query_rsArtists = sprintf("SELECT * FROM artists WHERE name LIKE '%%%s%%' ORDER BY name", $titlevalue);
 $rsArtists = mysql_query($query_rsArtists, $connMembers) or die(mysql_error());
 $row_rsArtists = mysql_fetch_assoc($rsArtists);
 $totalRows_rsArtists = mysql_num_rows($rsArtists);

 // ***Albums***
 // Any keyword Albums (Albums) using $originquery
 mysql_select_db($database_connMembers, $connMembers);
 $query_rsAlbums = sprintf("SELECT * FROM albums WHERE title LIKE '%%%s%%' ORDER BY title", $titlevalue);
 $rsAlbums = mysql_query($query_rsAlbums, $connMembers) or die(mysql_error());
 $row_rsAlbums = mysql_fetch_assoc($rsAlbums);
 $totalRows_rsAlbums = mysql_num_rows($rsAlbums);

 $totalResults = $totalRows_rsSongs + $totalRows_rsArtists + $totalRows_rsAlbums;
 
 $m = 0;
?>
[/font]

Now, the art goes with you, i mean how you place your code in your page to look good is totally upto you.

I've left almost every thing after that code in this discussion which would have to be used to display the search results, because it depends on how you want it to look like.

Finally below is the code to BOLD the search keywords in the search results. You have to somehow integrate this code in to your page, so that each result is processed through this code.

If you are comfortable with PHP, you will surely understand the simple logic which I myself created using a bit of built-in PHP functions.

The if-else statements ensures that all the matches in the record whether they be in capital letters or small letter must be processed for Bold.

The variable $keywords below in the code is for that search terms which user entered in the search box. The rest will be explained by the code itself.

CODE
[font=Courier]<?php  // Bold
       $cnt = substr_count($keywords, " "); // $cnt is the number of words in the query
$val = explode(" ", $keywords); // $val is an array to store all the words separately

for($j=0; $j<=$cnt; $j++) {
  if(substr_count($resultvalue, $val[$j]) > 0) {
    $search[$j] = $val[$j];
 $replace[$j] = "<b>".$val[$j]."</b>";
  } else
  if(substr_count($resultvalue, ucfirst($val[$j])) > 0) {
    $search[$j] = ucfirst($val[$j]);
 $replace[$j] = "<b>". ucfirst($val[$j]) ."</b>";
  } else
  if(substr_count($resultvalue, strtoupper($val[$j])) > 0) {
    $search[$j] = strtoupper($val[$j]);
 $replace[$j] = "<b>". strtoupper($val[$j]) ."</b>";
  } else
  if(substr_count($resultvalue, strtolower($val[$j])) > 0) {
    $search[$j] = strtolower($val[$j]);
 $replace[$j] = "<b>". strtolower($val[$j]) ."</b>";
  }
}
$search = implode(" ", $search);
$replace = implode(" ", $replace);
       
       str_replace($search, $replace, $subject);
       // $search is the text to be replaced
       // $replace is the text which replaces the $search text.
       // $subject is the text which contains $search text.

 }
?>[/font]

I hope it helped you a lot. But if you feel uncomfortable with any statement, feel free to post your problem. I'll try my best to answer you. That's it for this reply to my own topic.

Well, you should keep this thing in mind that your user is going to enter any value in the search terms & your code should be able to process or handle all such requests. For example when you are querying the MySQL server, and if you use the values in the MySQL command from a variable, so you must ensure that the variable doesn't contain any apostrophies in it, else the result could be very unexpected & the next results page will seriously show off that you're a poor programmer.

Then your search form should not make a user get in to trouble, like there's a lot of options like, search in artists or search in albums or search in song titles or like that.. Rather if the user enter any keywords, your code should automatically search for all the possible matches in artists tables & in albums table & in songs tables & then display the results separably without effecting the look of your page.

Another best method by which your website visitor will be able to save time is that if he/she founds some pre-defined searches in a corner, and if the keywords are the same he/she was going to enter in the search box, then a direct click to that keywords link will surely solve the problem. Keep track of the entered keywords by adding some code to your page & then display them on the main page of your website as a separate source.

Now, the point raises which form method to be used. Of course, you must use GET method, because this is how you'll be able to provide the direct search links for the most used keywords.

And, I think, all these things would perfectly make up a complex & effective database search system. I used songs information database as an example here, but the tips n tricks may be followed for any type of database. Hope, you get my point.

Notice from wassie:
Plz put all posts you made about 1 thing in 1 post. and code the queries

 

 

 


Reply

Mithshark
Nice, Maybee this should be moved to tuts? Well either way it was a lovely tut I'm going to attempt to use it now:) ;p Thanks

Notice from BuffaloHELP:
Edited as reported.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. "create search box" grey text - 279.48 hr back. (1)
  2. drupal 6 "own search" - 303.96 hr back. (1)
  3. substr_count replace - 357.51 hr back. (1)
  4. php & mysql ecommerce tshirtshop search box failure - 473.01 hr back. (1)
  5. open discussion character encoding site:trap17.com - 506.47 hr back. (1)
  6. search bar php mysql - 518.84 hr back. (1)
  7. php mysql sophisticated searching - 524.32 hr back. (1)
  8. how to create mysql music database - 564.37 hr back. (1)
  9. i want to create a search bar which can search from mysql database - 600.41 hr back. (1)
  10. auto search box field php mysql database - 607.33 hr back. (1)
  11. php mysql search like to many results - 627.61 hr back. (1)
  12. querying more than one field in php mysql - 682.93 hr back. (2)
  13. how to make the search result keyword bold mysql - 688.44 hr back. (1)
  14. search box in php with mysql - 739.68 hr back. (1)
Similar Topics

Keywords : tips, creating, search, php, mysql, integrated, search, site

  1. Cannot Open My Site
    (11)
  2. Please Review My Site
    (10)
    I've had the same layout for ages but I decided that it's time to get a proper review. My
    website url is: http://princesstimes.com I'd like to know what you think of the layout and
    navigation. Content-wise I don't really need reviews because my website has always catered to
    a specific group of audience so I don't know if everyone here appreciates my content and I
    wouldn't want half-baked criticisms about things they don't understand. But if you want to
    comment on my content, I won't stop you, just that I probably won't take your ....
  3. Golf Tips & Tricks- Improving Your Grip
    (0)
    QUOTE The placement of your hands on the golf club believe it or not has a lot to do with your
    golf game. Holding the club can vastly improve your game providing you follow these tips. You will
    notice a difference. The first basic understanding is that the hand placement on the club guide the
    ball. If you have your hands all over the place then the club will guide the ball in the same
    manner. There are two types of grips, the light grip and the firm grip. If you grip the club too
    tight it will cause the ball to slice. The lighter grip makes for easier wrist movemen....
  4. 10 Top Relationship Tips
    (0)
    QUOTE What's the key to a successful relationship? Some might think that's the million
    dollar question. Sometimes it's just the simple things, that we easily forget or think are
    unimportant that hold the key to a healthy and happy relationship. Read through the helpful tips
    below on how to make your relationship go the distance. 1. Without quality time together, your
    relationship will not survive. Aim to devote at least half an hour a night, and at least one day a
    month when the two of you spend time exclusively together. 2. You both want to feel secu....
  5. Central-gaming. Please Review.
    My new gaming site, that's taken me a few weeks to develop. (4)
    We have come to the time where we must advertise to get some more members. /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> I have finally and officially
    opened up Central-Gaming. A gaming website and forum for people who love to play games, discuss
    games and that lot. Our forum may look boring when your viewing as a guest. We protect our other
    content on the forum from Guests for a good reason, and it is suppose to attract more members into
    joining us. So if your a gaming gamer who loves and/or likes to play games, join us now and get....
  6. Review Innosia.com
    please review my website, this site is currently in trial and testing (6)
    Dear all, Please help me visit http://www.innosia.com This site is about spiritual (lots) and
    computer (still few), and it discusses several spiritual perspective about life and hence has the
    foundation from spiritual teachers who I have met myself and have seen his way of life, way of
    thinking and his much every aspect of life. He can tell something that you don't know as
    spiritual is a way to achieve something miraculously, he often able to know your mind, your thinking
    and always give the best way of understanding yourself so that you could become better by bui....
  7. Defining Regular Expressions
    some tips.... (0)
    Guys, I've got this document today and post here to share with you. I hope it is helpful to you.
    QUOTE In JavaScript, regular expressions are represented by RegExp objects. RegExp objects may
    be created with the RegExp( ) constructor, of course, but they are more often created using a
    special literal syntax. Just as string literals are specified as characters within quotation marks,
    regular expression literals are specified as characters within a pair of slash (/) characters. Thus,
    your JavaScript code may contain lines like this: var pattern = /s$/; Th....
  8. I Can't Seem To Access My Site.
    (8)
    I try to go to my hosted site http://saitunes.trap17.com and it just thinks, then it says the
    operation timed out. Can anyone else access my site?....
  9. My Site Is Crippled.
    I can't access anything (10)
    Hey guys please help. I haven't been able to access my cPanel for a long time now. I think the
    problem started when most members had those password problems. I think that's when I made too
    many unsuccessful login attempts. Now I can't access my cPanel and any site hosted here. Someone
    told me my IP is blocked but I don't know who to tell to have the problem resolved.
    BuffaloHelp(sorry if it wasn't u Buff) stated in one post that he could reset password if nayone
    would write a post with the line "Please reset my password". I did that but still there is....
  10. Site Help
    Been reviewed but needing ideas (6)
    Well seems as some like and some dislike Echo-Of-Thunder we have a few members, which is nice. and
    PLEASE do not think this is spam to get ppl to join, IT'S NOT! What I am curious to know is
    how I can inprove the site to get more members. I had been thinking of maybe adding a radio station
    of my own. I know Bandwith. There is a chat, which nobody uses. I know some do not like or really
    understand weather. Maybe that is the problem I dont know. I have spent a lot of my time on this
    site, and really would like it to be one of the better weather sites on the n....
  11. My Site Is Down :(
    (11)
    QUOTE http://frover.trap17.com/qyfyre.com it has been down since yesterday /sad.gif"
    style="vertical-align:middle" emoid=":(" border="0" alt="sad.gif" />....
  12. Web Design For New Bie
    its a learnng topic for how to design and maintain a web site (17)
    Hi, i had started a new topic, coz i would like to learn - how to start a new web site and manage
    it, how ever i am good at desktop programming....
  13. Search Engine That Pays U 4 Searching
    Did you hear about Scour? It is the next gen search engine with Google (11)
    Did you hear about Scour ? It is the next gen search engine with Google/Yahoo/MSN
    results and user comments all on one page. Best of all we get paid for using it by earning points
    with every search, comment and vote. The points are redeemable for Visa gift cards! It's
    like earning credit card or airline points just for searching! Hit the link below to join for
    free!!! http://scour.com/invite/signup.aspx/ ....
  14. Need Advice On Setting Up Mysql Database.
    I have a huge amount of daily data updates to be inserted. (4)
    First of all, to get the idea of what I am trying to achieve here, please have a look at my
    idea thread, entitled Idea For Using A Cron Job To Grab Daily Travian Map.sql Updates . This gives
    a prelude to the project, what it is for, what it is supposed to do, etc. Now, what I need is
    specific advice on setting up the mySQL database(s) to implement this idea. There is a useful FAQ
    file for how to make use of the map.sql data located on the help.travian.com website, which also
    gives an example of the CREATE TABLE instruction that matches the data in the map.sql fi....
  15. How Many Times Do You Reinstall Windows In A Year
    There goes some useful tips about it. (19)
    I reinstall (restore) windows almost every 2-3 months. What about you??? Seeing the title some would
    say "I do it daily" and some others "How to reinstall?". Let's see the poll results. Linuxers,
    see you in other post. Reinstalling windows is a daily chore for the students studying computers.
    They have to switch a lot of softwares, compilers etc etc. Windows as we know, will not tolerate a
    whole lot of (Un)installations and will ultimately slow down the whole system. This is not the case
    with Linux. Just consider this, I installed Red Hat WS 3(the older version whi....
  16. Can Someone Rate My Site?
    Well i created this site about a month ago and i need some tips (11)
    I'm not as advanced as other website designers, but about a month ago i designed this local news
    website. I need someone to rate it and tell me what they think and give me some pointers to fix it
    up or even change the look completely. I've changed the design about 3 times already. It
    contains local news about my home country Belize and i only take about an hour to update daily. Any
    pointers, suggestions, questions i will appreciate. Armond Times Belize ....
  17. Best Browser To Desighn Your Site To
    not much of a question but more like a statement (28)
    well since i started web desighning i've always used internet explore (why?) well when i first
    tried FF2, i wasn't a "web-designer" then so i didn't care much about css and htlm stuff, i
    din't like it and one of the reason was beacause a couple of the sites i visited wasnt firefox
    compatible. but anyways recently i was desighning a site for this community. so like two weeks ago i
    downloaded FF3 for testing purposes. in IE the site looked great but what i found was that in
    firefox the site was horible, as bad as they could come. so since firefox was better....
  18. Workout Tips?
    Gym and more! (7)
    Does anyone have some tips or websites I can look on for my workout on the Gym?....
  19. Review My Site
    Review my site please (30)
    Hello. I am new here to trap17.com and would really like some input on my site. Echo Of Thunder
    Made using Dream Weaver and note pad. Planning on a better forum and chat page as time goes by.
    This is just a little hobby that I have. "weather" and I hope that you all here at trap17 will take
    a look see, and tell me what you all think. ....
  20. Review My Site?
    At www.chantellepaige.org (16)
    Ok.. so. after almost trowing up on how disgusting my site used to look. i tried something
    different.. please visit here . Its a flipsyde & chantelle paige fansite.. doubt you know who
    they are.. but still i need opinions & advice on the way the site looks XD....
  21. Job Site
    Colors alright? (9)
    http://www.jzambrano.trap17.com I've had this site for a while but have been starting to
    question whether I perhaps should change the colors to something less vivid and more neutral like
    navy blues, greys, etc. Do you think it needs to be changed, and if so, to what? Also, if you
    have any thoughts on site features or constructive criticism about the site, that would be
    appreciated too.....
  22. Forget Adsense. Try Widgetbucks.com
    earn more money with your site (44)
    Hello . A new company that blow all the Google Adsense program is here , idk if its operating
    from long time , i dont think soo but its realy realy good. Forget of 0.01 $ from Google
    Adsense , Widget pays you from 0.45 pay per click no less depending what widget you choose ,
    3$ at 1000 immpresions . I put Laptops widgets and i get 0.45 $ no less . Its more
    then good in one day ived made 30 dollars from 1 site and i have more then one Register now and
    you get 25 $ BONUS : Please signup over the link below . Thanks http://www.wid....
  23. Microsoft Search Engine
    Getting a huge upgrade to compete with google (20)
    QUOTE "SEATTLE - Microsoft Corp., the No. 3 Web search provider in the U.S., is rolling out
    changes to its search engine aimed at narrowing the gap between it and market leader Google Inc."
    Source It looks like microsoft is trying to make a super search engine to compete with google. I
    thought what they did to the live search was a big change but it looks like they have a lot planned
    for live search. There are some features that i like that they are going to add but some that is
    just going to put to much junk on the page when you are searching. QUOTE "It....
  24. Check This Totally Css Site Out
    (6)
    Check out this and give me ur opinion www.eskiotongroup.com....
  25. Real Paying Site
    Easy and quick (10)
    That site really pays and I have recieved my first payment of 10 dollars. This is really easy not
    much of a big minimum payout and if you want you too can make money by just going here and signing
    up http://adbux.org If you want please do add me as a referrer as zak92 Thank you.....
  26. Harry Potter Roleplay Site
    suggestions, comments, all welcome (6)
    Hi, everybody, I recently finished a harry potter roleplay site of mine that's a subdomain on my
    main site (thenr.net) the url of my harry potter site is http://morsmorde.thenr.net We currently
    have 8 members, but are hoping to grow quickly, and we're looking for experienced, seasoned
    roleplayers. Many main characters are still free, the site is also all canon, one of very few all
    canon sites on the web. - we're in dire need of members to get the fun starting - Anyway, check
    it out and tell me what you think! (I'm Cedric Diggory on the site). Tha....
  27. The Search Engine At Blogger.com
    (4)
    I just wonder, how many of you are using the search at blogger.com to search weblogs? I sometimes
    do. Most of us would like to know, say, what are the first hand comments from diners, software
    users, and travelers etc before we visit a restaurant, buy software and travel to a certain
    destination. We think that the comments on those personal sites should be more impartial then
    coverage from commercial gourmet magazines, software reviews and travelers’ guides. Although there
    are now some biased notes on the weblogs, I still think that blogs from others give me a lot of inf....
  28. Quick Question
    Is there a limit on the size of the MySQL database? (10)
    Question's in the title!....
  29. Secrets To Getting To The Top Of Google
    Search Engine Optimization Tips for Google (46)
    Here, you will give advice (tutorials) on how to get to the top of Google.....
  30. Help To Get Traffic For New Site
    submitting url to google? (23)
    aye there, Just want to know the quickest and best way to start getting good ranks in google search
    engine. Im making a new site from scratch and i decided to use meta tags including the robots, index
    all tag. If i already have the robots tag on each of my pages, could i still submit my main URL to
    google ( http://www.google.com/addurl/?continue=/addurl ) and not worry about them ignoring it? or
    could i just leave my site as it is and google will automatcally send a spider crawl to my site and
    add it to their directory?.. im just confused about this all, i just want to ....

    1. Looking for tips, creating, search, php, mysql, integrated, search, site

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for tips, creating, search, php, mysql, integrated, search, site

*MORE FROM TRAP17.COM*
Similar
Cannot Open My Site
Please Review My Site
Golf Tips & Tricks- Improving Your Grip
10 Top Relationship Tips
Central-gaming. Please Review. - My new gaming site, that's taken me a few weeks to develop.
Review Innosia.com - please review my website, this site is currently in trial and testing
Defining Regular Expressions - some tips....
I Can't Seem To Access My Site.
My Site Is Crippled. - I can't access anything
Site Help - Been reviewed but needing ideas
My Site Is Down :(
Web Design For New Bie - its a learnng topic for how to design and maintain a web site
Search Engine That Pays U 4 Searching - Did you hear about Scour? It is the next gen search engine with Google
Need Advice On Setting Up Mysql Database. - I have a huge amount of daily data updates to be inserted.
How Many Times Do You Reinstall Windows In A Year - There goes some useful tips about it.
Can Someone Rate My Site? - Well i created this site about a month ago and i need some tips
Best Browser To Desighn Your Site To - not much of a question but more like a statement
Workout Tips? - Gym and more!
Review My Site - Review my site please
Review My Site? - At www.chantellepaige.org
Job Site - Colors alright?
Forget Adsense. Try Widgetbucks.com - earn more money with your site
Microsoft Search Engine - Getting a huge upgrade to compete with google
Check This Totally Css Site Out
Real Paying Site - Easy and quick
Harry Potter Roleplay Site - suggestions, comments, all welcome
The Search Engine At Blogger.com
Quick Question - Is there a limit on the size of the MySQL database?
Secrets To Getting To The Top Of Google - Search Engine Optimization Tips for Google
Help To Get Traffic For New Site - submitting url to google?
advertisement



Tips On Creating Your Own Search - PHP/MySQL Integrated search on your site



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE