Jump to content

Help split up data


gotornot

Recommended Posts

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

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

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

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

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.