473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Nov 1 '06 #1
7 2256
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_contents('/xml/ts/data1.txt');
$str = file_put_contents('/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, "danielbaars" <danielba...@gmail.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.
Nov 1 '06 #2
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 "Interpreting the results".

<?php
$cTargetPage = "http://validator.w3.org/docs/users.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTargetPage);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

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

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

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

Nov 1 '06 #3


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.
Nov 1 '06 #4
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,Pineapple,the Azores
20061104,Coconut,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($lines);
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,$fruit,$location) = explode(',', $info);
printf("Today's drink is %s juice from %s.", $fruit, $location);

See:
http://uk.php.net/manual/en/function.file.php
http://uk.php.net/manual/en/function.date.php
http://uk.php.net/manual/en/control-...ures.while.php
http://uk.php.net/manual/en/function.array-pop.php
http://uk.php.net/manual/en/function.substr.php
http://uk.php.net/manual/en/function.explode.php
http://uk.php.net/manual/en/function.list.php
http://uk.php.net/manual/en/function.printf.php

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

Nov 1 '06 #5
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,Bananas,Chili,Jack Jones
20061101,Orange,Seville,Robert Brown
20061101,Coconut,Fiji,Ed Johnson
20061102,Apple,California,Tess Williams
20061102,Bananas,Argentina,Joe Taylor
20061102,Orange,South Africa,Sarah Anderson
20061102,Coconut,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

Nov 1 '06 #6
In article <11**********************@b28g2000cwb.googlegroups .com>,
da*********@gmail.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.
Nov 1 '06 #7
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.266104.251...@b28g2000cwb.googlegroups .com>,
like I said - using grep makes life easier.

nigel.
Nov 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jason | last post by:
I have a form which ads a technician service ticket into mysql. The fields are tech name time spent service client name etc... I assumed that mysql was storing the date of each record...
4
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record...
1
by: Skully Matjas | last post by:
Thank you for getting back to me. I am very new at this so i didnot understand what you said, here i will give as much cetails as possible: 1) The combo box i am using is combox39 2) I imported...
3
by: rugger81 | last post by:
I am currently working in the sql server 2000 environment and I want to write a function to pull all dates within a given date range. I have created several diferent ways to do this but I am...
5
by: esparkman | last post by:
Hey guys I am working on a application, that will pull our inventory campus wide. I'm having troubles getting it to pull our software serial numbers. It keeps throwing a Exception when it gets to...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
20
by: scolivas | last post by:
I have a query that is pulling from a table of 35000+ records But for some reason any records beyond 25999 are not coming thru. The Table is a list employees - and thier assignments - so there...
1
by: tvance929 | last post by:
This should be simple, I just cannot find out the answers easily. At work I have 9 Aircards. I am responsible for checking them out and keeping track of them. I wanted to make a simple C#...
1
by: bostonguy70 | last post by:
I am not a mysql expert and was trying to find something like this. I have two tables. Comments Table has 3 fields, country, comments and date with NO unique or primary keys Table below...
3
by: Nepenthen | last post by:
Hello – I am still new to Access and I am currently working on debugging some code. I have a form with the following: Employee Name (combo with select all choice) Task (combo with select all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.