Page 1 of 1

Help with a PHP function to filter Keywords

Posted: Thu May 06, 2010 5:18 am
by pedroteixeira07
Hi there, I'm having to big issues to achieve my objective.

1st Issue:

I have an external source that allows a user to insert 0 or more keywords which are posted to a PHP file with this names: keyword0, keyword1, keyword2, etc... How do I get this files in PHP ?
I know if it was just one keyword I could retrieve it like this:

Code: Select all

$keyword=$_POST[keyword]
But in this case it can be 0, just 1 or "infinite" keywords. Can you help me please?

2nd Issue:

Once I have this keyword values ''stored'' in php variables e.g. ($keyword0, $keyword1, etc) I need to make a query to my DB with this multiple possible keywords. I already have an N-to-N relationship set. It's something like this:

Picture
P_ID Pic
1 LA.jpg
2 NY.jpg
3 MI.jpg
4 SE.jpg
5 DA.jpg

Keywords
K_ID Keywords
1 beach
2 sun
3 cold
4 hot
5 sea

PicKey
PK_ID P_ID K_ID
1 1 1
2 1 2
3 1 4
4 1 5
5 2 3
5 2 5

I know for example for one keyword it would be something like :

Code: Select all

mysql_query("SELECT P.Pic FROM Picture P, Keywords K, PicKey PK WHERE P.P_ID=PK.P_ID AND K.K_ID=PK.K_ID AND K.Keyword="beach" 
But how about for multiple keywords ? Which can be one or more depending on the number of keywords posted by the user.

I would really appreciate some help with this. Everything is blury for me know. I don't have enough knowledge to complete this.
Thank you very much

Re: Help with a PHP function to filter Keywords

Posted: Thu May 06, 2010 11:03 am
by AbraCadaver
Depends upon how the user inputs the keywords. For example with text inputs you can build an array:

Code: Select all

<input type="text" name="keyword[]">
<input type="text" name="keyword[]">
//etc
Then on the page that receives the form data:

Code: Select all

foreach($_POST['keyword'] as $word) {
   if(!empty($word)) {
      // check to see if word is in Keywords table and if so get the id, if not then insert it and get the new id
      // insert the P_ID and K_ID into PicKey
   }
}
Once you have your head around this we can look at the select.