在for循环中显示一个月中每一天的记录数,以显示一个月中的某天

时间:2019-06-05 18:10:25

标签: php

我有一个for循环,显示一个月中的所有日子。我还有一个表格,其中存储了各种日期。我希望能够查询该表以选择该月中每个日期的所有记录的总数,并将其显示在for循环的结果中的每个日期。

不知道如何完成它。

我认为问题出在查询构造中,但我无法找出实现目标的最佳方法。

例如,对于六月的第一天,我想查看5个广告位。第二天,10个广告位等等

$ucimslots = "25";
$curyear = date("Y");
$month = $_POST['month'];
$day = date('j');
$daysinmonth = date('t',mktime(0,0,0,$month,1,$curyear));


$query = "SELECT uciID, bookingdate, paymentstatus FROM ucibooking WHERE bookingdate=?";
$stmt = $connQlife->prepare($query);
$stmt->bind_param('s', $day);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($uciID, $bookingdate, $paymentstatus);
$stmt->fetch();
$totalnumrows = $stmt->num_rows;
$stmt->close();


if ($totalnumrows!=0){
    $availableslots = $ucimslots - $totalnumrows;
}else if ($totalnumrows==0){
    $availableslots = $ucimslots;
}


<?Php for($day = 1; $day <= $daysinmonth; $day++){?>            
   <div class="calendarcont">
       <div class="calendarheadercont">
           <div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$curyear)); ?></div>
           </div>
           <div class="clear_1"></div>
           <div class="calendarsubcont">
               <div class="calendardatecont">
                   <div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$curyear)); ?></div>
                   <div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$curyear)); ?></div>
               </div>
               <div class="calendartextcont">
               <div class="calendartextr"><?Php echo $availableslots; ?></div>
           </div>
       </div>                                           
    </div>
   <?Php } ?> 

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。因此,以防万一将来其他任何人遇到此问题,我所做的就是将查询修改为数据库。问题出在查询及其呼叫位置。它在for循环之外。所以我将其包含在如下所示的for循环中:

 <?Php for($day = 1; $day <= $daysinmonth; $day++){
    $query = "SELECT uciID, bookingdate, paymentstatus FROM ucibooking WHERE YEAR(bookingdate)=? AND MONTH(bookingdate)=? AND DAY(bookingdate)=?";
    $stmt = $connQlife->prepare($query);
    $stmt->bind_param('sss', $curyear, $month, $day);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($uciID, $bookingdate, $paymentstatus);
    $stmt->fetch();
    $totalnumrows = $stmt->num_rows;
    $stmt->close();?>

       <div class="calendarcont">
           <div class="calendarheadercont">
               <div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$curyear)); ?></div>
               </div>
               <div class="clear_1"></div>
               <div class="calendarsubcont">
                   <div class="calendardatecont">
                       <div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$curyear)); ?></div>
                       <div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$curyear)); ?></div>
                   </div>
                   <div class="calendartextcont">
                   <div class="calendartextr"><?Php echo $availableslots; ?></div>
               </div>
           </div>                                           
        </div>
 <?Php } ?>