douxinghuai3150 2018-08-26 12:28 采纳率: 0%
浏览 38
已采纳

php mysqli事件按日期获取

I have made a code where I get all nubers in the month then I am adding on events from my database. I need the numbers to come from the database in $sqldates I am now only getting array as when I echo the $sqldates. I have 4, 10, 22 and 26 in my database now.

This is what I am getting now look at the picture enter image description here

This is the result I want look at this picture.

enter image description here

How do I get the results from my database as picture 2 shows? Pleas help how to get the numbers from array.

    <table>
    <?php
     //database connent
        include 'con.php'; 
      //get day from event
   $sql = "SELECT day, color FROM events";
   $result = mysqli_query($con, $sql);



$sqldates = array(array('day', 'color_value'), array('date_value', 'color_value'), array('date_value', color_value));
while($row=mysqli_fetch_array($result)){   
array_push($sqldates, $row['day'], $row['color'] );
echo $row['day'];

} 
$counter = 0;
//first day
  $firstday = '1';

  $two = cal_days_in_month(CAL_GREGORIAN, 8, 2018); // 31
//for loop get all day in month  
  for ($number = $firstday; $number <= $two; $number++)


    if (in_array($number , $sqldates)) {
        echo "<td width=50 bgcolor='#{$sqldates[$counter][1]}'>$sqldates[$counter][0]</td>";
        $counter++;
      } else {
        echo"<td width=50 bgcolor='#1e8e8e'>$number</td>";
      }        
    ?>
    </table> 
  • 写回答

2条回答 默认 最新

  • dongsheng4679 2018-08-26 20:05
    关注

    For your updated question this should be good guide:

    <table>
            <?php
             //database connect
                include 'con.php'; 
              //get day from event
              $sql = "SELECT day, color FROM events";
              $result = mysqli_query($con, $sql);
    
              $sqldates = array();
              $colors = array();
              while($row=mysqli_fetch_array($result)){   
                  array_push($sqldates, $row['day']);
                  array_push($colors, $row['color']);
              }
    
              //first day
               $firstday = '1';
    
              $two = cal_days_in_month(CAL_GREGORIAN, 8, 2018); // 31
              //for loop get all day in month  
               $counter = 0;
              for ($number = $firstday; $number <= $two; $number++)
    
              if (in_array($number , $sqldates)) {
                  echo "<td width=50 bgcolor='$colors[$counter]'>$sqldates[$counter]</td>";
                  $counter++;
                } else {
                  echo"<td width=50 bgcolor='#1e8e8e'>$number</td>";
              }
          ?>
        </table> 
    

    Note that you need to have defined color for each day, otherwise you will get "Undefined offset" notice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装