drmeu26880 2014-06-12 11:16
浏览 21
已采纳

多个ResultSet变量PHP

On my database I have the table reserves, on the reserves I have inserted the following data:

enter image description here

Then I used the following query(this is just one part):

    $checkInresult = mysql_result(mysql_query("SELECT DAYOFMONTH(`checkIn`) FROM `reserves` WHERE `idApart` = 1 AND `expired` = 0"), 0,0);
    $checkOutresult = mysql_result(mysql_query("SELECT DAYOFMONTH(`checkOut`) FROM `reserves` WHERE `idApart` = 1 AND `expired` = 0"), 0,0);
    $curDay = mysql_result(mysql_query("SELECT DAYOFMONTH(CURDATE())"), 0,0);
    if((1 == $curDay) && (1 >= $checkInresult) && (1 <= $checkOutresult)){
        $day1 = "currentO";
    }elseif(1 == $curDay){
        $day1 = "currentA";
    }elseif((1 >= $checkInresult) && (1 <= $checkOutresult)){
        $day1 = "occupiedDay";
    }else{
        $day1 = "availableDay";
    }

And the html(only one part too):

<div id="calendar">
  <h1>June</h1>
  <table>
    <tr>
        <td class="<?php echo $day1; ?>"><center>1</center></td>
        <td class="<?php echo $day2; ?>"><center>2</center></td>
        <td class="<?php echo $day3; ?>"><center>3</center></td>
        <td class="<?php echo $day4; ?>"><center>4</center></td>
        <td class="<?php echo $day5; ?>"><center>5</center></td>
        <td class="<?php echo $day6; ?>"><center>6</center></td>
        <td class="<?php echo $day7; ?>"><center>7</center></td>
    </tr>

And the result is this:

enter image description here

And I want the 2nd data to apear marked with red too without making too many ifs like same variable contains the checkIn and checkOut data from both datas. Is it possible? Or is it too confusing? :s

  • 写回答

2条回答 默认 最新

  • douao7937 2014-06-12 11:53
    关注

    I would do:

    $curDay = mysql_result(mysql_query("SELECT DAYOFMONTH(CURDATE())"), 0, 0);
    $result = mysql_query("SELECT DAYOFMONTH(`checkIn`) AS checkIn, DAYOFMONTH(`checkOut`) AS checkout FROM `reserves` WHERE `idApart` = 1 AND `expired` = 0");
    
    // initialize all days as available
    for ($i = 1; $i <= 30; $i++) {
        $days[$i] = 'availableDay';
    }
    
    // mark the current day
    $days[$curDay] = 'currentA';
    
    while ($row = mysql_fetch_array($result)) {
        $checkInresult = $row['checkin'];
        $checkOutresult = $row['checkout'];
    
        // run through occupied days
        for ($i = $checkInresult; $i <= $checkOutresult; $i++) {
            if ($i == $curDay) {
                // we hit the current day again, so mark as occupied
                $days[$i] = 'currentO';
            } else {
                // occupy any other day
                $days[$i] = 'occupiedDay';
            }
        }
    }
    

    For output use:

    <td class="<?php echo $days[1]; ?>"><center>1</center></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题