By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Separating search terms with commas and passing variables in the URL

Ajm113
100+
P: 161
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*
Jun 19 '07 #1
Share this Question
Share on Google+
15 Replies


epots9
Expert 100+
P: 1,351
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="nextpage.php" method="get"></form>[/HTML]
Jun 19 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Ajm113.

Have a look at explode().
Jun 19 '07 #3

Ajm113
100+
P: 161
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]
Jun 19 '07 #4

pbmods
Expert 5K+
P: 5,821
Changed thread title to better describe the problem.

Heya, 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.
Jun 19 '07 #5

Ajm113
100+
P: 161
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.
Jun 19 '07 #6

pbmods
Expert 5K+
P: 5,821
Heya, 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.]
Jun 19 '07 #7

Ajm113
100+
P: 161
I think I do have that. But I know this mite seem odd, but what do they mean by "yoursql"? Database perhaps?
Jun 19 '07 #8

P: 2
opps i answered this beofre I saw it was already answerd
Jun 19 '07 #9

pbmods
Expert 5K+
P: 5,821
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:

Expand|Select|Wrap|Line Numbers
  1. mysqld --version
  2.  
Jun 19 '07 #10

Ajm113
100+
P: 161
I have 4.1.10 of mysql :(. Are their any alternatives?
Jun 19 '07 #11

pbmods
Expert 5K+
P: 5,821
Heya, 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:

Expand|Select|Wrap|Line Numbers
  1. mysql_query("SELECT * FROM `table` WHERE `field` LIKE '%" . implode("%' AND `field` LIKE '%", $matches) . "%'");
  2.  
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?
Jun 19 '07 #12

Ajm113
100+
P: 161
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?
Jun 20 '07 #13

Ajm113
100+
P: 161
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]
Jun 20 '07 #14

pbmods
Expert 5K+
P: 5,821
Heya, Ajm113.

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

Here's what implode() does:

Expand|Select|Wrap|Line Numbers
  1. $array = array('one', 'two', 'three');
  2. echo "`field` LIKE '%" . implode("%' OR `field` LIKE '%", $array) . "%'";
  3. echo "\n";
  4. echo '--' . implode('-<>-', $array) . '--';
  5.  
Will output:
Expand|Select|Wrap|Line Numbers
  1. `field` LIKE '%one%' OR `field` LIKE '%two%' OR `field` LIKE '%three%'
  2. --one-<>-two-<>-three--
  3.  
The first parameter of implode() is what goes in between the pieces.
Jun 21 '07 #15

Ajm113
100+
P: 161
I got it to work with no errors, but it does not seem to do much. Do I have to make the $array work with the $row where it displays the results? I want to make sure... Becuase I don't know what I should link the $array to.
Jun 21 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.