Languages :: PHP :: reading info into table |
|||
| By: roe1and |
Date: 05/02/2008 11:06:13 |
Points: 20 | Status: Answered Quality : Excellent |
|
i've been creating a lot of tables that get their content from uploaded textfiles. typically a textfile would look like this: value1¬value2¬value3¬value4 value5¬value6¬value7¬value8 this works quite well because you can insert it line for line and explode the information at the ¬ and then you have your values to insert into fielda, fieldb, fieldc, fieldd. that is quite straight forward. at the moment i'm looking at text files that look like this: 01 infromation 02 information 03 information 04 information 05 information 01 information 03 information 04 information 01 information 02 information 03 information i would only be interested in the bits that start with 01 and 03 and would like to insert them into 2 fields in a table. at the moment i'm trying to load these into an array and then stick them into a query everytime the 03 comes up? any suggestions |
|||
| By: VGR | Date: 07/02/2008 08:44:20 | Type : Answer |
|
| idea : $fp=fopen('toto.csv','r'); // I voluntarily skip the required tests on returned values $complete=FALSE; while ($ch=fgets($fp)) do { $z=explode($ch,' '); // TAB character = #9 if ( ($z[0]=='01') or ($z[0]=='03') ) { // keep the line if ($complete) { // this means the OTHER element has already been found! // get second element if ($z[0]=='01') $val01=$z[1]; else $val03=$z[1]; // write to DB the memorized values $val01 and $val03 $complete=FALSE; } else { // so this is the FIRST element we need // get first element if ($z[0]=='01') $val01=$z[1]; else $val03=$z[1]; } } // else NOP, skip } fclose($fp); // done. not tested but should work |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








