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

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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

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

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

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Last edited by Ollie Saunders on Fri Oct 06, 2006 8:42 pm, edited 1 time in total.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Volka, yes thats how I intended it to be.
Post Reply