普通网友 2019-06-05 18:10
浏览 281
已采纳

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

I have a for loop that displays all the days in a month. I also have a table that has various dates stored in it. I want to be able to query that table to select total count of all records of each date in that month and display it on each date in the results of the for loop.

Don't know how to get it done.

I think the problem is in the query construction but i can't figure out the best way to go about it to achieve the aim.

For example, for day one of month June, i want to see 5 slots. for day two, 10 slots, etc

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

  • duan198123 2019-06-06 09:56
    关注

    I solved the issue. So in case anyone else in the future encounters this problem, what i did was i amended the query to the database. The issue was with the query and its call position. It was outside the for loop. So i included it in the for loop like below:

     <?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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?