473,383 Members | 1,805 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,383 software developers and data experts.

How to use for loop to write to file?

This has got to be an easy question - for some reason I can't seem to get
this to work. Is there a better way? All I want is a quick and dirty way
to populate a file with UNIX time stamps...

<?php
//viscount_helper.php
$vc = 'viscounter_test.txt';
$avdaily = 5; average daily
$total_count = (365 * $avdaily); //time period = one year
echo $total_count;
$interval = (86400)/$avdaily; //evenly spaced intervals
echo $interval;
$visit = time() - (31536000);
echo $visit;
//$next_time = ($first_time + $interval)
$fp = fopen($vc,"a");
for($i = 1; $i > $total_count; $i++)
{
fwrite($fp, $visit."\n");
$visit = $visit + ($interval * $i);
}
fclose($fp);
Jul 17 '05 #1
4 2768
On Tue, 20 Jul 2004 07:03:18 +0000, deko wrote:
This has got to be an easy question - for some reason I can't seem to get
this to work. Is there a better way? All I want is a quick and dirty way
to populate a file with UNIX time stamps...

<?php
//viscount_helper.php
$vc = 'viscounter_test.txt';
$avdaily = 5; average daily
$total_count = (365 * $avdaily); //time period = one year
echo $total_count;
$interval = (86400)/$avdaily; //evenly spaced intervals
echo $interval;
$visit = time() - (31536000);
echo $visit;
//$next_time = ($first_time + $interval)
$fp = fopen($vc,"a");
for($i = 1; $i > $total_count; $i++) ^

Are you sure this should be > rather than < ?

Unless average daily is ever 0, the loop will never run =)

{
fwrite($fp, $visit."\n");
$visit = $visit + ($interval * $i);
}
fclose($fp);

Other than that, I'd say it's pretty much the "only" way =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2
> Are you sure this should be > rather than < ?

Good catch - that was part of it...

Here's a working version:

$vc = 'countfile.txt';
$avdaily = 38; //expected average daily hits
$total_count = (366 * $avdaily);
echo "<br>total_count = ".$total_count;
$interval = (86400 / $avdaily); //evenly spaced intervals
echo "<br>interval = ".$interval;
$visit = (time() - (31622400));
echo "<br>first visit = ".$visit."<br><br>";
$fp = fopen($vc,"w");
for($i = 1; $i <= $total_count; $i++)
{
echo $visit."<br>";
fwrite($fp, $visit."\n");
$visit = ($visit + $interval);
}
echo "<br>last visit = ".$visit;
fclose($fp);

The problem is that I am getting decimal output in $visit - e.g.
1058691603.37
Is there a way to force an integer value to avoid decimals?
Jul 17 '05 #3
deko wrote:
The problem is that I am getting decimal output in $visit - e.g.
1058691603.37
Is there a way to force an integer value to avoid decimals?


http://www.php.net/intval
Jul 17 '05 #4
> http://www.php.net/intval

that's the ticket
Jul 17 '05 #5

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

Similar topics

8
by: David P. Jessup | last post by:
Well I have seen this posted before and haven't seen much in response. My application has to browse through various folders and find file names. Sometimes these folders can have thousands of...
5
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
3
by: AutoShutdown | last post by:
How can I change a FOR EACH loop to a DO WHILE loop? I have this code... For Each objFile In objFolder.Files response.write objFile.Name Next
1
by: showson1 | last post by:
Hi all! I have some files that are basically a TIF with an ASCII header. I wrote an app that reads in the file, pulls some values from the header and writes out everything after the header as a...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
1
by: Semajthewise | last post by:
Hi all!! What I am trying to do today is write to a textfile then read from the file get some lines from that file and edit them. and re-write them to the file. Not replace I want the edited lines...
10
by: AdemusPrime | last post by:
I need to write data to a file in a loop. I get a "file already in use" error on the second loop. The problem is, I don't know the name of the file until I'm in the loop. How can I keep a file open...
5
by: dp_pearce | last post by:
I have some code that takes data from an Access database and processes it into text files for another application. At the moment, I am using a number of loops that are pretty slow. I am not a...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.