...

View Full Version : Daily Fact Script Help



CanHasPotato
09-12-2009, 01:44 AM
Basically, it is supposed to open "quotes.txt", explode and find the current date (in Ymd format), then echo the second exploded part of the matching date. I just can't seem to find the problem with it. I've included my quotes.txt and php file below:

20090911|That my script works!
20090912|That my script works! (again) YAY.


$current = date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
$file = fopen("quotes.txt","r");
$explode = explode('|', $file); // [0] date, [1] quote
$strpos = strpos($current, $explode[0]);
if ($strpos == TRUE) {
echo "Did you know... " . $explode[1];
}
else {
echo "error";
}

sea4me
09-12-2009, 04:18 AM
This should work:

$current = date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
$file = file("quotes.txt");
foreach($file as $key => $value){
$strpos = strpos($current, $key);
if ($strpos == TRUE) {
echo "Did you know... " . $value;
}
else {
echo "error";
}
}

chump2877
09-12-2009, 04:32 AM
Without testing it, something like this perhaps:


$success = false;
$file = file_get_contents("quotes.txt");
$lines = preg_split('/\n|\r/', $file, -1, PREG_SPLIT_NO_EMPTY);
foreach ($lines as $line)
{
$line_parts = explode('|', $line)
if (is_array($line_parts) && count($line_parts) > 1)
{
if ($line_parts[0] == date("Ymd"))
{
echo "Did you know... " . $line_parts[1];
$success = true;
break;
}
}
}

if (!$sucesss)
{
die('Error');
}

chump2877
09-12-2009, 05:01 AM
This should work:

$current = date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
$file = file("quotes.txt");
foreach($file as $key => $value){
$strpos = strpos($current, $key);
if ($strpos == TRUE) {
echo "Did you know... " . $value;
}
else {
echo "error";
}
}


looks like file() will also work with the optional flags for PHP5:


$success = false;
$lines = file("quotes.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line)
{
$line_parts = explode('|', $line)
if (is_array($line_parts) && count($line_parts) > 1)
{
if ($line_parts[0] == date("Ymd"))
{
echo "Did you know... " . $line_parts[1];
$success = true;
break;
}
}
}

if (!$sucesss)
{
die('Error');
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum