Page 1 of 1
PHP Includes and file extensions
Posted: Sat Nov 15, 2003 5:05 pm
by Cyx07
I'm new to PHP, but have the extreme basics figured out. It helps that it is similar to C++, which made it a lot easier for me to pick up on. 2 things I've ran into so far though that I need some help with.
First, my host won't parse any PHP code without having a .php extension. Is there a way around this, or should I just use the .php extension?
Second, I have a page with a link of the day, and its history. Up to now, I've been manually updating this, copy/pasting info every day. I've already been using SSI to include the link on all my pages, but I'd like to go further and automate the history...I'm in the process of switching these includes to PHP because the host won't parse .shtml files for PHP code. My question here is this...if I have file lotd111203.html and file lotd111303.html etc. how can I get PHP to automatically look for these files and include them in a loop?
Re: PHP Includes and file extensions
Posted: Sat Nov 15, 2003 5:42 pm
by vigge89
Cyx07 wrote:First, my host won't parse any PHP code without having a .php extension. Is there a way around this, or should I just use the .php extension?
Second, I have a page with a link of the day, and its history. Up to now, I've been manually updating this, copy/pasting info every day. I've already been using SSI to include the link on all my pages, but I'd like to go further and automate the history...I'm in the process of switching these includes to PHP because the host won't parse .shtml files for PHP code. My question here is this...if I have file lotd111203.html and file lotd111303.html etc. how can I get PHP to automatically look for these files and include them in a loop?
1: You have to save it as .php, else it will be processed as normal HTML, except if you use httaccess to process .html files with PHP.
Re: PHP Includes and file extensions
Posted: Sat Nov 15, 2003 11:02 pm
by Cyx07
vigge89 wrote:1: You have to save it as .php, else it will be processed as normal HTML, except if you use httaccess to process .html files with PHP.
Gotcha. Anyone else have an answer about my page includes?
Posted: Sun Nov 16, 2003 12:07 am
by Paddy
Working from your example
Code: Select all
<?php
$filename = "lotd111203.html";
$filepart = "2";
while (file_exists($filename))
{
include($filename);
$filepart ++;
$filename = "lotd111".$filepart."03.html";
}
?>
Obviously this would only work for ten files but it will push you in the right direction hopefully.
Posted: Sun Nov 16, 2003 12:13 am
by Cyx07
Paddy wrote:Working from your example
Code: Select all
<?php
$filename = "lotd111203.html";
$filepart = "2";
while (file_exists($filename))
{
include($filename);
$filepart ++;
$filename = "lotd111".$filepart."03.html";
}
?>
Obviously this would only work for ten files but it will push you in the right direction hopefully.
SWEET! I guess I just wasn't sure how to do the naming. Hopefully this works

