gotornot Posted October 15, 2009 Share Posted October 15, 2009 i have created a shopping cart and i wanted to add a mass add script. I will use a standard form to post the data over but what comands do i use to take that data and split it up ready for insertion into the database. I will be copy and pasting from a excel spreadsheet so im assuming its tsv. Any help would be great. Link to comment https://forums.phpfreaks.com/topic/177773-help-split-up-data/ Share on other sites More sharing options...
gotornot Posted October 15, 2009 Author Share Posted October 15, 2009 I am looking at the explode function however how owuld that work in the scenario below: Block A - Block B - Block C Bob - Smith - Stinks Fred - Jones - Clear Is there any limit to blocks and rows and how do i start to seperate them and check them. Link to comment https://forums.phpfreaks.com/topic/177773-help-split-up-data/#findComment-937395 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 Get the contents of the file into a variable ($file), explode the $file variable based on the newline character. Then loop through it exploding each row on the tab character. $file = $_POST['info']; $lines = explode("\n", $file); // note you might have to do \r\n else you'll end up with the dummy character in teh last item of the row foreach($lines as $line) { $blocks = explode("\t", $line); // insert $blocks[0], $blocks[1] into the database as approritate. } Link to comment https://forums.phpfreaks.com/topic/177773-help-split-up-data/#findComment-937399 Share on other sites More sharing options...
gotornot Posted October 15, 2009 Author Share Posted October 15, 2009 ive outputed the $blocks variable and all i get is ArrayArrayArrayArrayArrayArray i only put a few items in Link to comment https://forums.phpfreaks.com/topic/177773-help-split-up-data/#findComment-937410 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 If you echo $blocks it will say array because $blocks is essentially equal to array ("Block A", "Block B", "Block C"); etc... foreach($lines as $line) { $blocks = explode("\t", $line); echo '<pre>'; print_r($blocks); echo '</pre>'; } As it is essentially a table I would generally use the variable $columns rather than blocks as it makes a better explanation of what is actually contained there. Giving it a direct comparison to excel. Link to comment https://forums.phpfreaks.com/topic/177773-help-split-up-data/#findComment-937413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.