New to PHP, have specific question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
analog.kid
Forum Newbie
Posts: 3
Joined: Thu Oct 23, 2003 6:41 pm

New to PHP, have specific question...

Post by analog.kid »

Hi, I'm brand new to PHP. Here is my question:

I'm working on a website that wants to include a page in a box on the front page. The catch is, there are 14 pages to include, 2 for ever day of the week. What I need is a way to identify what day and time it is when the user accesses the page, and then include the correct page in the box for them to view.

I've been looking at getdate and related things, and I know more or less how to do includes, but I have no idea how to tie them together so this will work.

Can anyone point me in the right direction? TIA

a.k
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Rename those 14 files that you need to include to things like mon_am.htm, mon_pm.htm, tue_am.htm, wed_am.htm, sat_pm..... etc..... and then this code should work.

Code: Select all

<?

$getfile = date("D_a")."htm"; // that will give you the file name to include.. ie sun_am.htm

require($getfile); // same as include() but will tell you if any errors pop up.

?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Gen-ik wrote:Rename those 14 files that you need to include to things like mon_am.htm, mon_pm.htm, tue_am.htm, wed_am.htm, sat_pm..... etc..... and then this code should work.

Code: Select all

<?

$getfile = date("D_a")."htm"; // that will give you the file name to include.. ie sun_am.htm

require($getfile); // same as include() but will tell you if any errors pop up.

?>
why is there a include function is require is so much better?
analog.kid
Forum Newbie
Posts: 3
Joined: Thu Oct 23, 2003 6:41 pm

Post by analog.kid »

Gen-ik, thank you for your help! I'll try it out once I figure out why I can't upload to my site :roll:

a.k
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

When did he say require() is better than include() ??

The difference between require() and include() is that require() will cause a fatal error if the file cannot be included, include() produces an error.

So, if you have the following code:

Code: Select all

<?php
require("topOfPage.html");
echo '<p>main stuff</p>';

?>
Now, if topOfPage.html doesn't exist, or it cannot be included then nothing but an error will be produced on the page, you will not see "main stuff." If you use include() instead of require() you will also see an error, but in addition to the error you will see the text "main stuff."

Hope this helps ?
analog.kid
Forum Newbie
Posts: 3
Joined: Thu Oct 23, 2003 6:41 pm

Post by analog.kid »

Thanks again, Gen-ik - that code seems to be the answer. I did have to change "htm" to ".htm" to make it work, though. Also my files had to be renamed with capitalized days ("Fri" instead of "fri") but other than that, it works like a charm.

:D

a.k
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You can use strtolower() to change "Fri" to "fri"
http://us4.php.net/manual/en/function.strtolower.php
Post Reply