473,666 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newbie Question - How to import arrays from textfile

5 New Member
I am new thanks for the help.

I have a text file set up like this:
word1a<tab>word 2a<tab>word3a<t ab>word4a
word1b<tab>word 2b<tab>word3b<t ab>word4b
word1c<tab>word 2c<tab>word3c<t ab>word4c

I want a php program that compares a date and removes a line if the date is past.

Please don't say to use cron or just throw a function at me, I really am super new =]

The problem I am having already is that I can't seem to assign the values into an array that has them all and remembers them. If I could get it to work, it would look like:

whatever.array would be word1a, word2a, word3a, word4a but it would have all the lines like that....

And then I would work on the date but I can't even get the array to work.

Let me show you what I have, which I basically just copied from a book.

[php]
$values=array(6 ); // 6 different fields in the text file
$arrays=array(1 00); //I have no idea how to really do arrays so I just guessed
$fp = fopen("test.txt ", "r"); //open test.txt to read
$firstline = fgets($fp); //do nothing with the first line for now
print $firstline . "<br /><br />"; //print the first line (debug really)

while (!feof($fp)){ //while not end of file
$i += 1; //increment for array position (again no idea if this is right)
$line = fgets($fp); //read from file
$values = split(" ", $line); //tab-deliminated
$arrays[$i] = $values; //is this right?
print "Show #" . $i . ": <br /> " . $line . "<br />"; //debug print
}//end while

print "<br/><br/><br/>" . "values[0]: " . $values[0] . "values[1]: " . $values[1] . " values[2]: " . $values[2] . " values[3]: " . $values[3] . " values[4]: " . $values[4] . " values[5]: " . $values[5] . " values[6]: " . $values[6] . "<br /><br /><br />" . $arrays; //this is just a test debug print
fclose($fp); //close test.txt
[/php]

The problem is it reads the line into the array, then replaces it with the next line...I think... I am so damn lost help me !! =]
Mar 14 '06 #1
8 3157
Banfa
9,065 Recognized Expert Moderator Expert
I think your code is fairly correct but you are missing a

$i = 0;

just before the while loop, without it

$i += 1;

is a bit meaning less.

You could post your output it might help;

This line is not necessary
$values=array(6 ); // 6 different fields in the text file

This line
$arrays=array(1 00);

should be

$arrays=array() ;

I would do it like this

[php]
$arrays = file("test.txt" ); /* file reads an entire file into an array of lines */

foreach($arrays as $key => $line) /* foreach loops through all items in an array */
{
if ( $key == 0 )
{
print $line . "<br /><br />"
}
else
{
$arrays[$key] = split(" ", $line);
print "Show #" . $key . ": <br /> " . $line . "<br />"; //debug print
}
}//end foreach
[/php]
Mar 14 '06 #2
scythetleppo
5 New Member
ahhh SO cool thanks! I have a question.

I have gotten this:

--total entries--

Show #1:
1. (date1) (ages1) (location1) (price1) (comments1)
Show #2:
2. (date2) (ages2) (location2) (price2) (comments2)

as output (the content of test.txt).

I want to know how to get the value of the date?

I want to compare the date with the current date and delete the whole record if the date is before today's current date.


How would I go about that?

Thanks again!


EDIT: Also, just so you know, these fields will of course have real data in them, I just have those laid out to figure out where I was within the arrays.
Mar 14 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
You can use the date functions provided by php to help you

int strtotime ( string time [, int now] ) - converts a date in string format to a unix timestamp

int time ( void ) - returns the unix timestamp for right now

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] ) returns the unix timestamp for a specific time and date

Since unix timestamps are just the number of seconds past the Unix Epoch (January 1 1970 00:00:00 GMT) they can be easily compared using the normal comparison operators (i.e. < > == <= >= != etc)
Mar 16 '06 #4
scythetleppo
5 New Member
I'm really sorry, I have no idea how to use that to do what I need. I see that it changes the date to a number.

