while和for循环不工作 [英] While and for loop not working

查看:173
本文介绍了while和for循环不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我认为有一个非常简单的解决方案,但我现在忙了12个小时,我不知道这个问题的解决方案。



我有一个具有多个数据的数据库。重要的数据是我保存在数据库中的时间。我想建立一个给出数据08:00至17:00的模块。例如:

 
08:00可用
09:00不可用
10:00可用
11:00可用
12:00可用
13:00可用
14:00可用
15:00可用
16:00可用
17: 00不可用

我将使用以下代码

  $ mysql ['avail'] = mysql_query(SELECT time FROM`module` WHERE`date` ='。$ dbdate。'ORDER BY date); 
while($ avail = mysql_fetch_assoc($ mysql ['avail'])){

$ hour = date('s',$ avail ['time']); ($ i = 8; $ i $ = 17; $ i ++)

{

if($ hour == $ i){

echo $ i。':00& nbsp; not available< br />';

} else {


echo $ i。':00& nbsp; available< br />';

}

}

}

现在我得到以下输出:

 
08:00可用
09:00不可用
10:00可用
11:00可用
12:00可用
13:00可用
14:00可用
15:00可用
16:00可用
17:00可用

08:00可用
09:00可用
10:00可用
11:00可用
12:00可用
13:00可用
14:00可用
15:00可用
16:00可用
17:00不可用

无论我尝试什么,我没有办法解决问题。有没有人可以帮助我?

解决方案

您必须先获取所有可用的时间,然后与您的时间表,并检查每小时是否在可用的时间阵列。



这样的一个

  $ not_available_hours = array(); 
$ mysql ['avail'] = mysql_query(SELECT time FROM`module` WHERE`date` ='。$ dbdate。'ORDER BY date);
while($ avail = mysql_fetch_assoc($ mysql ['avail'])){
$ not_available_hours [] = date('s',$ avail ['time']);
}

($ i = 8; $ i = 17; $ i ++){
if(in_array($ i,$ not_available_hours){
echo $ i。':00& nbsp; not available< br />';
} else {
echo $ i。':00& nbsp; available< br />';
}
}


I have a question and I think there is a really easy solution for, but i'm busy for 12 hours now and I don't know the solution for this problem.

I have a database with several data. The important data is the time that I save in the database. I want to build a module that give the data 08:00 to 17:00. For example:

08:00 available
09:00 not available
10:00 available
11:00 available
12:00 available
13:00 available
14:00 available
15:00 available
16:00 available
17:00 not available

I'll using the following code

$mysql['avail'] = mysql_query("SELECT time FROM `module` WHERE `date` = '" . $dbdate . "' ORDER BY date");
while($avail = mysql_fetch_assoc($mysql['avail'])){

  $hour = date('s',$avail['time']);

  for ($i = 8;$i <= 17; $i++) {

    if($hour == $i) {

        echo $i.':00&nbsp;not available<br />';

    } else {


        echo $i.':00&nbsp;available<br />';

    }

  } 

}

Now I get the following output:

08:00 available
09:00 not available
10:00 available
11:00 available
12:00 available
13:00 available
14:00 available
15:00 available
16:00 available
17:00 available

08:00 available
09:00 available
10:00 available
11:00 available
12:00 available
13:00 available
14:00 available
15:00 available
16:00 available
17:00 not available

Whatever I try, I've no solution to get it right. Is there anybody that can help me?

解决方案

You have to fetch all your available hours at first and then make one loop with your timetable and check for each hour if it is in available hours array.

Something like this one

$not_available_hours = array();
$mysql['avail'] = mysql_query("SELECT time FROM `module` WHERE `date` = '" . $dbdate . "' ORDER BY date");
while($avail = mysql_fetch_assoc($mysql['avail'])){
    $not_available_hours[] = date('s',$avail['time']);
}

for ($i = 8;$i <= 17; $i++) {
    if (in_array($i, $not_available_hours) {
        echo $i.':00&nbsp;not available<br />';
    } else {
        echo $i.':00&nbsp;available<br />';
    }
}

这篇关于while和for循环不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