nimzie Posted October 31, 2007 Share Posted October 31, 2007 1st is this : When I process my file after simply doing : $arr = explode( "\t" , $line); then looping through things inserting in to my DB - it works fine. when I do this: $delim = "\t"; /*actually loaded from a database is just \t in the db ??this could be a problem?? */ $arr = explode( $delim , $line); It does not work. I get an error : Column count doesn't match value count at row 1 as I parse the file using this statement: $sql = "insert into origin ( StoreID, OrderID, OrderNumber, OrderDate ) values (' " . implode ( "','" , $arr) . "')"; which works file when I do not put a var in there. I'm sure this is a pretty simple thing... Part 2 - where do you ask non oop, non math php based questions like this? The room structure isn't clear to me. Thanks for the help. Adam Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted October 31, 2007 Share Posted October 31, 2007 You would ask this question in the general PHP forum...this is a child board of that forum. After you loaded the value of $delim from the DB, did you try printing that variable out to see what it gives you? Maybe it isn't what you expected. Also, for your array, see what it holds by doing this print_r($arr); See if those are the values you expect as well. Quote Link to comment Share on other sites More sharing options...
nimzie Posted October 31, 2007 Author Share Posted October 31, 2007 when I show delim (echo to the screen) , the value is \t (not "\t"). Do I need to store "\t" in the DB? That's originally where this comes from.. I give the user choices of what delims to use. The rest of things are fine - it's just not parsing the file properly with the var instead of "\t" hard coded in there. Should this work as I have coded it? Thanks for the help Here is a screenshot of some of my output code... Input delimiter is the variable that is being echoed here and being used. echo "Input delimiter is set to " . $delim . "<br />"; I think the way I am using this variable (syntax) is what is going wrong. It won't parse $arr properly with explode cause the $delim is not being interpreted the same as "\t" is when it's hardcoded. Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 anyone got any pointers for this one? Thanks! Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 I'm still looking at this issue with no luck. Could this have sometihng to do with me storing \t in the db and using it like this in the explode function, it may need something around it to cause pHP may be escaping? Someone mentioned this to me but I don't quite understand what he is saying. Thanks Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 1, 2007 Share Posted November 1, 2007 Could you post your actual code? I think it does have something to do with double quotes vs single quotes. I have been playing around with it on my server. Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 <?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition /* if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } */ //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br />"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { die ( "Sorry your file was not uploaded<br />" ); } //If everything is ok we try to upload it else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file " . basename( $_FILES['uploadedfile']['name']). " has been uploaded<br />"; echo "About to parse file to Origin Capacity Database<br />"; require '../php/config.php'; require '../php/opendb.php'; require '../php/options_logic.php'; $delim = $CapacityDelimiterIn; if (isset($delim)) echo "Input delimiter is set to " . $delim . "<br />"; else die ("Input delimiter is not set"); if ($ok==1) { $fcontents = file ( $target ); //Begin iterating through the file records and inserting them in to the database for($i=1; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); //$arr = explode( $delim , $line); $arr = explode( "\t" , $line); #if your data is comma separated # instead of tab separated, # change the '\t' above to ',' in the options screen or database. $sql = "insert into origin ( StoreID, OrderID, OrderNumber ) values (' " . implode ( "','" , $arr) . "')"; mysql_query($sql); //echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br />"; $ok=0; } echo "."; } if($ok==0) die ( "Error parsing file to database - process aborted **MYSQL ERROR<br />" ); else { echo ( "<br />File parsed and imported to database<br /> $i records imported." ); } } } else die ( "Sorry, there was a problem uploading your file<br />" ); } //Begin Processing for output file. ?> <br /> <input type="submit" value="Make Export File" /> </form> </body> </html> That's the code for what I am doing. The $delim is set based on a variable initialized in '../php/options_logic.php'; as: <?php require 'config.php'; require 'opendb.php'; $sql = "select * from options"; $statement_back = mysql_query($sql); $statement = mysql_fetch_array ( $statement_back ); $FTPOriginAddress = $statement['FTPOriginAddress']; $FTPUserOrigin = $statement['FTPUserOrigin']; $CapacityDelimiterOut = $statement ['CapacityDelimiterOut']; $CapacityDelimiterIn = $statement ['CapacityDelimiterIn']; ?> the DB is currently storing \t - that is it. Thanks for your help Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 1, 2007 Share Posted November 1, 2007 Try changing this line $arr = explode( $delim , $line); To $arr = explode( "$delim" , $line); I don't know if that will make the difference or not. Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 I just tried wrapping in quotes. I also tried putting "\t" in the DB as such. I also tried storing \"\t\" in the DB. Neither worked. Someone's got to have used explode() before with a dynamically set delimiter before Quote Link to comment Share on other sites More sharing options...
revraz Posted November 1, 2007 Share Posted November 1, 2007 I found this code where the explode is in a Function, maybe you can try that approach: Example <?php function between($beg, $end, $str) { $a = explode($beg, $str, 2); $b = explode($end, $a[1]); return $beg . $b[0] . $end; } echo between('<a>', '</a>', 'fsdfsdfsd<a>fsdfsd<a><a></a>sdfsdfsdf') //<a>fsdfsd<a><a></a> ?> Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 I'm pretty much positive this has something to do with an escaped character \t being interpreted somehow by PHP. Putting it in a function didn't change my result - however about 2 mins before you posted that, I found the same example! Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 1, 2007 Share Posted November 1, 2007 Tell us what you are trying to accomplish (a bit of the larger picture). Maybe there's a better way to handle your need that doesn't include that delimiter (unless of course it's hard-coded into something you don't control). PhREEEk Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 1, 2007 Author Share Posted November 1, 2007 I am uploading and parsing files in to a database. The user should have an option to choose between the tab delimited version of the file or the comma delimited version. I'm hoping to make this tool relatively flexible so loading options from another table (options) is a must. From there, I parse the file right in to the database - it has to adhere to a file layout so it's their choice how they want to delimit the file. That part is all taken care of. In the end, I want to have my option screen to save which delimiter for which part of the import and export. That will be loaded from the DB and used in the explode (or whichever function may better suit things). As mentioned, this works perfectly using "\t" in explode. Ideally there is a way to have the value load properly from the DB - or some kind of workaround. Cheers Quote Link to comment Share on other sites More sharing options...
nimzie Posted November 2, 2007 Author Share Posted November 2, 2007 any tips? Quote Link to comment 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.