Can I just do....

[php]
<?
$firstDate = "03/16/06";
$secondDate = "03/17/06";

if(strtotime($f irstDate) - strtotime($seco ndDate) >= 0){ //if the difference is positive

print "second date is AFTER first.";

}else{

print "first date is AFTER second.";

}
?>
[/php]
is that how it goes???


also, how do I specify the 2nd value in each row of an array?
Mar 17 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
I'm really sorry, I have no idea how to use that to do what I need. I see that it changes the date to a number.

Can I just do....

[php]
<?
$firstDate = "03/16/06";
$secondDate = "03/17/06";

if(strtotime($f irstDate) - strtotime($seco ndDate) >= 0){ //if the difference is positive

print "second date is AFTER first.";

}else{

print "first date is AFTER second.";

}
?>
[/php]
is that how it goes???
Yes you can that is exactly right, see so you do have an idea stop putting yourself down :D

Note your comment is not quite accurate it should say

// if the difference is positive or zero

and the printed text should say

second date is AFTER or EXACLTY THE SAME AS first

note also you need not do the arithmatic you could just compare the results

[php]
<?
$firstDate = "03/16/06";
$secondDate = "03/17/06";

if(strtotime($f irstDate) <= strtotime($seco ndDate) ){ //if the difference is positive

print "second date is AFTER first.";

}else{

print "first date is AFTER second.";

}
?>
[/php]


and to test against now

[php]
<?
$testDate = "03/22/06";

if(strtotime($t estDate) <= time() ){

print "test date is in the past or RIGHT NOW!!.";

}else{

print "test date has not happened yet";

}
?>
[/php]

also, how do I specify the 2nd value in each row of an array?
I assume you mean the value stated as ages. Since you have not said what this field actually is it is hard for me to answer you.
Mar 18 '06 #6
scythetleppo
5 New Member
yeah that's working nicely thanks!

what I'm trying to do is get the second value from the array record, then compare it to the date and delete the whole record from the file if it's past that date.

like this:



text file:
useless line here.
1 03/18/06 text moretext text blah blah text
2 05/14/04 text moretext text blah blah text
3 09/16/02 text moretext text blah blah text
4 11/15/07 text moretext text blah blah text


the program reads the different dates and sees that the 2 middle dates have passed so it rewrites the file to be:

useless line here.
1 03/18/06 text moretext text blah blah text
2 11/15/07 text moretext text blah blah text


How should I approach this? should I just use a database, even though there will never be more than, say, 20 records? If so, please help =]

EDIT: Also, how can I do the comparison you have showed me, but to check vs tomorrow?

In other words, I want to keep the record until the day *after* the date it has.
Mar 18 '06 #7
scythetleppo
5 New Member
bump bump bump
Mar 22 '06 #8
Banfa
9,065 Recognized Expert Moderator Expert
Sorry I've been a bit ill so haven't logged on for a couple of days.

I think you have all the information you need it's just a matter of putting it together (I probably would use a database for such a small amount of data, although I might consider using a directory with a file for each record any on with the approach that you already have).

I will assume that you can work out the reading and writing of files (but say if this is too much), I can not stress strongly enough that you seem to have grasped stuff and you may be able to work out for yourself a lot of what you are asking by looking in the PHP documentation. I would download it from www.php.net (I have the chm file).

Use the functions file and split as explained in my first post to this thread. Note if each record only consits of a maximum of 3 items, number date and text then you can use the optional 3 parameter to split to prevent youself getting mor than 3 items out of each record. This would be a problem if the text could contain the delimiter you are using for the first 2 items.

I have explained reading a file in a previous post here

testing against the day after a date is very easy, just use strtotime again (please look this function up in the help it does a lot)

so

[php]
$array = <Array of records from file with each element split into an array of items in the record>;

