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条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)