Member Offer
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Coding a search function?

Discussion in 'Website Design Forum:' started by nexusbird, Jun 4, 2010.

  1. nexusbird

    nexusbird New Member

    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:
     
  2. Harry

    Harry Senior Member

    Does your site run off a database?
     
  3. nexusbird

    nexusbird New Member

  4. nexusbird

    nexusbird New Member

    havnt actually built it yet but i want to know how to do it before i put it in my design plan
     
  5. Harry

    Harry Senior Member

  6. nexusbird

    nexusbird New Member

    yeah i dont mind adding my own database.
     
  7. Harry

    Harry Senior Member

    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.
     
  8. Greg

    Greg Active Member

    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.
     
  9. glenwheeler

    glenwheeler Senior Member

    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.
     
  10. Jazajay

    Jazajay Active Member

    @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
     

Share This Page