Jump to content

[SOLVED] Reading data from a text file into a database


VTS

Recommended Posts

The problem that I am having is finding a way to read data into a database from a textfile that is set up like this:

 

"1 23 23.90 10"

 

Each line begins and ends with the " mark.  I have read data from text files before, but they just had the "\n" at the end of the line.  What do I do when it has a " mark at the beginning and ending of each line?

 

Thanks in advance,

VTS

Do you, or do you not want the " in the data you enter?

 

If you do, you would probably want to do addslashes() on the line before you input, then stripslashes() when you pull it out.

 

If you don't want them in there I'd suggest using some substringing, a regex, or explode(). I myself would use explode('"',$line), but I'm not sure how well it handles the explode token if it's at the beginning and end of the line.

 

In case you can't tell, the first parameter I passed to explode there was single quote, double quote, single quote.

If you're going to use explode I would suggest this (assuming $line is the line of the file read in):

 

$line ="abcd"; //gibberish!

$line .= //read in file (I don't use files often enough to know function call)

$line .="dcba";

$linearr = explode('"',$line);

$line = $linearr[1];

 

That should give you the data inside the quotes.

Let me add one more thing.  Each line has 4 values in it.  In this line: "1 23 23.90 10"

1, 23, 23.90, and 10 are all seperate fields so I need to read them in seperately.  I tried this:

  $file = "c:\\data.txt";
  if(!file_exists($file)){
   echo "File does not exist";
  }
  $handle = fopen($file, "r");
  $contents = fread($handle, filesize($file));
  fclose($handle);
  $row = explode("\n", $contents);
  $rows = count($row);

  $i = 0;
  while($i < $rows-1){
   $parts = explode('"', $row[$i]);
   unset($numbers);
   $numbers = array();
   foreach($parts as $part) {
    $part = trim($part);
    if(is_numeric($part)) {
 array_push($numbers, $part);
}
   }
   $store = array_pop($numbers);
   $a = array_pop($numbers);
   $b = array_pop($numbers);
   $c = array_pop($numbers);
   echo "</br> $store  $a  $b  $c </br>";
  $i++;
  }

 

but it doesnt seem to work.  I am just using an echo statement so I can see that it is working properly before I try inserting any data into the database.  I probably just overlooked something stupid like I usually do.  Any ideas on why its not working?

You think you feel bad? How's about the guy who's sitting here being baffled trying to help you? :P

 

Then again, I'm at work, what else would I be doing? ::)

 

I've got this feeling like $rows contains 0, but I don't know why. Try echoing $rows to see what it prints out.

Replace your similar lines with this (make sure to backup your stuff first):

 

$i = 0;

while($i < $rows-1){

$line = "abcd".$row[$i]."dcba";

$parts = explode('"', $line);

unset($numbers);

$numbers = array();

$part = trim($parts[1]);

if(is_numeric($part)) {

array_push($numbers, $part);

}

 

 

See if that works.

I just found out what it is doing....instead of having one field in $part, it has the whole line.  I guess I need to go a little further with my exploding now and use it to seperate the fields. When I used echo on $part right after I used trim() on it, I get a whole line in it.  I am about to go and try this now and see how it works.

Ok, I figured it out  :D.  Here is what I used:

  set_time_limit(999);
  $file = "c:\\data.txt";
  if(!file_exists($file)){
   echo "File does not exist";
  }
  $handle = fopen($file, "r");
  $contents = fread($handle, filesize($file));
  fclose($handle);
  $row = explode("\r", $contents);
  $rows = count($row);
  $i = 0;
  while($i < $rows-1){
   $parts = explode('"', $row[$i]);
   unset($numbers);
   $numbers = array();
   foreach($parts as $part) {
    $part = trim($part);
echo "</br>trimed part = $part";
$x = 0;
$partA = explode(" ", $part);  //new lines added here to store each field seperately
foreach($partA as $var){
 if(is_numeric($var)) {
  echo "</br> *var = $var";
  array_push($numbers, $var);
  }
 $x++;
    }
   }
   $store = array_pop($numbers);
   $a = array_pop($numbers);
   $b = array_pop($numbers);
   $c = array_pop($numbers);
   echo "</br> store = $store  || a= $a  || b= $b  || c= $c </br>";
   $i++;
  }

 

Thanks for the help Balmung!

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.