1
$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

4
  • It also looks like you are comparing an object ($data) to an integer (timestamp $frmdate). I would check what you are expecting from db_fetch_object Commented Oct 12, 2011 at 1:55
  • @BrendanLong cuz i do not want any addition to $iii when $frmdate not equal to $data Commented Oct 12, 2011 at 1:58
  • @Cooper - So just leave the else out. You don't need one. if statements are perfectly fine alone. Commented Oct 12, 2011 at 2:00
  • @phindmarsh do u know any way i could compare these two variable ($data, $frmdate). i expect db_fetch_object would fetch the 'dayoff' field in the database. the 'dayoff' field contains timestamp. i need to compare this dayoff field with $frmdate. Commented Oct 12, 2011 at 2:01

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

omg! it work! you're the man. $data->dayoff could change $data to integer! my if statement are workin now. thanks for the help man.
@Cooper, no problem, I'm glad it helped. As mentioned above, if this or the other solution (by @Brendan) solves your problem, please mark it as accepted.
both answer solves my problems. so i should mark both as accepted. right? sorry. a newbie here.
1

$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.

1 Comment

no wonder i get 0 all the time. I've find the way to convert the fetch_object into timestamp so that it could be compared with $frmdate! thanks mate!
1

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

3 Comments

so, is there any function or way for me to make 2nd loop run everytime 1st loop is runnin? i already add variable declaration, query and debug result in the block of code.
@Cooper - Set $result inside the outer loop.
ohh. done! sorry. im still new. thanks for the help again brendan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.