String as a URL variable - split to query recordset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vlinder
    New Member
    • Nov 2007
    • 7

    #1

    String as a URL variable - split to query recordset

    I'd like to pass a multi-word string as a variable to a page which then splits the string into seperate words. After this I want to create a recordset using each of the words in the query on the same field eg:

    the url would look like this:

    mypage1.php?sea rch=apple banana

    where fruit LIKE apple OR fruit LIKE banana (in this example)

    I've been bothering this for a while now and can't seem to get it to work. I'm using Dreamweaver 8

    Any help would be greatly appreciated.
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Nothing to do with dreamweaver here, You have to use a script like this. Check this out please:

    split_url_strin g.php
    [code=php]
    <?php
    $OriginalQuery = "SELECT col_names FROM table";
    $string = array();
    $where = "";
    if (isset($_GET['search']) AND !empty($_GET['search'])) {
    $URLString=expl ode(" ",$_GET['search']);
    foreach($URLStr ing as $key){
    $string[] = " fruit LIKE $key "; // Hope You Know the Logic use to "LIKE" :D
    }
    }
    if(!empty($stri ng)){
    $where = " WHERE ".implode(" OR", $string);
    }
    echo $OriginalQuery. $where;
    ?>
    [/code]

    Once you called to the page like in way of this format:
    http://localhost/tut/split_url_strin g.php?search=ba nana apple
    outputs:
    SELECT col_names FROM table WHERE fruit LIKE banana OR fruit LIKE apple

    Comment

    • Vlinder
      New Member
      • Nov 2007
      • 7

      #3
      Many thanks. Had to tinker around with it a bit but got it working in the end. Most importantly, I've learned a few things.

      Again, thank.

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by Vlinder
        Many thanks. Had to tinker around with it a bit but got it working in the end. Most importantly, I've learned a few things.

        Again, thank.
        Glad I could help ! post back to the forum any time.
        Good luck! :)

        Comment

        Working...