Forum Moderators: coopster

Message Too Old, No Replies

Need help - php comparing

Trying to compare explode array to database table

         

SpazDes

5:19 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



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.
Code:

<?php
$dbHost = 'localhost';
$dbUser = 'spaztast';
$dbPass = '-----------;
$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:

Code:

+---------+-------------+-------+
¦ 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 <-- Would have been "book".

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.

cameraman

6:41 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at array_intersect() [us.php.net].
You could read the keywords from the table into one array and get the intersection between that and your inputted array. Use the intersect array to do your updates. Then use the intersect array to unset the keys in the inputted array and use that resulting array to do the inserts.