Nov 22, 2009

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

free web hosting
Open Discussion > MODERATED AREA > 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

 

 

 


Comment/Reply (w/o sign-up)

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.

Comment/Reply (w/o sign-up)



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*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

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

  1. Help With My Site
    had a idea and don't have a clue if it can be done (6)
  2. Designing An Online Community Center Site
    (4)
    Main Site Sections => Forums, Gallery, Free Web Portal Access, Site Information/Contact Info, Free
    Downloads Directory/Infobase. Desired Site Elements => Localized Site Search Engine/Bar, Login/Sign
    Up, Automated Feeds/Section Updates, Dynamic Page Generation/linking. Preferred Code Languages=>
    C++, PHP, ColdFusion, Javascript. Website Type: Dynamic Database-Driven. Specs: Windows XP
    x86(win32) OS w/ .NET Framework; SeaMonkey, FireFox, and IE7 Browsers currently installed. If
    anyone can give me any info about how best to go about this project, it would really help. ....
  3. How To Create Subdomain Using Php?
    creating subdomain automatically after signing up (6)
    Hi everyone, i really don't know if this is the right place, so please just move this to the
    right category.. now here's my problem: I want to create a website for my high school batch
    graduated last 2004 so we can interact to our old friends. but i want it to be like ning.com,
    myspace.com or friendster.com that you can add your own layout (css) for your profile and putting
    the new user in it's own profile page (using a subdomain for their own profile page) can anyone
    tell me how to automatically create the subdomain and putting the CSS and other files in....
  4. How To Add The Flash In Our Website ?
    Animated image in website make the site atractive, but take more time (4)
    Animation in this world: We see in most of the website that company icon, logos, memorandom,
    Advertisement, smily,even todat Button etc. in the website are animated in a sequencial manner so
    any body who see this get impressed, even so many time i also get impressed and want to give thanks
    that one who creates this logic a fantastic idea, adobe website we read some articles, i want a
    proper process or command from starting to end till image(how many types of image) animated in my
    website. what software, hardware, required to do complete this task. any idea, tutorial, not....
  5. Transfering A Joomla Site
    How do I make sure I don't get errors? (0)
    I was designing my Joomla powered site on a laptop and the XAMPP folder was on drive D. I zipped
    this folder and stored it somewhere before unistalling XAMPP and deleting everything. I installed
    Joomla on another machine but this time XAMPP is in C:\ . After replacing the contents of the
    \htdocs\ with the saved ones I can't access any page. All I'm getting now is: QUOTE
    Warning : require_once(D:/xampp/htdocs/site/includes/joomla.php) : failed to open stream: No such
    file or directory in C:\xampp\htdocs\site\administrator\index2.php on line 31 Fat....
  6. Web Design For New Bie
    its a learnng topic for how to design and maintain a web site (18)
    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....
  7. Creating A Floating Javascript Docking Menu
    (1)
    I have found two scripts. I found one(shown bellow) here that creates a floating box that will
    follow you down the page when you scroll. I found another one here that creates a docking menu
    like that of OSX. Now I want to combine these effects to create a docking menu that floats and
    follows you everywhere you scroll to on the page. In the above code I change the div class from
    floatdiv so that it could use the style in the style.css that came with the docking menu. I then got
    stuck trying to make both JavaScript scripts work on the same div. I'm appealing ....
  8. 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....
  9. Creating A Fully Featured Cms
    I am in the process of creating a fully featured CMS and need help (0)
    Hello, my name is Derek and I am in the process of creating a CMS. My CMS will be slightly different
    than most, because I am creating it for business purposes. I own a business, Black Development &
    Produktionz, LLC. and I am creating all of my own services that I will need, such POS (web based to
    be integrated with virtually any OS), a scheduling system (for appointment and so that my client
    will be able to schedule based on open time slots), an emailing system (which I know will require
    more than effort than a normal CMS because it will depend on hosting and so forth), ....
  10. Creating A Good Website, How?!
    (19)
    Creating a good website, How?! I looked at many forums, searching on now to create a good website,
    by good I meant good website interface. For example, Trap17 have this amazing flash header, and nice
    design… I searched and searched, I found that many people started with a photoshop picture, then
    they make it come true by requesting a website coder (A.K.A. Programmer) to code the whole website
    for it. If, I said if, I were good a art, I can design a good nice picture off photoshop, and I
    know how to code, does that means I can make a good website? Please post any comm....
  11. Building A Forum Under Another Web Site?
    (8)
    Hello, Im going to set up a simple website to use with my buddies. A main page where I can update
    from time to time and a forum are all I need. I know WYSIWYG builders, ftp uploaders and forum
    moderating. Im thinking of using phpBB since it's free and seems simple and reliable, right?
    My question is can I build the forum on my local computer then upload to hosting service? The
    readme.txt recomends to install online in host machine. And this is what I need to do, right?
    -build an index.html with WYSIWYG, upload it to main folder of host. -create a folder in the....
  12. Help Creating A Profile Website
    how do i make a profile website (20)
    Ok this is my idea: I am making a website about anime. Its function is add anime, review anime,
    search anime, anime information. Users can make a profile. People can make a profile which they can
    edit to theire likes with html to give it a fancy look.(this is what my problem is about) What i
    would like to know is how can i accomplish this. I already have the database for the anime titles
    etc ready, the only thing that needs to be done is the profile section. If you have any tutorial,
    tip or guide i would really really appriciate it. Thx in advance. ....
  13. How To Make A Website
    (If you're trying to drive people away from your site) (30)
    Alright so there are many topics out there of how to make a website that everyone will love and want
    to go to, but I can't seem to find any about how to make a website that people will hate and try
    to run away from, so here are some pointers for those who are trying to make the worst website:
    Step one: COLORS Be sure to use a vivid and bright background color, and a non-noticeable text
    color. Nothing wakes people wake up more than a florescent yellow background with white text. Make
    people work to try to read your website. After all, you have some great content....
  14. Ideas For New Fan Club Site!
    (5)
    Well, I'm working in a fan club site, this is like our 5° site because every host in which
    we've been in always change something and the site stops working. The site is a Black Eyed Peas
    fan club site, it already has profiles (with gallery and comments box) for each
    user,forum,chat,videos,gallery,lyrics,downloads,bios of the members of the BEP, but I think that the
    site need something else. Please gimme some ideas to make this site the best! /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />....
  15. Where Is There A Good Site To Learn Web Html?
    (20)
    im a noob when it comes to web html to design web sites, can some one tell me where to find a good
    website that has good tutorials on how to use web html?....
  16. Want To Open Shopping Portal In My Site
    What i do (2)
    I want to open a shopping portal in my site which will do the following: take the information from
    the visitor what good they want to sell then directly post it on my site What script i choose that
    will suit the website of mine could any body tell me....
  17. My New Site Template
    Heres a new template of mine done in photoshop (3)
    This is my new template I'm going to be using for my new site. It took me 3 days to do. I spend
    day 1 drawing it up sorry I cannot upload my draft sketch as my scanner is not working. The next 2
    days I spent drawing it up in photoshop. The colours are kinda generic I know but thats my style. I
    cant seem to create anything other then grey toned things. I think adding some decay onto the panels
    will give it some depth something my style of drawing lacks. The panels - I like as many panels as
    I can so I went with the 3 column layout and extra 2 side header panels for ....
  18. Free Site Counter Stats
    (7)
    hi frnds everyone want to keep an eye on their site traffic. histats is a really great one i found
    with gives site stats for free. it have many amazing features. visit www.histats.com i am
    sure u will like it... this is my first post so excuse me if my post have any mistakes....
  19. How To Attract Users To Register On A Site
    (13)
    :rolleyes:Use great graphics an good color which attracts the users/views eyes an capturers there
    attention which cuases them to view your website if its good enought they'll register. Also use
    good images, links an have blogs an forums also start a referring website such as like this: Visit
    this site and know all the info about the CHITWAN http://hamrochitwan.com Some thing like that
    would get more users depending on if the items in your shop are GOOD maybe like moderator for a week
    costing 100 referals or somethink. /blink.gif" style="vertical-align:middle"....
  20. A Small Html Problem
    How to display foreign characters correctly when designing a site. (5)
    I was wondering how I could solve a small problem. I was told that some people see accented and
    umlauted letters (such as " é " and " ë ") as question marks (" ? ") on my website. I come across
    the same thing sometimes when looking at websites which use non-English characters. Funnily enough,
    the other day, I looked at a site and the apostrophy ( ' ) was also shown as a question mark.
    That is a very common character usually, I would think. I thought it had something to do with the
    character encoding settings, and let me also mention I use Mozilla Firefox as my brows....
  21. Getting Flash Images On A Site
    (1)
    well, i don' t have a site yet, but about 6 months ago i made a template, with flash buttons and
    all, with sound and everything i was so proud so i tryed to put em on there with a special code u
    needet or something ,so i did, but i got blank images,, i even tryed uploading it on the site it
    didn't work, so i asked around they said u can' t install them on a cpanel, u need ftp, so i
    downloadet the program needet and i got stuck there, so basicly how do u do that,and could some1
    help me out how to get it working on a cpanel....
  22. Drupal Related
    Auction site (1)
    Hi, I've been trying to look for ways to run an auction site thats easy to set up and I saw
    drupal... Is there a way to run an auction site using drupal?? If anyone of you know how to do it
    plzz help me! I've tried to look through google but not getting any help! I want to make an
    auction site which will be able to take few types of product posted by the users and let other users
    log in to bid for the price! Is there a way to do so?? And if anyone of you know an easier way to
    go about doing it please help me! even if its not drupal but something and works!! Tha....
  23. Adding Second Page Of Cutenews To My Site
    How do I do this? (3)
    I'm using Cutenews on my website. I forgot to mention in my last post on this topic that
    Cutenews is news system that can be integrated into an existing website and can function like a
    blog. Well, I recently integrated cutenews into an existing site, and I'm wondering how I get
    the older news to show up on another page. For example, I'm using cutenews on my main index
    page. At the bottom of the page I'd like to put a link that says, "old news," and have that page
    open up into a new page of my older news/blogs that people can still post comments on. What....
  24. View Souce Code
    do you know this site? (8)
    I have seen this site before.The site is simple.there are a box that you can put a url link then
    press enter you can see the code of the page (see everything include meta,comment...),even it is
    written in php,asp... If anyone know this site,please let me know(I have format my computer,so I
    lost it).thank you. ....
  25. Please Suggest Me How To Do ....?
    My First Web Site ..... ? (11)
    Hi buddies . I just got free Web Hosting by Trap 17 , it is a real great pleasure for me. Now there
    are several problems in my mind regarding to CREAT my own FIRST website. * Which software should
    i use for building my website ? * I want to know how to show the IP of the visitor on the welcome
    page of my site? * I want to place e-messenger on my site , how to place it ? * I want to create
    different flash and java effects on my site how to create them? As in my earlier post i told that i
    just start using computer so please help me to solve my problems described a....
  26. Fast Site Indexing By Blogging?
    (8)
    I've heard many people use blogging for the fast indexing, and even there is software like
    blogging equalizer that allows you to index your site and all pages in couple of hours. What I need
    is a VERY detailed process of creating blog and how to get your site indexed fast. Please share
    your experience and knowledge!....
  27. "un-supported Browser" Message
    Site won't let me in because of it. (5)
    Boy, this is a new one for me. I know enough about the Web to know that it is possible to detect the
    Browser which is being used to call a page, and I know that there are differences between Browsers.
    I also don't think this is right, but has anyone else ever seen the situation I am about to
    describe? Cruising the Net. Click on a link. The Browser does its thing and an error message pops
    up to say "Sorry, your browser is no longer supported by Micro$oft, so you can't view this
    page. " Now, I could understand if it was a Micro$oft site or a techy style page, bu....
  28. How Can I Make My Site Load And Work Faster?
    (21)
    i just wanted to know some extra tricks and tips i can use to make my site work great, being fast,
    and work well on all browsers. i think this will also help a lot more people and not just me. so
    lets start a list of things we can do to make our site work better, faster. start your commenting.....
  29. Creating Cateories With Unlimited Sub-categories
    (2)
    This tutorial will show you how to create a script so that you can have multiple categories with
    unlimited sub-categories in each. They can be as deep as you want. This is very useful for
    organising data/items. A prime example of this is a link directory which has multiple categories.
    QUOTE Ok first of all I will just briefly explain how this will work - the logic behind it. I will
    then move on to writing the actual script which will manage the categories and put specific
    data/information into its category. The Logic Behind Categories If youve got a fairly decent a....
  30. Help With My Site
    (5)
    I would like to put a forums onto my site and I have no idea how to. Please tell me how to create
    one. Thanks!....

    1. Looking for tips, creating, search, php, mysql, integrated, search, site
Similar
Help With My Site - had a idea and don't have a clue if it can be done
Designing An Online Community Center Site
How To Create Subdomain Using Php? - creating subdomain automatically after signing up
How To Add The Flash In Our Website ? - Animated image in website make the site atractive, but take more time
Transfering A Joomla Site - How do I make sure I don't get errors?
Web Design For New Bie - its a learnng topic for how to design and maintain a web site
Creating A Floating Javascript Docking Menu
Best Browser To Desighn Your Site To - not much of a question but more like a statement
Creating A Fully Featured Cms - I am in the process of creating a fully featured CMS and need help
Creating A Good Website, How?!
Building A Forum Under Another Web Site?
Help Creating A Profile Website - how do i make a profile website
How To Make A Website - (If you're trying to drive people away from your site)
Ideas For New Fan Club Site!
Where Is There A Good Site To Learn Web Html?
Want To Open Shopping Portal In My Site - What i do
My New Site Template - Heres a new template of mine done in photoshop
Free Site Counter Stats
How To Attract Users To Register On A Site
A Small Html Problem - How to display foreign characters correctly when designing a site.
Getting Flash Images On A Site
Drupal Related - Auction site
Adding Second Page Of Cutenews To My Site - How do I do this?
View Souce Code - do you know this site?
Please Suggest Me How To Do ....? - My First Web Site ..... ?
Fast Site Indexing By Blogging?
"un-supported Browser" Message - Site won't let me in because of it.
How Can I Make My Site Load And Work Faster?
Creating Cateories With Unlimited Sub-categories
Help With My Site

Searching Video's for tips, creating, search, php, mysql, integrated, search, site
See Also,
advertisement


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

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com