Jump to content

2 part question


nimzie

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

OriginScreen.jpg

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

<?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 :)

Link to comment
Share on other sites

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>
?> 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.