Page 1 of 1

If statement not evaulating, any ideas where my error is?

Posted: Fri Oct 06, 2006 5:55 pm
by akimm
Problem is, as of now it only says articles are older than 10 days, when i know for sure i addedone, so its not. It should say there isa new one. However, it doesn't.

Code: Select all

<?php
#new graphic, besides link to article
$img = "<img src=http://www.akimm.com/images/new1.gif>";
#path for files
$path = "article/";
#check to see its a directory
if(is_dir($path)) {
#if so establish handle
$handle = opendir($path);
#while loop to read through that established handle
while ($file = readdir($handle)) {
#validate that it is a file
if (is_file($path.$file))  {
#from the time it is now, minus the time when it was made
$diff = round((time() - filectime($path.$file))/3600/24);
	#if its less  than 10 days then echo link statinga new link		
        if ($diff < 10) {
echo "<ul>" . "<li>" . $img .  $file . "<a href=http://www.akimm.com/philosophy.php>" . "     Click to view " . "</a>" . "</li>" . "</ul>";
#break loop, my task is done
break;
      } else {
#else, its older than 10 days, at whichpoint i invite someone to add an article
echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "clicking here " .

"</a>";
#break that loop task is done
break;
#close the directory, we are done reading it
 closedir($handle);
								}
						  	 }
						   }
					    }

?>

Posted: Fri Oct 06, 2006 6:00 pm
by volka
Which if?
What's it supposed to do?
What's does it do?
Is this script running with error_reporting E_ALL?
Did you let the script print debug messages? e.g. the contents of some variables?
if(is_dir($path)) { does not have an else block, if $path does not point to a directory the script does ...nothing, not good.

Posted: Fri Oct 06, 2006 6:07 pm
by akimm
It still runs with error_reporting(E_ALL);

I believe this if is the prob

Code: Select all

if ($diff < 10) {
I always try to debug it with my php program, 'PHP Designer 2005' it doesn't report any errors.
Nor does my webbie when I FTP it.

Posted: Fri Oct 06, 2006 6:30 pm
by volka
akimm wrote:I believe this if is the prob

Code: Select all

if ($diff < 10) {
meaning
echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "clicking here " .

"</a>";
is executed?

Posted: Fri Oct 06, 2006 7:34 pm
by Ollie Saunders
For the benefit of others:

Code: Select all

$img = '<img src=http://www.akimm.com/images/new1.gif>';
$path = 'article/';
if(is_dir($path)) {
    $handle = opendir($path);
    while ($file = readdir($handle)) {
        if (is_file($path.$file))  {
            $diff = round((time() - filectime($path.$file))/3600/24);
            if ($diff < 10) {
                echo '<ul><li>' . $img .  $file;
                echo '<a href=http://www.akimm.com/philosophy.php>Click to view</a>';
                echo '</li></ul>';
                break;
            } else {
                echo '<br /><br />no new articles, add one by';
                echo '<a href=http://www.akimm.com/add_art.php>clicking here</a>';
                break;
                closedir($handle);
            }
        }
    }
}
Do not comment by explaining what standard functions do, any Joe knows that and if they don't they can use the PHP manual which will do a lot better job. Also indent your code probably there's no way you can keep track of your logic without doing so. Also you were concatenating strings for no reason.

Code: Select all

$str = 'Hello, ' . 'world';
$str = 'Hello, world';
Which is easier to read? Constructive bitching over.

As for your problem:

Code: Select all

print_r(time());
print_r(filectime($path.$file));
Have a sneeky peek as to what is really going on.

Posted: Fri Oct 06, 2006 7:46 pm
by akimm
That fix doesn't work, IF I applied it correctly.

What I did.

Code: Select all

$diff = (print_r(round((time() - filectime($path.$file))))/3600/24);
No errors, just still not printing what it should be. Did I misunderstand you?

Thanks.

Posted: Fri Oct 06, 2006 7:46 pm
by akimm
Volka, yes thats how I intended it to be.