Jnerocorp Posted March 29, 2011 Share Posted March 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/ Share on other sites More sharing options...
RussellReal Posted March 29, 2011 Share Posted March 29, 2011 $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}"); Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/#findComment-1193814 Share on other sites More sharing options...
techdude Posted March 29, 2011 Share Posted March 29, 2011 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. -- techdude Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/#findComment-1193827 Share on other sites More sharing options...
RussellReal Posted March 29, 2011 Share Posted March 29, 2011 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. -- 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 Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/#findComment-1193833 Share on other sites More sharing options...
techdude Posted March 29, 2011 Share Posted March 29, 2011 Whoops! Thanks for correcting my misunderstanding. I didn't know that browsers always use \n. Thanks for the clarification. Hope we have been helpful, Jnerocorp. Please post back here and let us know. -- techdude Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/#findComment-1193837 Share on other sites More sharing options...
Jnerocorp Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks so much for the help guys I knew the code would be simple I really need to get on the ball and start getting this stuff to stick in my head. Quote Link to comment https://forums.phpfreaks.com/topic/232089-help-with-explode-do-while-for-each-and-inserting-into-mysql/#findComment-1194333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.