SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    comma separated values

    Hi,
    in my db I have fields
    PHP Code:
    valuesurlkeywords 
    and keywords field have comma separated values entered by user.
    In my live search script only first value from this keyword field is considered as a value.
    here is the code
    PHP Code:
    $query $db->query("SELECT value, url, keywords FROM my_DB WHERE keywords LIKE '$queryString%' LIMIT 10");
    if(
    $query) {
    while (
    $result $query ->fetch_object()) {
    $strUTF iconv('cp1250','UTF-8',$result->value); 
    echo 
    '<a  class="bodyText" href="'.$result->url.'"><li onClick="fill(\''.$strUTF.'\');">'.$strUTF.'</a></font></li>'
    }

    how to iterate trough each keword field to be considered by script not just first one.

    Regards,

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Store each keyword in its own row in a separate table. Database fields should always be atomic (single valued).

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    PHP Code:
    $sql "SELECT value, url, keywords FROM my_DB WHERE (keywords LIKE '$queryString%' ) OR (value LIKE  '$queryString%') OR ( url LIKE  '$queryString%' )  LIMIT 10"

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put &#37; in both side of your keyword:
    Code mysql:
    SELECT value, url, keywords FROM my_DB WHERE keywords LIKE '%keyword%' LIMIT 10

    Edit:
    Or you can do something like this to search more words in the searched keyword:
    PHP Code:
    $keywords "test test1 test2 test3";
    $words explode(" "$keywords);
    $sql "SELECT value, url, keywords FROM my_DB WHERE 1 ";
    if(
    count($words) >= 1){
        
    $sql $sql " AND (keywords LIKE '%" implode("%' OR keywords LIKE '%"$words) . "%')";
    }
    echo 
    $sql
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your posts and your time.
    I used the rajug advice and put % sign in both side of my $keyword var. Everything work just fine.

    regards

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Store each keyword in its own row in a separate table. Database fields should always be atomic (single valued).
    This would be a better way to do it.


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
  •