Jump to content

Problem with arrays


Lumikko

Recommended Posts

Hello

 

I have coded many years but this one is killing me  :-[

 

I have table and it looks like this:

table name: products
id | name

 

There is an form which are used to select prodcuts (with jquery autocomplete). I used comma (,) to separate these prodcuts, example:

Prodcut 1, Prodcut 2, Product 3, Product 4

 

Now is tricky part (at least for me  8)):

I need to function to check database that there is same product name's that user has given in a form and return's id and if there is unknown product, then it will be added to database.

 

I have used this code to separate form's input to array:

function split_product($string) {
$string = preg_split("/[\s]*[,][\s]*/", $string);
return $string;
}

 

Array looks like this:

Array ( [0] => Product 1 [1] => Product 2 [2] => Product 3 [3] => Product 4 )

 

I have an idea but i still don't get it.

 

Here is my function that i have tryed:

function check_product($array) {

foreach($array as $data) {
                // Check is there same product name
	$check = mysql_num_rows(mysql_query("SELECT name FROM products WHERE name = '$data'"));
	if($check == "1") {
		$checkid = mysql_fetch_array(mysql_query("SELECT id FROM prodcuts WHERE name ='$data'"));
		$id = $checkid['id'];
		return $id;
	}
	else {
		mysql_query("INSERT INTO products VALUES ('', '$data')");
	}
}
}

 

Of course, code don't work  :-\

Link to comment
Share on other sites

Firstly, use explode to create your array. Secondly, why are you executing two identical queries?

 

Third and most importantly, what debugging have you done? Simply telling us your code doesn't work doesn't help.

 

Thanks for the tip. I now use explode to split string to array.

 

I did use two almost same identical queries because.. I don't know :) I just trying to make it work and then clean it up :)

 

I use this to give output something:

<?php
error_reporting(-1);
$products = "Prodcut 1, Prodcut 2, Product 3, Product 4, Product 5";
$string = split_product($products);
print_r($string);
echo "<br />";
$string = check_product($string);
print_r($string);

 

Output is:

Array ( [0] => Prodcut 1 [1] => Prodcut 2 [2] => Product 3 [3] => Product 4 [4] => Product 5 )

5

 

Number 5 (which is bolded) is database id number for Product 1.

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.