• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Coding a search function?


#1
Hey guys, i want to add a search box to my designs but i dont know how to code one or go about intergrating it with my site, any help would be appreciated. :up:
 

Harry

Senior Member
#7
Okay well if you content manage it you'll likely have a table called pages, then you'd build a form which takes a user input (the search term) and the the result of that you'd run a search for. Something like

PHP:
$result = mysql_query("SELECT * FROM pages WHERE pageContent LIKE $searchTerm");

while($row = mysql_fetch_array($result)){
  echo $row['pageTitle'];
}
That's me as a non-programmer so there may well be someone here who'll either correct it or show you a much better way.
 

Greg

Active Member
#8
In addition to Harry's replies, if you're building on a 'CMS' that has it's own database, chances are it will have it's own search function, so would then just be a case of styling the front end search box and search results page how you need it.
 

glenwheeler

Senior Member
#9
It sounds like you need a site running on a CMS maybe wordpress or Drupal? The search function is already built for you. If you don want to build your own then you would need to know a bit of php or asp depending on how you want to develop the site. There's a guy on here called MCBWebdesign he can probably shed some light on this one for you a little more as he knows quite alot about this sort of thing.
 

Jazajay

Active Member
#10
@Harry
Liking the coding sniz fella, few changes but otherwise on the right path, impressed. :)

@Salamander
Use this, I will build on Harry's code.
<?php
//This is the results page, you would need a form and point it to this page for it to work, o and also a database.
if(isset($_POST['result'])){ //See if the form was submitted
function url($var){ //Create a function that we can use to create search engine friendly URLs with.
$rewrite=strtolower(ereg_replace(" ","-",$var)); //Takes a variable replaces any spaces with hyphans and turns all the letters to lower case.
return $rewrite; //returns our new variable.
} //End of the function
$searchTerm=strip_tags($_POST['result']); //Saves the search term to a variable and removes any html added to it.
$cxn=mysqli_connect("localhost","[User name]","[Password]"); //Connects to the database.
$result=mysqli_query($cxn,"SELECT * FROM [Database].[Table_name] WHERE [coloumn] LIKE '".$searchTerm."%'"); //Searches a table of the database for any terms that might match the users term.
?> //Ends the PHP section.
<ul> //Starts a unordered list.
<?php //Starts a new PHP section
while($row=mysqli_fetch_assoc($result)){ //While their is a match to the users query.
extract($row);?> //Extract the array into variables so we can use them.
<li><a href="yoursite.com/<?php echo url($category)?>/<?php echo url($subCategory)?>/<?php echo url($categoryName)?>.php"><?php echo $categoryName?></a></li> //Echo out to the browser the url to the article in a SEO friendly way bringing in the earlier function in a list form.
<?php }?> //end the php section.
</ul> //end the unordered list section
<?php }else{?> //If no post variable is set the form was not submitted so echo out a message to the user saying search again dumbass.
<p>We are sorry an error has occured, please search for your item again.</p>
<?php }?>

Key:
Red: PHP
Green: XHTML
Orange: Commented code

Please note that is basic the post variable should be screened more for more unwanted characters, their should be a message if thier is no results etc... etc... Also I have just wrote that off the cuff so I'm not saying it's error free either.

But if you want a hand getting it to work let me know.

Jaz