检查多行 - PHP& MySQL的

时间:2014-10-02 12:54:11

标签: php html mysql sql

我在HTML表格中循环使用PHP来为特定单元格着色。 我想为返回true的单元格着色:

if ($currentCell >= $reservationStartDayOfMonth && $currentCell <= $reservationEndDayOfMonth) {
    make red
} else {
    make normal color
}

这不是确切的陈述,而是它的想法。 我从MySQL服务器获取了reservationEndDayOfMonth和reservationStartDayOfMonth。 对于SQL Server中的每一行,都有一个带有开始和结束日期的新预留。随着id(1 ... 2 ... 3 ...)

基本上我在本月预订了。当我使用来自ONE预留的数据循环遍历单元格时,它就可以工作。但我的数据库中有很多保留。我需要检查并查看当前单元格是否与我所拥有的任何预订相对应。

我的代码冗长而笨重。它基本上只是一个包含所有检查器和参数的表。你走了:

<table border="1" id="calendar">


<?php

$dk_database = array("Januar","Februar","Marts","April","Maj","Juni","Juli","August","September","Oktober","November","December");
$dk_month = $dk_database[(int)date("m")-1];

ECHO '<caption><h1 style="color:#554100;">' . $dk_month . '</h1></caption>';



$sqldateStart = sqlRequest("SELECT res_datestart FROM spottrup_reservations WHERE res_id = 1");
$dateStartRaw = mysql_fetch_array($sqldateStart);
$dateStartArray = explode("-", $dateStartRaw[0]);
$dateStartDay = (int)$dateStartArray[2];
$dateStartMonth = (int)$dateStartArray[1];
$dateStartYear = (int)$dateStartArray[0];

$sqldateEnd = sqlRequest("SELECT res_dateend FROM spottrup_reservations WHERE res_id = 1");
$dateEndRaw = mysql_fetch_array($sqldateEnd);
$dateEndArray = explode("-", $dateEndRaw[0]);
$dateEndDay = (int)$dateEndArray[2];
$dateEndMonth = (int)$dateEndArray[1];
$dateEndYear = (int)$dateEndArray[0];

$currentYear = (int)date("y")+2000;
$currentDate = (int)date("d");
$currentMonth = (int)date("m");
$columnsInRow = 7;
$daysInMonth = date("t");

$currentDay = 1;
for ($i=0 ; $i < $daysInMonth ; $i++) {
    ECHO '<tr class="cal">';
    for ($j=0 ; $j < $columnsInRow && $currentDay <= $daysInMonth ; $j++) {
        if($currentDate==$currentDay) {
            if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
                ECHO '<div style="background:blue;height:100%;width:100%text-decoration:none;"><td class="cal"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style="color:red;">' . $currentDay . '</h2></a></td></div>';
            }else{
                ECHO '<div style="background:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style="color:red;">' . $currentDay . '</h2></a></td></div>';
            }
        }else{
            if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
                ECHO '<div style="background-color:blue;height:100%;width:100%text-decoration:none;"><td class="cal" style="background-color:#FF8080;"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style="color:#554100;">' . $currentDay . '</h2></a></td></div>';
            }else{
                ECHO '<div style="background-color:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style="color:#554100;">' . $currentDay . '</h2></a></td></div>';
            }
        }
        $currentDay++;
    }
    ECHO '</tr>';
}
?>

sqlRequest(string);调用一个返回你给出的查询结果的函数。

1 个答案:

答案 0 :(得分:0)

自从大量查询以来,它不是最有效的方法,但它确实减少了for循环,但您可以每天运行查询以查看是否有预留。易于实施。

for ($i=0 ; $i < $daysInMonth ; $i++) 
{
   ...
  $query = "Select * from table where currentDate >= startDate and currentDate <= endDate"
   ...
}