tags:

views:

41

answers:

3

if have file ex (news.txt)

and i need php to read my words in this file just one lind by day

every day php read one line only>>

i write this code but it not work >>>any help

 $wordfile = "words.txt";
 $open = fopen($wordfile, "r");
 $read = fread($open, filesize($wordfile));
 fclose($wordfile);
 $array = explode("\n",$read);
 $date = date("z");
 while ($date++){
echo $array;
 }
+1  A: 
$f = file("words.txt");
echo $f[date("z")];

It's as simple as that, given you have a file with 366 lines. The problem of your code is that the loop never finishes. You probably wanted while ($date--) and then do something in the loop that changed the "current line".

Artefacto
this code not print any thing
magy
@magy Yes, it does... http://codepad.viper-7.com/aZJSPT
Artefacto
A: 

The following code will, when run, output the record at line number [days since 7/13/2010]

<?php

$startDate = mktime(0,0,0,7,13,2010);
$currentDate = time();
$dateDiff = $currentDate - $startDate;

$dayOn = floor($dateDiff/(60*60*24));

$lines = file("words.txt");

echo trim($lines[$dayOn]) . "\n";

?>
Fosco
this code not print any thing
magy
How did you execute it?
Fosco
A: 

It will be an easier task if you use the file() function instead, which reads the entire file into an array:

$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
for ($i = 0; $i < $count; $i++) {
    echo 'Line ',($i + 1),': ',$lines[$i],'<br />';
}

Update:

If I understood correctly your requirements, you will have a text file with seven lines, one for each day of the week. So, the code will be like this:

$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
$day_of_week = date('z'); // 0 (for Sunday) through 6 (for Saturday)
echo $lines( $day_of_week );

Should you need a more complex solution, you can alter the line $day_of_week = date('z'); to match your needs. Read more about the date() function of PHP.

Anax
this code print all lines and i dont need that , i just need one line for one day,for ex (in saturday print hello, in sunday print the next line godbay and go on .
magy
then you need to use date('z'), see my updated answer.
Anax