如何查询表中的所有行?

时间:2018-06-02 21:41:12

标签: php mysqli

我有一个必须标记拍摄日期的日历。例如,我在表中有3条记录,但只添加了最后添加的最后一条记录。如何显示所有行?以下是问题的解决方法:

http://prntscr.com/jq25ax

这是我用来显示日期的查询。那么如何让它显示所有拍摄日期?我是否必须在直到num_row的for循环中进行查询?

<?php

function getCalendar($year = '', $month = '')
{
    $dateYear = ($year != '') ? $year : date("Y");
    $dateMonth = ($month != '') ? $month : date("m");
    $date = $dateYear . '-' . $dateMonth . '-01';
    $currentMonthFirstDay = date("N", strtotime($date));
    $totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN, $dateMonth, $dateYear);
    $totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7) ? ($totalDaysOfMonth) : ($totalDaysOfMonth + $currentMonthFirstDay);
    $boxDisplay = ($totalDaysOfMonthDisplay <= 35) ? 35 : 42;
?>
        <div id="calender_section">
            <h2>
                <a id="prev" href="#" onclick="return false" onmousedown="javascript:swapContent('prev')"><span class="glyphicon glyphicon-chevron-left"></span></a>
                <select name="month_dropdown" class="month_dropdown dropdown"><?php
    echo $this->getAllMonths($dateMonth); ?></select>
                <select name="year_dropdown" class="year_dropdown dropdown"><?php
    echo $this->getYearList($dateYear); ?></select>
                <a id="next" href="#" onclick="return false" onmousedown="javascript:swapContent('next')"><span class="glyphicon glyphicon-chevron-right"></span></a>
            </h2>
            <div id="event_list" class="none"></div>
            <div id="calender_section_top">
                <ul>
                    <li>Mon</li>
                    <li>Tue</li>
                    <li>Wed</li>
                    <li>Thu</li>
                    <li>Fri</li>
                    <li>Sat</li>
                    <li>Sun</li>
                </ul>
            </div>
            <div id="calender_section_bot">
                <ul>
                <?php
    $dayCount = 1;
    for ($cb = 1; $cb <= $boxDisplay; $cb++) {
        if (($cb >= $currentMonthFirstDay || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay - 1)) {

            // Current date

            $currentDate = $dateYear . '-' . $dateMonth . '-' . $dayCount;
            $currentDate = strtotime($currentDate);
            $eventNum = 0;

            // Include db configuration file
            // Get number of events based on the current date

            $sql = ("SELECT COUNT(book2_id), date_from, date_to FROM booking_2 GROUP BY date_from ORDER BY COUNT(book2_id) DESC");
            $result = $this->connect()->query($sql);
            $eventNum = $result->num_rows;
            if ($eventNum > 0) {
                while ($row = $result->fetch_assoc()) {
                    $date_from = $row['date_from'];
                    $date_to = $row['date_to'];
                    $time_from = strtotime($date_from);
                    $time_fromm = idate('d', $time_from);
                    $time_to = strtotime($date_to);
                    $time_too = idate('d', $time_to);
                    if ($currentDate >= $time_from && $currentDate <= $time_to) {
                        echo '<li  style="background-color:#FF3232 !important;" date="' . $currentDate . '" class="date_cell"><span>' . $dayCount . '</span>';
                    }
                    else {
                        echo '<li date="' . $currentDate . '" class="date_cell"><span>' . $dayCount . '</span>';
                    }
                }
            }
            else {
                echo '<li date="' . $currentDate . '" class="date_cell"><span>' . $dayCount . '</span>';
            }

            // Date cell

            echo '</li>';
            $dayCount++;
?>
                <?php
        }
        else { ?>
                    <li><span>&nbsp;</span></li>
                <?php
        }
    } ?>
                </ul>
            </div>
        </div>
<?php
} ?>

1 个答案:

答案 0 :(得分:1)

在输出数据之前结束while循环。尝试将结束括号向下移动到您回显信息的部分,如下所示:

$sql = ("SELECT * FROM booking_2 ORDER BY book2_id");
$result = $this->connect()->query($sql); 
$eventNum = $result->num_rows;
if($eventNum > 0){
  $previousDate = 0;
  while($row = $result->fetch_assoc()){
    $date_from = $row['date_from'];
    $date_to = $row['date_to'];
    $time_from = strtotime($date_from);
    $time_fromm = idate('d', $time_from);
    $time_to = strtotime($date_to);
    $time_too = idate('d', $time_to);

    if (date("Y-m-d", $time_from) != date("Y-m-d", $previousDate)) {
        if ($currentDate >= $time_from && $currentDate <= $time_to) {
           echo '<li id="'.$row['book2_id'].'" style="background-color:#FF3232 !important;" date="'.$currentDate.'" class="date_cell"><span>'.$dayCount.'</span>';  
        }else{
           echo '<li date="'.$currentDate.'" class="date_cell"><span>'.$dayCount.'</span>';
        }
    }
    $previousDate = $time_from;
  }
}else{
    echo '<li date="'.$currentDate.'" class="date_cell"><span>'.$dayCount.'</span>';
}