douxinghuai3150 2018-08-26 04: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 12: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部