Search Bar - Now with Ajax! (Preview)

Post your creations here for feedback and to share them.

Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 11:56 am

Hey :)
You may remember me from such plug-ins as http://community.iwebkit.net/viewtopic.php?f=16&t=339 and now, i am back!

There has been one or two topics based on searches around the forum.. and how to use one, and specifically an aax search, so i built one! I have a demo, and screens.

Screens
Image Image

P.S. The search button is purely cosmetic :)

You can take a look at my demo here: http://tarnfeldweb.com/dev-kit/ajax-search.html and tell me anything i could improve :)

Oh and by the way... it searches as you type: I currently have the following search terms in my database

    Google
    Bing
    Yahoo
    Ask

Download
http://tarnfeldweb.info/dev-kit/ajax%20search.zip
Please read the README.TXT file

How to modify the php search for your database:
Quite a few people has asked, how can i modify this for my already implemented database? Well here is a hopefully useful short tutorial.

My database, has the following fields:

id (auto increment)
name
url

And you can see with this php (commented) how these fields are taken into consideration:

Code: Select all
$sql = "SELECT * FROM `pages`
           WHERE `name`
           LIKE \"%"
           .$_POST['query'].
           "%\" ORDER BY id ASC;";


(Don't separate this out onto lines in your actual code!)

Lines commented:
1) Declaring the mysql query statement and SELECTING *(all) FROM the table `pages`
2) Where the field `name`
3) .. is LIKE
4) %(wildcard) POSTED QUERY %(wildcard)
5) Order rows by their ID in the database, ASCending

The use of wildcard, allows anything before or after the query, this is the real 'search' part.

Now we return all the rows in the database (search results) in a nice friendly format, that the good old jquery POST asked for :)

Code: Select all
if(mysql_num_rows($result)==0){
      echo("<li class=\"textbox\"><p>No search results matched your query</p></li>");
   }
   
   else {
      while($row = mysql_fetch_array($result)) {
         echo("<li class=\"menu\">");
            echo("<a href=\"".$row['url']."\">");
               echo("<span class=\"name\">".$row['name']."</span>");
               echo("<span class=\"arrow\"></span>");
            echo("</a>");
         echo("</li>");
      }
   }


Lines commented again:

1,2) If the number of rows returned is 0, echo on the page that no results were returned
3) Else do the following:
4) For each item in the arrayed rows, returned by the search SQL statement, do the following:
5) Make a new <li class="menu">
6) Make an <a> which links to the url that is stored in the database for that row
7) Echo out the 'name' field stored in the database for that row into an <span>
8) Shove in an arrow
9,10,11) Close it all off


I hope this is any use to you? I dont really want to explain it even more.. but here is a tip for searching in more than one field in your database:

Code: Select all
$sql = "SELECT * FROM `pages`
           WHERE `name`
           LIKE \"%"
           .$_POST['query'].
           "%\"
            OR
           `othername`
           LIKE \"%"
           .$_POST['query'].
           "%\"
            ORDER BY id ASC;";


Simply use the "OR" operator in your mysql statement :) You can use as many as you like!
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Re: Search Bar - Now with Ajax! (Preview)

Postby crmunro » Sun Oct 11, 2009 12:11 pm

Genius! thank you sooo much! Should save me a fair few hours of work ;)

So I should be able to modify it to work with my mysql db tables and display the results in the music store list, right?

That's fantastic :D
wadelaube wrote:I have created a mobile menu system with iWebkit, which is working really well, even though I have no idea what I am doing.
User avatar
crmunro
Devotee
Devotee
 
Posts: 307
Joined: Wed Sep 30, 2009 7:29 pm
Location: Perth, Australia

Re: Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 12:18 pm

Haha thanks, only took me about 40 mins...

If you add me on some kind of IM (look on my profile) i will help you modify it for your table :)

Tom
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Re: Search Bar - Now with Ajax! (Preview)

Postby crmunro » Sun Oct 11, 2009 12:26 pm

I added you, cameron.99@hotmail.com- when will you be on?
wadelaube wrote:I have created a mobile menu system with iWebkit, which is working really well, even though I have no idea what I am doing.
User avatar
crmunro
Devotee
Devotee
 
Posts: 307
Joined: Wed Sep 30, 2009 7:29 pm
Location: Perth, Australia

Re: Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 1:19 pm

I added you :)
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Re: Search Bar - Now with Ajax! (Preview)

Postby GeoNomad » Sun Oct 11, 2009 1:51 pm

Very nice! Excellent work! I will have to add that to my Pocket History app. You realize you are making more work for me with this kind of thing. :lol: I had been thinking about adding Ajax to the search, but was too lazy. Now you have shamed me into it.

Tip:

Image

You probably know this, but others reading this thread may not. You can change the Return key on the iPhone keyboard to Search or anything else you like by adding title="Search" to the <input type="text tag and surrounding the inputs with a <form></form>. The last part is lame, but necessary.

Peter
Make your iPhone happy with webapps from mwebapp.com
User avatar
GeoNomad
Admirer
Admirer
 
Posts: 31
Joined: Thu Aug 13, 2009 4:53 pm
Location: Nomadic

Re: Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 1:56 pm

Thanks :)
No, i didnt know that... i do now
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Re: Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 5:08 pm

You can only do title="Search" not anything else
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Re: Search Bar - Now with Ajax! (Preview)

Postby GeoNomad » Sun Oct 11, 2009 6:04 pm

tarnfeld wrote:You can only do title="Search" not anything else


Funny I hadn't tried anything but Search and Go.

"Go" also works.

I wonder what other words work...
Make your iPhone happy with webapps from mwebapp.com
User avatar
GeoNomad
Admirer
Admirer
 
Posts: 31
Joined: Thu Aug 13, 2009 4:53 pm
Location: Nomadic

Re: Search Bar - Now with Ajax! (Preview)

Postby tarnfeld » Sun Oct 11, 2009 6:09 pm

you wouldnt need to put Go as thats default
Portfolio - http://tarnfeldweb.com
Tidy Design - http://tidydesign.com

Check me out ;)
ares wrote:In fact I wouldn't use tables at all.
User avatar
tarnfeld
Moderator
Moderator
 
Posts: 764
Joined: Tue Jan 27, 2009 7:29 pm
Location: Portsmouth, UK

Next

Return to Your creations

Who is online

Users browsing this forum: No registered users and 0 guests

cron