PHP Includes and file extensions

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
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

PHP Includes and file extensions

Post 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?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Re: PHP Includes and file extensions

Post 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.
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Re: PHP Includes and file extensions

Post 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?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Post 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!
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Post 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!
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Post 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?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Post 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.
Quietus
Forum Newbie
Posts: 16
Joined: Sat Nov 15, 2003 1:59 am

Post 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.
Cyx07
Forum Newbie
Posts: 8
Joined: Sat Nov 15, 2003 5:05 pm

Post 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";
}

?>
Quietus
Forum Newbie
Posts: 16
Joined: Sat Nov 15, 2003 1:59 am

Post 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)
      &#123;
      if(somethingtrue)
         &#123;
         do_this
         &#125;
      else
         &#123;
         do_this
         &#125;
      $fileday-- etc...
      &#125;
Forgive me for not showing this fully using your code, but I'm lazy :wink:
Post Reply