为了账号安全,请及时绑定邮箱和手机立即绑定

是否可以将所有 php 数据按星期几排序到各个 div 中

是否可以将所有 php 数据按星期几排序到各个 div 中

江户川乱折腾 2023-12-11 10:29:11
你好,我在这里遇到一个问题,我想用 php 从 mysql 检索数据(完成了:)),然后例如当我的变量等于 $dia_semana星期日时,它将把星期日的所有数据显示到具有特定 id 的特定 div 中,然后继续这是我的 php 代码:<?phpfunction dia_da_semana() {    $connect = mysqli_connect("", "", "", "");    $sql ="SELECT temperature, humidity, data FROM sensor ORDER BY data DESC LIMIT 1";    $result = mysqli_query($connect, $sql);    setlocale(LC_TIME, "pt_PT"); // or LC_TIME    while($row = mysqli_fetch_array($result)) {        echo strftime("%A",$row['data']);        echo "<h2>".$row['data']."</h2>";        echo "<h4>".$row['temperature']."ºC</h4>";        echo "<h4>".$row['humidity']."%</h4>";    }}dia_da_semana();?>我希望将其发送到的 html 代码的一部分:<div class="7days">    <?php        include "loadgraph.php"    ?>    <div class="day1">    </div>    <div class="day2">    </div></div>
查看完整描述

2 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

这是我的完全有效的代码:


$WEEKDAYS = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday");

          $ResultsArray = array();


          while($row = mysqli_fetch_array($result)) {

              // create an array to hold the return values from your DB

              $date = $row['dia_semana']; //<--- coming from db holds date values

              $ResultsArray[$date][] = $row['temperature'];

          }

            echo "<table style='padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;'>";

            echo "<tr>";


            foreach ($WEEKDAYS as $key => $date) {

              echo "<th>".$date."</th>";

            }

                echo "</tr>";

              for ($x = 0; $x < 1; $x++){


                  echo "<tr>";

                   echo "<td>".$ResultsArray['Sunday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Monday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Tuesday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Wednesday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Thursday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Friday'][$x]."</td>";

                   echo "<td>".$ResultsArray['Saturday'][$x]."</td>";

echo "</tr>";

              }

          echo "</table>";


查看完整回答
反对 回复 2023-12-11
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

定义一个常量/数组来与数组进行比较,并使用键值将星期几设置为从星期日开始。然后使用该常量/数组运行 foreach 来回显从周日开始的每一天的表数据。检查常量中的值是否在返回的数组中,如果是,则回显该天及其也设置为该天的数组中的相应值。


define("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);


$parseDays = array(

    'Frank' => 'Monday',

    'Jean' => 'Monday',

    'Mike' => 'Sunday',

    'Bob' => 'Tuesday',

    'Bill' => 'Friday',

    'Jack' => 'Sunday',

    'George' => 'Friday',

    'Dillon' => 'Wednesday'

);

$stmt = '<table>';

$stmt .= '<tr>';

foreach(WEEKDAYS as $key => $day){

    if(in_array($day, $parseDays)){

        if($day === $day){

            $stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';

            foreach($parseDays as $name => $days){

                if($days === $day){

                    $stmt .= '<br>'.$name;

                }

            }

            $stmt .= '</td>';

        }

    }       

}

$stmt .= '</tr>';


$stmt .= '</div>';

echo $stmt;

根据您的情况,请尝试以下操作:


// make sure to define a constant that has the days to compare so we can start

// with Sunday and display in order of week from Sunday through the rest of 

// the week concurrently

define("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);

while($row = mysqli_fetch_array($result)) { 

    // create an array to hold the return values from your DB

    $date[] = $row['dia_semana']; //<--- coming from db holds date values

}


// construct the table and display the days

$stmt = '<table>';

$stmt .= '<tr>';

//loop through the constant and get the days in order from Sunday concurrently

foreach(WEEKDAYS as $key => $day){

    // check if the $day value from constant is located in the $date array from db

    if(in_array($day, $date)){

        if($day === $day){

            $stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';

            // loop through db array and assign key/values

            foreach($date as $name => $days){

                // If value from db array is equal to value from constant, display it

                if($days === $day){

                    $stmt .= '<br>'. //enter name values here;

                }

            }

            $stmt .= '</td>';

        }

    }       

}

$stmt .= '</tr>';


$stmt .= '</div>';

echo $stmt;

输出:

https://img1.sycdn.imooc.com/6576742f00015f6a06460211.jpg

查看完整回答
反对 回复 2023-12-11
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信