SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Suggestion Box for related keywords for my website.

    Hi,

    I am trying to create a suggestion box for my website.....which should must show keywords suggestions like this.....






    So if I am searching a single word or a full string it should show all the related keywords as given above.

    I hope I've explained enough to get my point.

    If there's something unclear please let me know.

    Thanks in Advance.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What code do you have so far?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well its here:

    I am new so there may be silly mistakes.....

    PHP Code:
    $pieces preg_split('/[\s&-]+/',$keyname);
                        
    $break count($pieces);
                        if (
    $break==5)
                        
    $sql_category="select * from category where category like '$keyname%' or category like '$pieces[0] $pieces[1]%' or category like '$pieces[2]%' or category like '$pieces[3]%' or category like '$pieces[4]%' order by category LIMIT 50";
                        if (
    $break==4)
                        
    $sql_category="select * from categorywhere category like '$keyname%' or category like '$pieces[0] $pieces[1]%' or category like '$pieces[2]%' or category like '$pieces[3]%' order by category LIMIT 50";
                        if (
    $break==3)
                        
    $sql_category="select * from category where category like '$keyname%' or category like '$pieces[0] $pieces[1]%' or category like '$pieces[2]%' order by category LIMIT 50";
                        if (
    $break==2)
                        
    $sql_category="select * from category where category like '$keyname%' or category like '$pieces[0] $pieces[1]%' order by category LIMIT 50";
                        else
                        
    $sql_category="select * from category where category like '$keyname%' or category like '$pieces[0]%' order by category desc LIMIT 50"
    Any expert comments would be appreciated.

    Thanks

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This will build the SQL query for you with an unlimited amount of keywords.
    PHP Code:
    <?php
    $sql 
    'SELECT * FROM category WHERE 1 <> 1';
    foreach(
    preg_split('/([\s&-]+)/'$_POST['keywords']) as $keyword){
        if(
    strlen($keyword)){
            
    $sql .= sprintf(
                
    " OR category LIKE '%%%s%%'",
                
    mysql_real_escape_string($keyword)
            );
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony,

    I will try this and let you know the out put.

    Thanks a lot.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    IN
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    This will build the SQL query for you with an unlimited amount of keywords.
    PHP Code:
    <?php
    $sql 
    'SELECT * FROM category WHERE 1 <> 1';
    foreach(
    preg_split('/([\s&-]+)/'$_POST['keywords']) as $keyword){
        if(
    strlen($keyword)){
            
    $sql .= sprintf(
                
    " OR category LIKE '%%%s%%'",
                
    mysql_real_escape_string($keyword)
            );
        }
    }
    ?>
    I am looking for the relevant keywords with the LIMIT of 50 only.

    I've put an example above.....so please let me know there any way to get the relevant results...as this one is not good for me.

    Thanks for your help though.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    To add a limit, you simply add, well, a limit.

    PHP Code:
    <?php
    $sql 
    'SELECT * FROM category WHERE 1 <> 1';
    foreach(
    preg_split('/([\s&-]+)/'$_POST['keywords']) as $keyword){
        if(
    strlen($keyword)){
            
    $sql .= sprintf(
                
    " OR category LIKE '%%%s%%'",
                
    mysql_real_escape_string($keyword)
            );
        }
    }
    $sql .= ' LIMIT 50';
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •