Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-11-2010, 05:15 PM   PM User | #1
stfc_boy
Regular Coder

 
Join Date: Jun 2007
Posts: 310
Thanks: 86
Thanked 3 Times in 3 Posts
stfc_boy is an unknown quantity at this point
Help reading .txt files and parsing to DB?

Hi,

I'm looping for some help. I've written a script which looks into a folder for .txt files (below)







...and prints the contents of what's in each file to the screen like so:

View Screen

Now what I want to do is take these details and for each file insert them row-by-row in the database like so:



But i'm not sure how to do this part of it where it inserts these details in the database. The rest of it works great. Can anyone kindly help and help me with this code?

Thanks

Chris

PHP Code:
<?php 

/* Define and Open the Directory */
$path    '/home/******/parsers/******/write/';
$dir    opendir($path);

/* Loop through the directory using a while loop if $dir exists */
while (false !== ($file readdir($dir))) {

//Read the files in the directory: one.txt, two.txt, three.txt
$the_text file_get_contents($path.$file);

  
//Now need to insert each new line in the database but not sure how???
  
$SQL "INSERT INTO test_table (phrase,phrase2,phrase3) VALUES ('$line_one','$line_two','$line_three')";
  
$QUE mysql_query($SQL);
  
echo 
'<pre>'print_r($the_text); echo '</pre>';
  
}

?>
stfc_boy is offline   Reply With Quote
Old 03-11-2010, 05:23 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Looks correct to me, you'll need to take care of some sanitation though. The problem is that $line_one, $line_two, $line_three do not exist, so we'll create them using list in conjunction with an explode:
PHP Code:
<?php  

if (function_exists('set_magic_quotes_runtime'))
{
    
set_magic_quotes_runtime(0);
}

/* Define and Open the Directory */ 
$path    '/home/******/parsers/******/write/'
$dir    opendir($path); 


/* Loop through the directory using a while loop if $dir exists */ 
while (false !== ($file readdir($dir))) { 

    
//Read the files in the directory: one.txt, two.txt, three.txt 
    
$the_text file_get_contents($path.$file); 
    list(
$line_one$line_two$line_three) = explode(PHP_EOL$the_text);
    
//Now need to insert each new line in the database but not sure how??? 
    
$SQL "INSERT INTO test_table (phrase,phrase2,phrase3) VALUES ('$line_one','$line_two','$line_three')"
    
$QUE mysql_query($SQL); 
   
    echo 
'<pre>'print_r($the_text); echo '</pre>';    
}
Try that.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
stfc_boy (03-11-2010)
Old 03-11-2010, 05:36 PM   PM User | #3
stfc_boy
Regular Coder

 
Join Date: Jun 2007
Posts: 310
Thanks: 86
Thanked 3 Times in 3 Posts
stfc_boy is an unknown quantity at this point
Awesome - looks good to me - thanks. Out of interest any other way you could do it rather than using PHP_EOL? Just thinking for example if I wanted to grab the second line only of a set line number of each text file?

Last edited by stfc_boy; 03-11-2010 at 05:40 PM..
stfc_boy is offline   Reply With Quote
Old 03-11-2010, 05:42 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP_EOL is just the linebreak in PHP. That will capture each line into its own variable.
If you instead use the file() command over the file_get_contents, it will automatically parse it into an array and the second line will always be $results[1]. This is fine if you're file size is always going to be small; I wouldn't recommend using file with large amounts of data as you'll find that it will quickly exhaust the memory available for you to use.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:26 PM.


Advertisement
Log in to turn off these ads.