Pulling date-specific data from flat file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danielbaars

    Pulling date-specific data from flat file

    Hello,

    I'm almost a complete newbie to using PHP and I was hoping one of you
    guys can point me in the right direction. I'm working on a site for a
    brand of handmade fruit juice and it only needs one bit of dynamic
    content: the info on what fruit is in the juice and where it is coming
    from (vendor, country etc.) today and tomorrow (the content of the
    juice is different everyday). So I thought it would be easiest to put
    that information in a flat file (an Excel file would be ideal but a
    plain text would do too) on a weekly basis and try to pull out the
    needed info with PHP. Except I don't know how yet. I don't expect any
    of you to do the work for me but if someone could point me to which
    functions of PHP I should do some research on, I'd be most grateful.

    Kind regards,



    Daniel Baars

  • Randy  Harmelink

    #2
    Re: Pulling date-specific data from flat file

    I have a PHP routine that does an RSS website scraping process only
    once per day - I use an external file to trigger whether it does
    anything or not. Here's what I use:

    $inData = file_get_conten ts('/xml/ts/data1.txt');
    $str = file_put_conten ts('/xml/ts/data1.txt', $pubDate);
    if ($inData != $pubDate) { ...do this process ... }

    The first statement retrieves a publication date that was last saved.
    The second statement writes the new publication date to the file.
    Then, I only do my daily process if the publication date I retrieved is
    different from the one I just wrote. Technically, I probably should
    only write the new date AFTER my process is complete -- just in case a
    problem occurs and the process doesn't complete.

    But, anyway, those two functions are a nice easy way to read and write
    a flat file.

    On Nov 1, 12:41 am, "danielbaar s" <danielba...@gm ail.comwrote:
    >
    I'm almost a complete newbie to using PHP and I was hoping one of you
    guys can point me in the right direction. I'm working on a site for a
    brand of handmade fruit juice and it only needs one bit of dynamic
    content: the info on what fruit is in the juice and where it is coming
    from (vendor, country etc.) today and tomorrow (the content of the
    juice is different everyday). So I thought it would be easiest to put
    that information in a flat file (an Excel file would be ideal but a
    plain text would do too) on a weekly basis and try to pull out the
    needed info with PHP. Except I don't know how yet. I don't expect any
    of you to do the work for me but if someone could point me to which
    functions of PHP I should do some research on, I'd be most grateful.

    Comment

    • pangea33

      #3
      Re: Pulling date-specific data from flat file

      Let's say your target page, is this pretty decent sized document at
      W3.org: http://validator.w3.org/docs/users.html

      View the page source manually to find some unique portions that
      delineate the important section, and plug them into the $cStartStr and
      $cEndStr variables. For this exercise I chose to extract the section
      labeled "Interpreti ng the results".

      <?php
      $cTargetPage = "http://validator.w3.or g/docs/users.html";
      $ch = curl_init();
      curl_setopt($ch , CURLOPT_URL, $cTargetPage);
      curl_setopt($ch , CURLOPT_HEADER, 0);
      curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
      curl_setopt($ch , CURLOPT_BINARYT RANSFER, 1);

      $cPgStr = curl_exec($ch);
      curl_close($ch) ;

      $cStartStr = "<div id=\"Interpret\ " class=\"stb\">" ;
      $cEndStr = "</div>";
      $cPageTail = stristr($cPgStr , $cStartStr);
      $nUsefulDataEnd Pos = strpos($cPageTa il, $cEndStr);
      $cUsefulData = substr($cPageTa il, 0, $nUsefulDataEnd Pos+6);
      echo($cUsefulDa ta);
      ?>

      Run the script and save the relevent portion somewhere useful instead
      of echoing it out. :-)

      Comment

      • nigel.t@by.uk

        #4
        Re: Pulling date-specific data from flat file



        write your data as csv and shell out to grep

        that will provide you with the ability to import/export your flat file
        from excel and to perform quite complex lookups without any difficult
        code to write.

        its what grep is for.

        nigel.

        Comment

        • Toby Inkster

          #5
          Re: Pulling date-specific data from flat file

          danielbaars wrote:
          that information in a flat file (an Excel file would be ideal but a
          plain text would do too) on a weekly basis and try to pull out the
          needed info with PHP. Except I don't know how yet. I don't expect any
          of you to do the work for me but if someone could point me to which
          functions of PHP I should do some research on, I'd be most grateful.
          If you create a text file much like this:

          20061101,Apple, Normandy
          20061102,Orange ,Seville
          20061103,Pineap ple,the Azores
          20061104,Coconu t,Fiji

          You should be able to pull that information into PHP very easily using the
          file() function; find out today's date using date(); then loop through
          each line of the file comparing the first few characters of the file with
          today's date, something like this:

          $today = date('Ymd');
          $lines = file('data.txt' );
          $info = FALSE;
          while (!$info)
          {
          $thisline = array_pop($line s);
          if ($today==substr ($thisline,0,8) )
          $info = $thisline;
          }

          This will find you the relevent line and store it into a variable called
          $info.

          The rest is easy-peasy...

          list($today,$fr uit,$location) = explode(',', $info);
          printf("Today's drink is %s juice from %s.", $fruit, $location);

          See:









          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact

          Comment

          • danielbaars

            #6
            Re: Pulling date-specific data from flat file

            Wow! Thank you all so much! The last example is very close to what I'm
            trying to do. I got this to work no problem but how do I list more than
            one item and also the items for tomorrow?

            The text file would be something like:
            20061101,Apple, Normandy,John Smith
            20061101,Banana s,Chili,Jack Jones
            20061101,Orange ,Seville,Robert Brown
            20061101,Coconu t,Fiji,Ed Johnson
            20061102,Apple, California,Tess Williams
            20061102,Banana s,Argentina,Joe Taylor
            20061102,Orange ,South Africa,Sarah Anderson
            20061102,Coconu t,Hawaii,Harry Thompson

            And make it into a listing like:
            The apples in our juice comes from:
            Today: Normandy (by John Smith)
            Tomorrow: California (by Tess Williams)

            The bananas in our juice comes from:
            Today: Chili (by Jack Jones)
            Tomorrow: Argentina (by Joe Taylor)

            etc...

            Many thanks,


            DB

            Comment

            • nigel.t@by.uk

              #7
              Re: Pulling date-specific data from flat file

              In article <1162392626.266 104.251670@b28g 2000cwb.googleg roups.com>,
              danielbaars@gma il.com says...
              Wow! Thank you all so much! The last example is very close to what I'm
              trying to do. I got this to work no problem but how do I list more than
              one item and also the items for tomorrow?
              >
              like I said - using grep makes life easier.

              nigel.

              Comment

              • danielbaars

                #8
                Re: Pulling date-specific data from flat file

                Thanks for your reply. Could you elaborate a bit? I've been
                investigating the PHP grep combination for the past few days but the
                few bits I could find on it I found very hard to apply to my needs. The
                regular expressions are not the biggest problem, I've worked with them
                before and I'm reasonably sure I can figure out how to select the right
                data in the CSV file. What I find hard to understand (and hard to find
                info on) is how to make it interact with PHP. For instance, how do I
                put the right date for 'today' and 'tomorrow' in my regular expression?
                And how do I use the data that the regex has found?


                DB

                On Nov 1, 6:23 pm, nige...@by.uk wrote:
                In article <1162392626.266 104.251...@b28g 2000cwb.googleg roups.com>,
                >like I said - using grep makes life easier.
                >
                nigel.

                Comment

                Working...