Idiom of the day php help

Hello everyone,

Need some help with php. Got a text file with idioms - idioms.txt with the following layout

to rub someone the wrong way : means to irritate someone : Don’t rub her the wrong way!

ie. idiom, meaning, example.

How can I get this to show a new idiom every day using php.

Any help will be really appreciated.

Thanks

If you load the text file into a database - It’ll be a lot easier to manager and do what you want to do.

[member=69011]Topcoder[/member] If there is only a handful of quotes, a database would be overkill.

This is a very simple task. Here is a couple examples:

[php] $quotes[] = ‘Your first quote’;
$quotes[] = ‘Your second quote’;
$quotes[] = ‘etc…’;

srand ((double) microtime() * 1000000);
$random_number = rand(0,count($quotes)-1);

echo ($quotes[$random_number]);[/php]

OR

[php]$file= “quotes.txt”;
$quotes = file($file);
srand((double)microtime()*1000000);
$randomquote = rand(0, count($quotes)-1);
echo $quotes[$randomquote];[/php]

[member=46186]Kevin Rubio[/member]

I don’t think he’s looking too pull a random quote, I think he’s looking to display a different quote everyday.

Yes, your right. Didn’t have my coffee yet.

Depending on how many quotes there are, I may convert the txt file to xml. Then you use rand to pick the numeric value of the quoting node.

I personally don’t think a think a database table is overkill, for one can simply do something like the following:

[php]<?php

require_once ‘lib/includes/utilities.inc.php’;

/* Here’s the structore of the database table
CREATE TABLE IF NOT EXISTS idiom (
id int(5) NOT NULL,
idiom text NOT NULL,
meaning text NOT NULL,
current_date datetime NOT NULL DEFAULT ‘0000-00-00 00:00:00’
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
*/
$current_date = new DateTime(“Now”, new DateTimeZone(‘America/Detroit’));
try {
$query = “SELECT idiom, meaning, daily_date FROM idiom WHERE daily_date >= CURDATE() AND daily_date < DATE_ADD(CURDATE(), INTERVAL 1 DAY)”;
$stmt = $pdo->prepare($query);
$result = $stmt->execute();
$row = $stmt->fetch(PDO::FETCH_OBJ);
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
?>

<?php echo $row->idiom; ?>

<?php echo $row->meaning; ?>

[/php]

Though I think daily_date should be unique in the database_table in order to avoid having two dates being the same.

Though how astonecipher is going about setting it up for using a text file is probably the best way to go for the OP question.

Sponsor our Newsletter | Privacy Policy | Terms of Service