Page 1 of 1

Help with if and date stuff :)

Posted: Fri Aug 16, 2002 12:32 pm
by DynamiteHost
Can anyone tell me what I am doing wrong here?

At the moment it just prints "Nothing" twice.

Any ideas?

Code: Select all

$datemonth = date("n");
$dateday = date("j");
$dateyear = date("Y");

if ($viewїmonth] == "$datemonth") {
if ($viewїday] == "$dateday") {
if ($viewїyear] == "$dateyear") {

print $viewї'username'];

}
}
}
print "Nothing";
}

Posted: Fri Aug 16, 2002 12:40 pm
by Johnm
Is this all in a loop that you did not show?
You have 3 opening braces and four closing braces.
If you can, show a little more code.

Direwolf

Posted: Fri Aug 16, 2002 12:42 pm
by DynamiteHost
sorry, here you go :)

Code: Select all

<?

$query = mysql_query("SELECT * FROM cdc_clients");
	while ($view = mysql_fetch_array($query)) &#123;
$datemonth = date("n");
$dateday = date("j");
$dateyear = date("Y");
if ($view&#1111;paymentduemonth] == "$datemonth") &#123;
if ($view&#1111;paymentdueday] == "$dateday") &#123;
if ($view&#1111;paymentdueyear] == "$dateyear") &#123;
print $view&#1111;'username'];
&#125;
&#125;
&#125;
else &#123;
print "No clients still to pay";
&#125;
&#125;
?>

Posted: Fri Aug 16, 2002 12:57 pm
by Johnm
It is good practice if not mandatory to use quotes in your database vars.
I work with informix where quotes are required.
Try this to ensure that you are getting the expected output from the database. It looks as though you go through the while loop twice but never satisfy all three conditions necessary to print the username.

Code: Select all

<? 

$query = mysql_query("SELECT * FROM cdc_clients"); 

while ($view = mysql_fetch_array($query)) &#123; 
    $datemonth = date("n"); 
    $dateday = date("j"); 
    $dateyear = date("Y"); 

    echo $view&#1111;'paymentduemonth'];
	echo $view&#1111;'paymentdueday'];
	echo $view&#1111;'paymentdueyear'];
	
	if ($view&#1111;'paymentduemonth'] == "$datemonth") 
	&#123; 
        if ($view&#1111;'paymentdueday'] == "$dateday") 
		&#123; 
            if ($view&#1111;'paymentdueyear'] == "$dateyear") 
			&#123; 
                print $view&#1111;'username']; 
            &#125; 
        &#125; 
    &#125; 
    else 
	&#123; 
        print "No clients still to pay"; 
    &#125; 
&#125; 
?>
Direwolf

Posted: Fri Aug 16, 2002 1:08 pm
by DynamiteHost
I do normally include qoutes in my db vars, but I couldn't think of anything else that could be wrong, so I thought maybe it could be that, obviously i was wrong :oops:

I found out what it was, $dateday should have been $datemonth and vice versa.

Only problem is, its still printing "No Clients To Pay" even when there are usernames to print.

Its got to be something to do with the else.... i think!

Posted: Fri Aug 16, 2002 1:35 pm
by Johnm
Use flags in each condition to find out where the problem lies. Also, echo out the $query and insert it nativly into the database (if you can) to see exactly what that query returns.
Also, something else to check: is the data stored as strings or ints in the database?
<?

$query = mysql_query("SELECT * FROM cdc_clients");
echo $query."\n";

while ($view = mysql_fetch_array($query)) {
$datemonth = date("n");
$dateday = date("j");
$dateyear = date("Y");

echo $view['paymentduemonth'];
echo $view['paymentdueday'];
echo $view['paymentdueyear'];

if ($view[paymentduemonth] == "$datemonth")
{
echo "Flag 1\n";
if ($view[paymentdueday] == "$dateday")
{
echo "Flag 2\n";
if ($view[paymentdueyear] == "$dateyear")
{
echo "Flag 3\n";
print $view['username'];
}
}
}
else
{
print "No clients still to pay";
}
}

Direwolf

Posted: Fri Aug 16, 2002 2:05 pm
by DynamiteHost
Its ok, I've managed to sort it all out with your tips :)

Thanks :D

Posted: Sat Aug 17, 2002 11:02 am
by twigletmac
You can also have multiple conditions in an if statement so:

Code: Select all

if ($view&#1111;'paymentduemonth'] == "$datemonth") 
   &#123; 
        if ($view&#1111;'paymentdueday'] == "$dateday") 
      &#123; 
            if ($view&#1111;'paymentdueyear'] == "$dateyear") 
         &#123;
becomes

Code: Select all

if ($view&#1111;'paymentduemonth'] == $datemonth && $view&#1111;'paymentdueday'] == $dateday && $view&#1111;'paymentdueyear'] == $dateyear) &#123;
Don't need double quotes around variables either.

Mac