SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast cwb's Avatar
    Join Date
    May 2004
    Location
    Michigan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ignoring First 6 Lines

    I am trying to create script that will import data into my database from a URL. It is working but I need it to ignore the first 6 lines. I am not sure how to do that so if anyone could help I would appreciate it...

    Here is what I got so far.
    (I am not that advanced in PHP so details would be helpful )

    PHP Code:
    <?

    $data
    =file("http://www.blah-blah-blah.com/file.txt");
    foreach(
    $data as $value)
     {
      
    $fields=explode("\t",$value);
      
    $num=count($fields);
      for (
    $n=0;$n<$num;$n++)
       {
        
    $null="";
        print (
    $n==0) ? "<b><font face=arial size=1>IMPORTING...></font></b><BR>" $null;
       }

    include(
    "../config.php");
    //set_time_limit(0); - not needed yet

    // Insert into categories table here
    mysql_query("INSERT into products2 (name,keywords) VALUES ('$fields[0]','$fields[1]')");

    }

    ?>
    Thank You

  2. #2
    SitePoint Enthusiast Martin63's Avatar
    Join Date
    Mar 2004
    Location
    Lincoln Park, NJ
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just try this:

    PHP Code:
    $data=file(http://www.blah-blah-blah.com/file.txt); 
    $skipped 0;
    foreach(
    $data as $value

      
    $skipped++;
      if (
    $skipped 6
        {
         
    $fields=explode("\t",$value); 
         
    $num=count($fields); 
         for (
    $n=0;$n<$num;$n++) 
         { 
            
    $null=""
            print (
    $n==0) ? "<b><font face=arial size=1>IMPORTING...></font></b><BR>" $null
         } 
     
        include_once(
    "../config.php"); 
        
    //set_time_limit(0); - not needed yet 
     
        // Insert into categories table here 
        
    mysql_query("INSERT into products2 (name,keywords) VALUES ('$fields [0]','$fields[1]')"); 
        }

    Hope it will solve your prob.

  3. #3
    Forum Buyer
    Join Date
    Jun 2004
    Location
    United States
    Posts
    811
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of skipping the first few lines after you've grabbed the data, skip the lines while you're grabbing the data using start/stop points.
    Founder/Admin of a pretty decent chat forum
    Download free winterboard themes for your iPhone
    I run sites powered by vbulletin and one about the HTC Jetstream.

  4. #4
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \t is a tab, I think you're looking for \n (UNIX) or \r\n (DOS) or even \r (MAC)
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  5. #5
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $data=file(http://www.blah-blah-blah.com/file.txt); 
    Might just want to change that to:
    PHP Code:
     $data=file('http://www.blah-blah-blah.com/file.txt'); 

  6. #6
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait, hold a sec...
    file() return an array and each element is a line of the file
    I though you were splitting the file into lines by tabs, my bad!

    So all you have to do is to trim the first 6 or do what GeorgeB. said, fopen, skip the first 6 fread? (from my previous experience, it reads line by line if the lenght is big enough)
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point


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
  •