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 :)

 

 

Link to comment
Share on other sites

$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}");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.