drmeu26880 2014-06-12 11:16 采纳率: 0%
浏览 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条)

报告相同问题?