Php 如何查询表中的所有行?

Php 如何查询表中的所有行?,php,mysqli,Php,Mysqli,我有一个日历,上面必须标出日期。例如,表中有3条记录,但只有最近添加的最后一条记录保持显示。如何显示所有行?问题是这样的: 这是我用来显示日期的查询。那么我如何让它显示所有的日期呢?我是否必须在for循环中进行查询,直到num\u row <?php function getCalendar($year = '', $month = '') { $dateYear = ($year != '') ? $year : date("Y"); $dateMonth = ($m

我有一个日历,上面必须标出日期。例如,表中有3条记录,但只有最近添加的最后一条记录保持显示。如何显示所有行?问题是这样的:

这是我用来显示日期的查询。那么我如何让它显示所有的日期呢?我是否必须在for循环中进行查询,直到
num\u 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
} ?>

  • 周一
  • 星期二
  • 结婚
  • 周四
  • 星期五
  • 太阳

在输出数据之前结束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>';
}
$sql=(“按book2\u id从book2订单中选择*);
$result=$this->connect()->query($sql);
$eventNum=$result->num\u行;
如果($eventNum>0){
$previousDate=0;
而($row=$result->fetch_assoc()){
$date_from=$row['date_from'];
$date_to=$row['date_to'];
$time\u from=strottime($date\u from);
$time\u from=idate('d',$time\u from);
$time\u to=strotime($date\u to);
$time\u too=idate('d',$time\u to);
如果(日期(“Y-m-d”$time\u from)!=日期(“Y-m-d”$previousDate)){

如果($currentDate>=$time\u从&&$currentDate您的如果..“回音”是在循环之后…所以是的,只有一个回音。可能你想让它们在循环中。美化你的代码,看看你在做什么。什么是美化?问题是:谷歌是什么?这不是第一件要做的事,或者我这么想很奇怪吗?你在这段代码中到底不明白什么?if表达式中的else语句让人困惑吗ou?很抱歉,如果是这样。这会产生一个新问题,相信我,我已经尝试过了。这是我在WHILE循环中包含if语句时得到的输出:。它只是根据记录的数量复制所有日期。请不要生气。是的,我已经尝试过了,但当我这样做时,天数只是重复。例如,如果有2条记录,则有两条记录将是两个相同的日期。如果我在while循环中包含if语句,这是一张图片:对。我明白你的意思。修复这一问题需要额外检查,以查看日期是否已被标记。让我更新答案以包含该日期。哦,太简单了,谢谢!但是我现在无法用代码检查这一点。早上,对于误解,我感到非常抱歉,但您提供的解决方案仍然输出相同的错误。当我查看您的编辑时,我认为这将修复错误。但现在在测试时,解决方案似乎仍然没有结果。对此表示歉意:/n那么,我们将需要您的更多代码来解决问题。对于instance,我想知道$currentDate和$dayCount的确切位置,因为它们从未在您的代码段中设置。如果它们在实际使用时仅用于前进,那么我发送的解决方案将不起作用,因为它跳过了它们使用的部分。