Jump to content

help with (explode, do while, for each, and inserting into mysql)


Jnerocorp

Recommended Posts

Hello I have a text list that looks like this:

 

ACACIA-Acacia spp.-Australia
AMANITAS-Amanita muscaria-Siberia
AYAHUASCA-Yage-Amazon Basin
... continues...

 

i am trying to create a script that if i placed that text like that in a textarea and submitted the form it would explode each line one at a time by the "-" and add it into a mysql database (COMMON_NAME, GENUS_SPECIES, NATIVE_TO)

 

I dont want to have to submit these one at a time and im sure this is very simple to do just im not understanding how to explode each line.

 

maybe a code like;

 

1. counts the total number of lines and places it in a variable called lines

2. do while $lines > 0

3. for each $line explode by "-"

4. insert each piece of the exploded line into the database as  (COMMON_NAME, GENUS_SPECIES, NATIVE_TO) as piece 0,1, & 2

5. after inserted subtract 1 from $lines

 

or something similar to that effect.

 

any help is greatly appreciated1 :)

 

 

$x = explode("\n",trim($_POST['text_field']));
foreach ($x as $line) {
  $y = explode("-",$line);
  mysql_query("INSERT INTO `table` (common_name,genus_name,native_to) VALUES('{$y[0]}','{$y[1]}','{$y[2]}');");
}

 

however this could also be done with only ONE insert query.. but it would be handled something like this:

 

$x = explode("\n",trim($_POST['text_field']));
foreach ($x as $key => $line) {
  $vals = ($y = explode("-",$line));
  foreach ($y as $key => $value) {
    $vals[$key] = "'{$value}'";
  }
  $x[$key] = '('.implode(",",$vals).')';
}
$x = implode(',',$x);
mysql_query("INSERT INTO `table` (common_name,genus_name,native_to) VALUES{$x}");

Very close, RussellReal, but that doesn't take care of the multiple possible line endings line \r\n, \r, AND \n. Here is my whack at the code.

 

$values = preg_split('/(\r\n|\r|\n)/', $_POST['text_area']); //this gets an array where each item on its own line is an element in the array

$query = "INSERT INTO  `database_name`.`table_name` (`value1` ,`value2` ,`value3`) VALUES "; //prepare the first part of the query with the fields

foreach($values as $index=>$value){
          $value = explode("-", $value); //make the array

          $query .= "('{$value[0]}',  '{$value[1]}',  '{$value[2]}'),"; //add that item
}

$query = rtrim($query, ","); //remove the trailing comma

// Now add your own code to execute the query.

:D

--

techdude

Very close, RussellReal, but that doesn't take care of the multiple possible line endings line \r\n, \r, AND \n. Here is my whack at the code.

 

$values = preg_split('/(\r\n|\r|\n)/', $_POST['text_area']); //this gets an array where each item on its own line is an element in the array

$query = "INSERT INTO  `database_name`.`table_name` (`value1` ,`value2` ,`value3`) VALUES "; //prepare the first part of the query with the fields

foreach($values as $index=>$value){
          $value = explode("-", $value); //make the array

          $query .= "('{$value[0]}',  '{$value[1]}',  '{$value[2]}'),"; //add that item
}

$query = rtrim($query, ","); //remove the trailing comma

// Now add your own code to execute the query.

:D

--

techdude

 

we are not talking about a text file, we're talking about a text input.. its always just an \n

 

and either way.. \r is just a carriage return \r\n is not a different way of a new line.. if anything even if there happened to be a carriage return found.. a simple trim() would do fine when you add my lines back to the main array..

 

so gf <3

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.