anyone know how to add a search bar?
search bar?
(6 posts) (2 voices)-
Posted 7 months ago #
-
Adding the search bar per se is not the difficult thing: that's just a simple form with an input field and a button. It's actually doing the search.
You can use a google search bar, or tell us more what you want from this search function (example of searches and what is should find), so we can help guide you writing your own.
Posted 7 months ago # -
well...it's for my husband's blog. He needs a basic keyword search that will turn up results of posts that include a specific word or phrase. Does that make sense?
Thanks,
BeckiPosted 7 months ago # -
Well the HTML part is pretty simple:
<form method="GET" action="search.php"> <input type="text" name="searchQuery" /> <input type="submit" value="Search!" /> </form>As for search.php:
if(count($_GET)>0 && isset($_GET['searchQuery'])){ //sanitize user submitted data (VERY IMPORTANT!) $srcQuery = strip_html(addslashes(trim($_GET['searchQuery']))); $sql = "SELECT * FROM <code>php_blog</code> WHERE title LIKE '%".$srcQuery."%' OR entry LIKE '%".$srcQuery."%'"; //connect to the DB mysql_connect ('localhost', 'db_username', 'db_password') ; mysql_select_db ('db_name'); $result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error()); mysql_close(); $resultCount = mysql_num_rows($result); if($resultCount > 0){ //parse list of results while($row = mysql_fetch_array($result)) { //display entry and format it nicely } } else { //didn't find anything } } else { //stuff to do if search query is missing }The article http://girlswhogeek.com/tutorials/2004/part-3-display-entries shows you how to display the list of entries formatted in a nice manner.
Posted 7 months ago # -
Sorry this took so long...but it's not working. Keeps telling me that I have a syntax error on the line that says,
$resultCount = mysql_num_rows($result);
Not sure why...any ideas?
Posted 6 months ago # -
nevermind...I figured it out...nothing was being echoed.
Thanks
Posted 6 months ago #
Reply
You must log in to post.