Jump to content

Need help - php


SpazDes

Recommended Posts

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.