SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    see above
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a do while loop for parsing a text file

    Hi~

    I am fairly certain that this can be done, from what i have read/been reading i am just not sure how.

    i have a text file with a list of zipcodes and cities for a project i am working on, i need to seperate the list for a drop down menu, my text file is formated nicely like so:

    Zip city
    60606 Chicago

    there are 1600 entries and there are 2 spaces between the zipcode and the name of the city, I need php to parse the text file and deposite each item into a table in my database for each entry, that way i can simply create a dynamic dropdown menu via a MySQL db.

    thanks

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $data file('filename.txt');
    $total count($data);

    $query 'INSERT INTO sometable (zip, city) VALUES ';

    for (
    $i 0$i $total$i++) {
       list(
    $zipcity) = explode("  "$data[$i]);
       
    $query "($zip, '$city'), ";
    }

    $query substr($query0, -1); // should remove the last ","

    mysql_query($query); 
    Or you could use something like phpmyadmin and load the file into the table.

  3. #3
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MySQL can load the file natively.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    see above
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, thanks for the quick reply, one thing i am wondering about, maybe its not really, but how will php know which is which from your script? how does it know where the zipcode ends and the city name begins?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    see above
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    phpMyAdmin is the bomb! i was able to just upload my text file to via phpMyAdmin. what a time saver!!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •