SpazDes Posted September 10, 2008 Share Posted September 10, 2008 I need some help I am trying to build a script that takes multiple keywords and explodes them then compares them to whats in a database and if the keyword exists just update the table adding a number to a field, but if the keyword doesn't exist then add the keyword and add the number the the field. This is a basic idea I suppose of what I want to do it "sort" of works. <?php $dbHost = 'localhost'; $dbUser = 'spaztast'; $dbPass = 'jungle11688'; $dbName = 'spaztast_spazweb'; mysql_connect($dbHost,$dbUser,$dbPass); mysql_select_db($dbName); $var = "book programming"; $var2 = explode(" ", $var); $result = mysql_query("SELECT * from tbl_keyword"); $i = 0; while($r = mysql_fetch_array($result)) { if($var2[$i] == $r['k_word']) { echo "Key Exist's"; } else{ echo $r['k_word'] . "<br>"; } $i = $i++; } ?> Now heres the thing the table looks like this: +---------+-------------+-------+ | word_id | k_word | pd_id | +---------+-------------+-------+ | 1 | palm | 11 | | 2 | programming | 17 | | 3 | C# | 18 | | 4 | book | 18 | +---------+-------------+-------+ This is the output when the script is run: palm programming C# Key Exist's I guess what I am asking is, how I loop one exploded value against all the keys in the database? I think thats what I want too do just unsure of how to do it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 10, 2008 Share Posted September 10, 2008 You just need a single query to accomplish all of that! Assuming word_count is the field you want updated by adding a number: the following script will attempt to insert all of the values intot he database, but if it finds a duplicate it will instead update the count field by 1. $var = "book programming"; $values = '(' . implode('), (', explode(' ', $var)) . ')'; $query = "INSERT INTO table (k_word) VALUES $values ON DUPLICATE KEY UPDATE word_count=word_count+1"; Quote Link to comment Share on other sites More sharing options...
Maq Posted September 10, 2008 Share Posted September 10, 2008 I need some help I am trying to build a script that takes multiple keywords and explodes them How do you want to explode these keywords (commas, spaces, etc.)? I'm assuming spaces cause that's what you have in your example. All you do is take the $var2 and foreach through it. Example: foreach ($var2 as $value) { //Search through table for '$value' } Hope this helps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.