So to put a second variable into the first, I put it between periods...gotcha. Let's see how this works now!
Posted: Sun Nov 16, 2003 12:19 am
by Paddy
Not sure what you mean by putting a second variable into the first. But this might help.
Code: Select all
<?php
$filename = "lotd111".$filepart1.$filepart2."03.html";
?>
Will work fine.
Posted: Sun Nov 16, 2003 12:20 am
by Cyx07
Paddy wrote:Not sure what you mean by putting a second variable into the first. But this might help.
Code: Select all
<?php
$filename = "lotd111".$filepart1.$filepart2."03.html";
?>
Will work fine.
Exactly what I was talking about. Wasn't quite sure how to say it...so that sentence after rereading it seems to be a bit of a verbal disaster. Thanks again!
Posted: Sun Nov 16, 2003 1:24 am
by Cyx07
An additional question, which hopefully you or someone else can answer...is there a simple way to skip a file not in existence, or should I just create an empty file for a day I might skip? Also, is it possible to have it continue until it hits "today" somehow?
Posted: Sun Nov 16, 2003 1:39 am
by Paddy
Ah, sorry, I am an aussie. Didn't realise they were dates. Any reason you can't just number them sequentially? lotd00001.html, lotd00002.html, etc? What you are trying to do will cause a fair bit of thinking.
Posted: Sun Nov 16, 2003 2:56 am
by Cyx07
I could number them sequentially, but I'm using the date info for more than just the filename. Currently, the code looks like this:
Code: Select all
<?php
$today = date("mdy");
$filename = "lotd".$today.".html";
$filemonth = date("m");
$fileday = date("d");
$fileyear = date("y");
$run = "0";
while ($filemonth.$fileday.$fileyear != "092703") {
if (file_exists($filename)) {
echo "<tr><td></td><td>";
$run++;
if ($run > "1"){
echo "<hr>";
}
echo "<h4>";
echo "$filemonth";
echo "/";
echo "$fileday";
echo "/";
echo "$fileyear";
echo "</h4>";
include($filename);
echo "</td></tr>";
$fileday--;
if ($fileday == "00") {
$filemonth--;
$fileday = "31";
}
if ($filemonth == "00") {
$fileyear--;
$filemonth = "12";
}
if ($fileday < "10") {
$fileday = date("d", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
/* if ($filemonth < "10") {
$filemonth = "0".$filemonth;
}
if ($fileyear < "10") {
$fileyear = "0".$fileyear;
}
*/
$filename = "lotd".$filemonth.$fileday.$fileyear.".html";
} else {
echo "";
}
}
?>
The problem I'm running into (and why I'm curious about a nonexistent file) is that it times out on nonexistent files, even though it should just start the next iteration of the loop. It will successfully read all files up to a point where it can't find one. I then eventually get this error:
Fatal error: Maximum execution time of 30 seconds exceeded in D:\FoxServ\www\php\filename.php on line 33
And I should point out that line 33 is the beginning of the loop. I've only posted the PHP, as I figure the rest is a waste of space.
Posted: Sun Nov 16, 2003 6:04 am
by Quietus
Your problem is not nonexistant files, it's an infinite loop.
You need to have the $fileday-- etc... code outside of the if statement.
Posted: Sun Nov 16, 2003 6:09 pm
by Cyx07
I realize this is probably a mess of code and could be much simplified, but it gets the job done...so I thought I'd share, and let everyone enjoy my haphazard code.
Code: Select all
<?php
$today = date("mdy");
$filename = "lotd".$today.".html";
$filemonth = date("m");
$fileday = date("d");
$fileyear = date("y");
$run = "0";
$exists = "0";
while ($filemonth.$fileday.$fileyear >= "092703") {
while (file_exists($filename)) {
echo "<tr><td></td><td>";
$run++;
if ($run > "1"){
echo "<hr>";
}
echo "<h4>";
echo "$filemonth";
echo "/";
echo "$fileday";
echo "/";
echo "$fileyear";
echo "</h4>";
include($filename);
echo "</td></tr>";
$fileday--;
if ($fileday == "00") {
$filemonth--;
if ($filemonth == "04" || $filemonth == "06" || $filemonth == "09" || $filemonth == "11") {
$fileday = "30";
}
if ($filemonth == "01" || $filemonth == "03" || $filemonth == "05" || $filemonth == "07" || $filemonth == "08" || $filemonth == "10" || $filemonth == "12") {
$fileday = "31";
}
if ($filemonth == "02") {
$fileday = "28";
}
}
if ($filemonth == "00") {
$fileyear--;
$filemonth = "12";
}
if ($fileday < "10") {
$fileday = date("d", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
if ($filemonth < "10") {
$filemonth = date("m", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
if ($fileyear < "10") {
$fileyear = date("y", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
$filename = "lotd".$filemonth.$fileday.$fileyear.".html";
}
$fileday--;
if ($fileday == "00") {
$filemonth--;
if ($filemonth == "04" || $filemonth == "06" || $filemonth == "09" || $filemonth == "11") {
$fileday = "30";
}
if ($filemonth == "01" || $filemonth == "03" || $filemonth == "05" || $filemonth == "07" || $filemonth == "08" || $filemonth == "10" || $filemonth == "12") {
$fileday = "31";
}
if ($filemonth == "02") {
$fileday = "28";
}
}
if ($filemonth == "00") {
$fileyear--;
$filemonth = "12";
}
if ($fileday < "10") {
$fileday = date("d", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
if ($filemonth < "10") {
$filemonth = date("m", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
if ($fileyear < "10") {
$fileyear = date("y", mktime (0,0,0,$filemonth,$fileday,$fileyear));
}
$filename = "lotd".$filemonth.$fileday.$fileyear.".html";
}
?>
Posted: Sun Nov 16, 2003 6:20 pm
by Quietus
Sorry, I wasn't clear. What I meant was for the decrement of $fileday to appear only once, but outside of the if. i.e.:
Code: Select all
while(evaluates_true)
{
if(somethingtrue)
{
do_this
}
else
{
do_this
}
$fileday-- etc...
}
Forgive me for not showing this fully using your code, but I'm lazy
