duankan8739 2018-06-02 21:41
浏览 20

如何查询表中的所有行?

I have a calendar that has to mark-up taken dates. For example I have 3 records in the table but only the last the most recent record added keeps displaying. How do I make all the rows show? Here is how the problem looks:

http://prntscr.com/jq25ax

And this is the query that I use to display the dates. So how do I make it show all the taken dates? Do I have to make the query in a for loop that goes until num_row?

<?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条回答 默认 最新

  • dongtui0650 2018-06-02 21:58
    关注

    You end the while-loop before outputting the data. Try moving the ending bracket down past the section where you echo the info, like this:

    $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>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度