Help with if and date stuff :)

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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Help with if and date stuff :)

Post 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";
}
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post 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;
?>
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post 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!
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

Its ok, I've managed to sort it all out with your tips :)

Thanks :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply