Separating search terms with commas and passing variables in the URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajm113
    New Member
    • Jun 2007
    • 161

    Separating search terms with commas and passing variables in the URL

    Ok, I want to know. What do they call it when you want to separate words from spaces or ',' in the php script so I may enter in multiable items for that script to look up? Like a search engine perhaps.

    How do I have to so when a user submits a input field. That data would display on the address bar. So that person can save that link of that information that was entered.

    I forgot, but I have been looking around what do you call a person that knows much , but not to much in something? I just lost what that word was! *chuckle*
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Originally posted by Ajm113
    Ok, I want to know. What do they call it when you want to separate words from spaces or ',' in the php script so I may enter in multiable items for that script to look up? Like a search engine perhaps.

    How do I have to so when a user submits a input field. That data would display on the address bar. So that person can save that link of that information that was entered.

    I forgot, but I have been looking around what do you call a person that knows much , but not to much in something? I just lost what that word was! *chuckle*
    for your first question i think u're asking about "delimiters ".
    second, if u want the the input to appear in the address bar, u have to set the form method to get:
    [HTML]<form name="myForm" id="myForm" action="nextpag e.php" method="get"></form>[/HTML]

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Ajm113.

      Have a look at explode().

      Comment

      • Ajm113
        New Member
        • Jun 2007
        • 161

        #4
        I tried those and they work! But for the exception of explode. I tried this code though it just cuts off the text if their was a ',' after it and uses the text to the left on the side to search something.
        [PHP]
        $arr = explode(",", $search);
        foreach($arr AS $search)[/PHP]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Changed thread title to better describe the problem.

          Heya, Ajm113.

          Originally posted by Ajm113
          I tried those and they work! But for the exception of explode. I tried this code though it just cuts off the text if their was a ',' after it and uses the text to the left on the side to search something.
          Hrm. I'm not sure I understand. It sounds like explode() is working perfectly... but not doing what you want (dontcha love it when that happens?). Give us an example.

          Comment

          • Ajm113
            New Member
            • Jun 2007
            • 161

            #6
            Sorry I meant right side. Lets say you want to look for anything like "PHP" then you want to look for anything that has the word "forum". Then the user can enter in "PHP, forum". With a space or ',' after that word. Mysql should pull up anything with those two words.

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Ajm113.

              Originally posted by Ajm113
              Sorry I meant right side. Lets say you want to look for anything like "PHP" then you want to look for anything that has the word "forum". Then the user can enter in "PHP, forum". With a space or ',' after that word. Mysql should pull up anything with those two words.
              Ah.

              You might be interested in using a MySQL boolean fulltext search instead.
              [EDIT: But only if you have MySQL 5.]

              Comment

              • Ajm113
                New Member
                • Jun 2007
                • 161

                #8
                I think I do have that. But I know this mite seem odd, but what do they mean by "yoursql"? Database perhaps?

                Comment

                • willyp
                  New Member
                  • Jun 2007
                  • 2

                  #9
                  opps i answered this beofre I saw it was already answerd

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Originally posted by Ajm113
                    I think I do have that.
                    phpMyAdmin will show your MySQL version info after you log in. You can also log into the MySQL client via CLI (e.g., `mysql -u username -p database`), and it will tell you what version of MySQL Server you have. Alternatively, you can just issue this command in the shell:

                    [code=sh]
                    mysqld --version
                    [/code]

                    Comment

                    • Ajm113
                      New Member
                      • Jun 2007
                      • 161

                      #11
                      I have 4.1.10 of mysql :(. Are their any alternatives?

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, Ajm113.

                        Originally posted by Ajm113
                        I have 4.1.10 of mysql :(. Are their any alternatives?
                        So what you're doing right now is splitting the search string by ','s and then running a query:

                        [code=php]
                        mysql_query("SE LECT * FROM `table` WHERE `field` LIKE '%" . implode("%' AND `field` LIKE '%", $matches) . "%'");
                        [/code]

                        Where $matches is the processed list of search words. Is this correct?

                        For pre-5 MySQL, that's probably the best way to do it. And if it's not the best, well by golly it's the easiest, so there.

                        Anyway, I'm curious as to what was causing explode() not to give you expected results. Can you provide an example of a string that was misbehaving?

                        Comment

                        • Ajm113
                          New Member
                          • Jun 2007
                          • 161

                          #13
                          It was cutting off anything that had a ',' in it from the left so it would only search the left side of the input field. I'm just wondering since my Mysql knowledge is not that stapled. Do I add the implode after each search of field?

                          Comment

                          • Ajm113
                            New Member
                            • Jun 2007
                            • 161

                            #14
                            Iv been playing around with the code, but I can't figure out where the error is:
                            [PHP]"SELECT * FROM sites WHERE type='$type' '%" . implode("%' AND url LIKE '%".$search." %' or description LIKE '%".$search." %' or title LIKE '%".$search." %' ORDER by RAND() '%", $matches) . "%'";[/PHP]

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Ajm113.

                              Ah. To search 3 fields, you'll have to use implode() 3 times.

                              Here's what implode() does:

                              [code=php]
                              $array = array('one', 'two', 'three');
                              echo "`field` LIKE '%" . implode("%' OR `field` LIKE '%", $array) . "%'";
                              echo "\n";
                              echo '--' . implode('-<>-', $array) . '--';
                              [/code]

                              Will output:
                              Code:
                              `field` LIKE '%one%' OR `field` LIKE '%two%' OR `field` LIKE '%three%'
                              --one-<>-two-<>-three--
                              The first parameter of implode() is what goes in between the pieces.

                              Comment

                              Working...