Web Analytics Made Easy -
StatCounter Read multiple text files to update corresponding !URGENT! - CodingForum

Announcement

Collapse
No announcement yet.

Read multiple text files to update corresponding !URGENT!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read multiple text files to update corresponding !URGENT!

    I have a products/inventory MySQL table populated with item numbers, weights, images, etc. Is what I don't have is product descriptions. My product descriptions are all in seperate text (.txt) files in multiple directories. Each product's description is in its own text file named, for example, itemNumber.txt or 16000.txt. I need a PHP loop that will perform the following tasks (I think):

    1. read all the file names from the directory, parse the item numbers from the names, and put them into an array

    2. read the contents of each file into an array associated with the item number or file name

    3. update the description field in the database with the corresponding (itemNumber) description from each text file.

    The entire contents of the text file will go into the description field so no 'line by line' parsing is neccasary.

    Basically I need to make the following poor example recursive as I have a lot of descriptions:

    $txtFile[16000] = fopen '/products/descriptions/16000.txt';
    $description = fread($txtFile[16000]);
    $sql = 'UPDATE inventory SET description = ' . $description . ' WHERE itemNumber = ' . 16000; // I don't know how to come up with the itemNumber from file name here???

    The above is ONLY an attempt to illustrate what I am trying to do. Also, the itemNumber needs to be parsed from the txt file's name so it can tell the query which record it belongs to.

    Given enough time I could research, revisit, and learn how to accomplish this but I have NO time and need this today.

    Thank you very much to anyone who can be of help!

    PHP Version: 4.4.4
    MySQL Version: 4.1.21-standard
    Apache version: 1.3.37 (Linux)
    God helps those who help others.

  • #2
    How many different directories are the .txt files in? The reason I ask is that since this is a one time operation, if the directories are few, manually copying the .txt files into one location would simplify the code needed to do this.

    Edit: Never mind the above, here is code which will recursively get a list of all the paths/files into an array, the code to read and update the database would be straight forward -
    PHP Code:
    <?php
    // step one - get all of the folders into an array
    // step two use the array of folders to get a list of files into an array

    // the following works using the glob(...) function (PHP 4 >= 4.3.0, PHP 5)

    function globfolder($path){
        
    $current =glob("$path/*"GLOB_ONLYDIR); // get a list of folders in the current folder
        
    foreach($current as $value){
            
    $current array_merge($current,globfolder($value));
        }
        return 
    $current;
    }
     
    $start "."// where do we start - current folder

    $folders = array(); // array to hold listing of folders

    $folders globfolder($start); // get a list of recursive folders

    $file_match "*.txt"// the files to search for

    $files = array(); // array to hold listing of files

    // loop through the folders and merge matching file names into $files
    // echo out the folders and files found
    echo "<pre>";
    foreach(
    $folders as $value){
        echo 
    "Folder: $value<br />"// display the folder names
        
    $current glob("$value/$file_match"); // get the matching file names
        
    $files array_merge($files$current); // merge file names

        
    foreach($current as $list){
            echo 
    "    Files: $list<br />";    // display the file names
        
    }
    }
    echo 
    "</pre>";


    foreach(
    $files as $value){
    // put your code to process each file here...
    }
    ?>
    Last edited by CFMaBiSmAd; Oct 11, 2006, 08:00 PM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      Absolutely perfect! Thank you very much!

      I added the following code to your foreach loop and it worked like a charm:

      foreach($files as $value){
      // put your code to process each file here...

      $handle = fopen("$value", "r"); // open file

      $description = fread($handle, filesize($value)); // read file

      $itemNum = substr("$value", 8, 5); // strip $value from this: ./child/10000.txt to this: 10000

      mysql_query("UPDATE inventory SET description = '" . $description . "' WHERE itemNumber = " . $itemNum);

      fclose($handle); // close file

      }

      Thanks again
      God helps those who help others.

      Comment

      Working...
      X