foreach( $array as $key => $record )
{
$recordTime = strtotime($arra y);
$recordTimePlus 1Day = strtotime("+1 day", $recordTime);
// You could just add 24 * 60 * 60 or the number of seconds in 1 day but this is safer

if ( $recordTimePlus 1Day < time() )
{
echo( "Record $key OUT OF DATE\n" );
}
else
{
echo( "Record $key CURRENT\n" );
}
}
[/php]

So putting that together with writing a file using fopen/fwrite/fclose you get something like

[php]
$arrays = file("test.txt" ); /* read the entire file into arrays */

fout = fopen("test.txt ", "w"); /* Open the file to write to it */

foreach($arrays as $key => $line) /* Loop through each line of the file*/
{
// Special handling for first line that is not actual data
if ( $key <= 0 )
{
fwrite( fout, $arrays[$key] );
}
else
{
$line = split(" ,", $line, 3); // Get record number and date

// Calculate the check time
$recordTime = strtotime($arra y);
$recordTimePlus 1Day = strtotime("+1 day", $recordTime);

// If we still want this record write it back to the file
if ( $recordTimePlus 1Day >= time() )
{
fwrite( fout, $arrays[$key] );
}
}
}//end foreach

fclose( fout);
[/php]

Remember, when testing code like this ALWAYS do it on a backup of your data file or back your datafile up first.
Mar 23 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2533
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various columns, and then uses a query to massage some of the data for me (deletes the first blank row and does a couple of calculations) Then I use DTS to import the Access query as a table. the textfile has a column called "File_num", and among several...
3
3664
by: Elaksomfan | last post by:
Hi, I am having trouble importing data from an excel spreadsheet into MS SQL Server 2000 using DTS Wizard. The DTS import process is successfull, no errors, but only 50 rows of approx. 1500 rows of data are imported. I tried to remove 20 rows in the excel spreadsheet in the interval row 0-50. When i later ran the import, only 30 rows were imported. I deleted almost every row in the interval 0-50, with the result of the import having 0...
2
455
by: Sigurd Bruteig | last post by:
Hi all! With the transferText method I have made a routine that import a textfile to a table.The textfile is a pricelist from the supplier. The problem is that the supplier uses . (dot) as descimal point, but the standard desimal point is , (comma). How do i change the desimal point, without any user action. Is it possible to do this in a query? Sigurd --
4
5293
by: mibispam | last post by:
Hello, I'm trying to import a textfile into an Access table. I want to write a macro to do all the work automatically. My textfile looks like that: artNr;description;sellingValue 111111;top;0.34 The different colums are divided by ";" and the sellingValue is a
1
2615
by: joel.sjoo | last post by:
I'm a dba for SQL server and I Will import a textfile to SQL. For example I use a file with 3 columns. ID, Name and Surname and the columns are tab separated. I don't know much about programming. Anyway, I use this code below. It works, but it will not split the columns. I have tried to change the argumnts in str(alllines) Some of the columns can include many characters and some not. For exampel names can be Bo or Lars-Ture. I be glad...
1
4674
by: wadacom | last post by:
I'm sorry to take your time for newbie problems but I've been searching what to do about the problem with my apache server I have. I work with ubuntu dapperdrake I put the last apache server on it and I tried to put the mod_python module to work. the only answer I got was that apache didn't find api structure in the module. afterwards I read I didn't need this module to work with python (only a speed difference as I understand) but the problem...
2
1828
by: arunaksha7 | last post by:
I am new to ASP.NET. I am facing a problem to import contents of a textfile to my ASP.NET Web Application datagrid.
9
5771
by: a | last post by:
Dear friends I want import data from CSV file to mdb file How can I do that in vb.net?
5
3157
by: kashif73 | last post by:
I have a text file with hundreds of records. each line contain 1250 values seperated by a semicolon. I have created 2 tables in SQL server 2000, one with 1000 columns & the second with 250 columns. my question is how can i import this textfile in to these 2 tables.and how i ensure that first 1000 values go to first table & the rest 250 in the next from each line of the text file. can be this be done via VB.NET or ASP? I would be grateful for...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.