Help me debug this php/mysql statment

Soldato
Joined
9 May 2005
Posts
7,400
Location
Berkshire
I've been staring at this bit of php & sql for ages now, can any one see what's wrong with it?
Code:
<?php
if ($myrow['completed'] == "1")
 echo "Complete: ";
elseif ($myrow['date'] < $curdate && $myrow['completed'] == "0")
 echo "Overdue";
elseif ($myrow['completed'] == "2")
echo "Working: ";
elseif ($myrow['completed'] == "3")
echo "Rejected: ";
else
echo "Open";
 echo ($myrow['compby']);
?>

It's designed to show the user if a task they have to do is either Complete, Overdue, Working, Rejected or Open. Problem is it's showing Open where it should be showing Overdue.

I'm sure it's a straight forward fix but I'm really not seeing it. Any ideas?
 
Last edited:
not really done much with PHP, but in ASP I'd suspect it's to do with the variable types for comparison, eg $myrow['completed'] = 2 could be true as they are both ints, however $myrow['completed'] = "2" could be false as the db value could be an int and "2" is a string. Because all previous tests are false, the else procedure is called.

You'd need to cast them both to the same type to make a valid comparison.
 
Code:
<?php
if ($myrow['completed'] == 1) {
 echo "Complete: ";
} elseif (($myrow['date'] < $curdate) && ($myrow['completed'] == 0)) {
 echo "Overdue";
} elseif ($myrow['completed'] == 2) {
 echo "Working: ";
} elseif ($myrow['completed'] == 3) {
 echo "Rejected: ";
} else {
 echo "Open" . $myrow['compby'];
}
?>
 
space between else and if?

personally I would right it like this, but it is just preference

Code:
<?php
if ($myrow['completed'] == "1")
{
 echo "Complete: ";
}
else if ($myrow['date'] < $curdate && $myrow['completed'] == "0")
{
 echo "Overdue";
}
else if ($myrow['completed'] == "2")
{
 echo "Working: ";
}
else if ($myrow['completed'] == "3")
{
echo "Rejected: ";
}
else
{
 echo "Open";
}

 echo $myrow['compby'];
?>
 
Jonno I tried your suggestion and it's behaving exactly the same as how I had it before. To add to that it's also no longer displaying who last handled the task.
 
Last edited:
you need to just check the "overdue" bit. forget the rest of the code for now.....

Code:
if (($myrow['date'] < $curdate) && ($myrow['completed'] == 0)) {
 echo "Overdue";
}

if that doesn't work, there's something wrong with your logic. :p
 
All of the things people have posted are identical to the OP :/

var_dump both $myrow and $curdate to check that they're as you'd expect. If all your checks are failing (which they are, because you're dropping through to your else) it's probably a scope issue, or a problem with your SQL, or something along those lines. There doesn't appear to be anything wrong with the code you posted.
 
Try using a switch.. would help readability a bit at least.
Code:
switch ($myrow['completed'])
  case 0:
    if ($myrow['date'] < $curdate) echo 'Overdue';
    break;

  case 1: 
    echo 'Complete';
    break;

  case 2:
    echo 'Working';
    break;

  case 3:
    echo 'Rejected';
    break;

  default:
    echo 'Open';
    break;
}

echo $myrow['compby'];
and as Rob points out, you need to check you are receiving those values for a start. Seeing your code wrapped in <?php ?> and nothing else would indicate they are not defined.
 
Back
Top Bottom