Jump to content

Lumikko

New Members
  • Posts

    4
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Lumikko's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hello Here is the problem: I have this example string: $string = "Product 1, Product 2, Product 3, Product 4, Product 5, Product 6, Product 7, Product 8, Product 9, Product 10, Product 11, Product 12, Product 13"; Now i wan't to check if string has multiple same names (or numbers) and to be cleaned using array_unique function. First, those products will be stripped to "tags" with this function: function split_tags($string) { $array = preg_split("/[\s]*[, ][\s]*/", $string); while(list($key,$val)=each($array)){ if($val<>" " and strlen($val) > 0){ $tag .= "$val, "; } } $tag=substr($tag,0,(strLen($tag)-2)); return $tag; } So the $string will looks like this: Then this function should do the "magic": function unique_tags($array) { $array = implode(',',array_unique(explode(',', $array))); return $array; Here is desired output: But my script give's this output: So it almost works. I also tryed to change $string to this (see: , before Product 1): [code]$string = ", Product 1, Product 2, Product 3, Product 4, Product 5, Product 6, Product 7, Product 8, Product 9, Product 10, Product 11, Product 12, Product 13"; [/code] But it don't do anything.
  2. Sorry for the douple post, but i got it working. Reason why it did not work was that mysql_fetch_array works only one time so i put it while loop.
  3. 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: Number 5 (which is bolded) is database id number for Product 1.
  4. 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: Now is tricky part (at least for me ): 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: 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 :-\
×
×
  • 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.