简体   繁体   中英

PHP: While loop condition

$result= db_query("SELECT dayoff FROM specificdayoff");
$node->frmdate = mktime(0,0,0, $node->frmdate['month'], $node->frmdate['day'],$node->frmdate['year']);
$node->todate = mktime(0,0,0, $node->todate['month'], $node->todate['day'],$node->todate['year']);
$node->dayoff = mktime(0,0,0, $node->dayoff['month'], $node->dayoff['day'],$node->dayoff['year']);
$data = $node->dayoff;
$frmdate = $node->frmdate;
$todate = $node->todate;    

$iii=0;
while($frmdate!=$todate)
{
    while($data= db_fetch_object($result))
    {
        if($frmdate==$data)
        {
            $iii++;
           //debug
        }

    }
    $frmdate=mktime(0, 0, 0, date("m", $frmdate), date("d", $frmdate)+1, date("Y", $frmdate));
    //debug2     
   }
$diff=$iii

I need every element of $frmdate to be compared with all element of $data . but when I run this code, $diff keep returning 0. meaning to say, there's something wrong with my if statement, but I can't seem to figure out what is wrong with it.

when I debug this block of code, both while loop did not run as i want it. the 2nd while loop only run once. I need the 2nd while loop to be run every time 1st while loop is running. any help?

the debug run like this: debug debug debug debug2 debug2 debug2

i need it to run like this: debug debug debug debug2 debug debug debug debug2 debug debug debug debug2

You said the problem is this:

while($data= db_fetch_object($result))

What's happening is that after running through this loop once, you'll fetch all of the results, so there are none left. Going through the loop again, there's still no results because you already fetched them all. You need to set up $result again. You didn't include this, code but look for a line that looks like $result = ... .

Something like this:

$node->frmdate = mktime(0,0,0, $node->frmdate['month'], $node->frmdate['day'],$node->frmdate['year']);
$node->todate = mktime(0,0,0, $node->todate['month'], $node->todate['day'],$node->todate['year']);
$node->dayoff = mktime(0,0,0, $node->dayoff['month'], $node->dayoff['day'],$node->dayoff['year']);
$data = $node->dayoff;
$frmdate = $node->frmdate;
$todate = $node->todate;    

$iii=0;
while($frmdate!=$todate)
{
    $result= db_query("SELECT dayoff FROM specificdayoff");
    while($data= db_fetch_object($result))
    {
        if($frmdate==$data)
        {
            $iii++;
        }
    }
    $frmdate=mktime(0, 0, 0, date("m", $frmdate), date("d", $frmdate)+1, date("Y", $frmdate));  
}
$diff=$iii

$frmdate is a simple PHP timstamp, created by mktime(). You then compare this timestamp value agains an OBJECT created by a database fetch statement. A timestamp and a DB fetch object can never be equal, so your while loop's $iii++ can never get triggered.

I get the feeling you have a timestamp in your specificdayoff table. If this is the case, then your db_fetch_object method will retrieve an object that has dayoff property.

To access this you would use $data->dayoff . So you need to change the condition of the if($frmdate==$data) to if($frmdata == $data->dayoff)

This is all a guess without knowing exactly what your db_fetch_object will return. That said, I still don't follow exactly what is supposed to be happening here, and this may just be one of a few problems you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM