简体   繁体   中英

While loop only runs once although it should run more than once

I have a database table which stores which days (Monday to Sunday) are working days and which are not. The table includes 2 attributes, "day" (1, 2, 3, 4, 5, 6, 7 for Monday to Sunday) and "working". I use the numbers '0' and '1' to indicate if they are working days or not, '1' being non working day. I have set in the database for Friday, Saturday and Sunday to be nonworking (5, 6, 7).

$addrange = $annstart;
    $minusday = 0; //numbers of nonworking days

    $resultnonworking = mysql_query("SELECT dayid FROM workingdays WHERE working='1'");
    while ($nonworking = mysql_fetch_array($resultnonworking))
    {
        while ($datediffdays > 1)
            {
                $addrange = date('Y-m-d', strtotime($addrange . ' + 1 day'));
                $daycheck = date('N', strtotime($addrange));
                if ($daycheck == $nonworking['dayid'])
                    {
                        $minusday = ++$minusday;
                    }               
                $datediffdays = --$datediffdays;
            }
        $startdaycheck = date('N', strtotime($annstart));
        if ($nonworking['dayid'] == $startdaycheck)
        $minusday = ++$minusday;    
    }

However my while loop only runs once and checks which of the dates are Fridays, leaving out Saturday, and Sunday.

Can somebody spot what is wrong with my code? Thanks in advance.

Explanation to what you asked, your loop is as below.

while ($nonworking = mysql_fetch_array($resultnonworking))
{
    while ($datediffdays > 1)
    {

see it first fetches one record and then until the $datediffdays goes less then 1 it runs the inner loop. now when you come back to the first loop the value of $datediffdays is already less than 1 so it doesn't goes into the second loop. to make this workig you have to initialize $datediffdays inside first loop like,

while ($nonworking = mysql_fetch_array($resultnonworking))
{
    $datediffdays = 5;
    while ($datediffdays > 1)
    {

where as the changes that you made now, fetces the record inside the while loop and then it reduces the value of $datediffdays variable only once a loop and therefore it is working, because value of $datediffdays is not set to less than 1 the first time it comes inside the loop.

I can't suggest which will be the best approach as all of your code is not here

I've switched the inner while loop with the outer one, and with a little bit of changes, its working now. But I have no idea why it is working this way but not the other.

$addrange = date('Y-m-d', strtotime($annstart . ' - 1 day'));
    $minusday = 0; //numbers of nonworking days

    while ($datediffdays > 0)
        {
            $addrange = date('Y-m-d', strtotime($addrange . ' + 1 day'));
            $checkrange = date('N', strtotime($addrange));
            $resultnonworking = mysql_query("SELECT * FROM workingdays WHERE working='1'");
            while ($nonworkingcheck = mysql_fetch_array($resultnonworking))
                {
                    if($nonworkingcheck['dayid'] == $checkrange)
                        {

                            $minusday = $minusday + 1;

                        }
                }
            $range = $range." ".$addrange;
            $datediffdays = $datediffdays-1;
        }

    $range = substr($range,11); 

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