Jump to content
karmaB

Php: Include Based On Day

Recommended Posts

Hello. I am newly registered here and have been a customer of TCH for a couple years now... I hope someone can help me with my problem. I'm kind of a PHP dunce and what seems to be simple isn't turning out to be so. Here's what I've got:

 

I am trying to use this script I found to include a certain file based on the current day of the month. So, on the 1st it will include file1.txt, the 2nd will include file2.txt, etc.

 

I'm including the script on one of my pages so the designated text file will be shown (ideally) on the page for each day of the month. Here's the code...if you see that I've formatted something incorrectly, please help!

 

--------The Files---------

1- File that calculates: todaysFile.php

2- Result Page - test.php: <? include('todaysFile.php');?>

3- Files to be included: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt, file6.txt, file7.txt

-------------------------------

 

Here is everything inside todaysFile.php:

 

<?php

 

$totalFiles="7"; // Total files to pull from

 

$todaysFile = Array ( // Up to 31

 

'1' => 'file1.txt' , // Name of or Path to file

'2' => 'file2.txt' , // Name of or Path to file

'3' => 'file3.txt' , // Name of or Path to file

'4' => 'file4.txt' , // Name of or Path to file

'5' => 'file5.txt' , // Name of or Path to file

'6' => 'file6.txt' , // Name of or Path to file

'7' => 'file7.txt' , // Name of or Path to file

 

);

 

// Place day of month in variable 'x'

$x=date("d");

 

// If value of date exceeds the amount of files then pick a random file to display

if($x > $totalFiles) { $x=rand(1,$totalFiles); }

 

// Display file

include("$todaysFile[$x]");

 

?>

 

And here is the error on test.php when all is said and done:

 

Warning: main(): Failed opening '' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/blah/public_html/todaysFile.php on line 24

 

It looks to me like it isn't actually requesting the file to be included...like it can't open a file that isn't named. I know this has got to be something simple but I don't know enough to figure it out. Please help if you can! Thank you so much.

 

L

Share this post


Link to post
Share on other sites

Welcome to the forum, karmaB. :unsure:

 

Sorry, don´t know the answer to this but hold on, there are plenty of php gurus here.

Share this post


Link to post
Share on other sites

>include("$todaysFile[$x]");

At first glance it appears that you're telling the server to look for a file named $todaysFile[$x] instead of the value of $todaysFile[$x]. I would guess this would work better

 

include($todaysFile[$x]);

or

include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x];

Share this post


Link to post
Share on other sites
>include("$todaysFile[$x]");

At first glance it appears that you're telling the server to look for a file named $todaysFile[$x] instead of the value of $todaysFile[$x].  I would guess this would work better

 

include($todaysFile[$x]);

or

include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x];

 

Thank you for helping!

 

When I tried include($todaysFile[$x]); the error is the same.

 

When I tried include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x]; the error is:

 

Warning: main(/home/blah/public_html/): failed to open stream: Success in /home/blah/public_html/todaysFile.php on line 24

 

Warning: main(/home/blah/public_html/): failed to open stream: Permission denied in /home/blah/public_html/todaysFile.php on line 24

 

Warning: main(): Failed opening '/home/blah/public_html/' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/blah/public_html/todaysFile.php on line 24

Edited by karmaB

Share this post


Link to post
Share on other sites

Why don't you change the

 

include($todaysFile[$x]);

 

to

 

echo($todaysFile[$x]);

 

to see what the variable really is.

Share this post


Link to post
Share on other sites
Why don't you change the

 

include($todaysFile[$x]);

 

to

 

echo($todaysFile[$x]);

 

to see what the variable really is.

 

 

Just tried that and it echos nothing. Just a blank space. It's like no file is actually being plugged in there...even in the original error, it said "Failed opening '' for inclusion" like its not actually including anything at all. ?? Mind you, I don't honestly know what I'm talking about. :unsure:

Share this post


Link to post
Share on other sites

date("d") will put a leading '0' on your single digit dates, so today would be 01, not 1. Your array currently looks like this:

 

>$todaysFile = Array ( // Up to 31

'1' => 'file1.txt' , // Name of or Path to file
'2' => 'file2.txt' , // Name of or Path to file
'3' => 'file3.txt' , // Name of or Path to file
'4' => 'file4.txt' , // Name of or Path to file
'5' => 'file5.txt' , // Name of or Path to file
'6' => 'file6.txt' , // Name of or Path to file
'7' => 'file7.txt' , // Name of or Path to file

);

 

Try this:

 

>$todaysFile = Array ( // Up to 31

'01' => 'file1.txt' , // Name of or Path to file
'02' => 'file2.txt' , // Name of or Path to file
'03' => 'file3.txt' , // Name of or Path to file
'04' => 'file4.txt' , // Name of or Path to file
'05' => 'file5.txt' , // Name of or Path to file
'06' => 'file6.txt' , // Name of or Path to file
'07' => 'file7.txt' , // Name of or Path to file

);

 

Or to get the day of the month without the leading 0, use date("j"). More here on PHP date functions.

Share this post


Link to post
Share on other sites
date("d") will put a leading '0' on your single digit dates, so today would be 01, not 1.  Your array currently looks like this:

 

>$todaysFile = Array ( // Up to 31

'1' => 'file1.txt' , // Name of or Path to file
'2' => 'file2.txt' , // Name of or Path to file
'3' => 'file3.txt' , // Name of or Path to file
'4' => 'file4.txt' , // Name of or Path to file
'5' => 'file5.txt' , // Name of or Path to file
'6' => 'file6.txt' , // Name of or Path to file
'7' => 'file7.txt' , // Name of or Path to file

);

 

Try this:

 

>$todaysFile = Array ( // Up to 31

'01' => 'file1.txt' , // Name of or Path to file
'02' => 'file2.txt' , // Name of or Path to file
'03' => 'file3.txt' , // Name of or Path to file
'04' => 'file4.txt' , // Name of or Path to file
'05' => 'file5.txt' , // Name of or Path to file
'06' => 'file6.txt' , // Name of or Path to file
'07' => 'file7.txt' , // Name of or Path to file

);

 

Or to get the day of the month without the leading 0, use date("j").  More here on PHP date functions.

 

 

YEEEHAAAWWWWW!

 

Thank you guys soooooo much!!! The 0 did the trick. The stupid thing is I was going to try that last night when I was so danged frustrated but got totally sidetracked with another thought.

 

Thank you thank you thank you!

 

L

Share this post


Link to post
Share on other sites

Good catch Tim :clapping:

 

 

oh yes

:dance: Welcome to the family KarmaB :dance:

 

 

Don't be a stranger :)

Share this post


Link to post
Share on other sites

This forum amazes me, time and time again.

 

Thank you to everyone for making the forums so special.

 

Bill

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...