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

你好,我在这里遇到一个问题,我想用 php 从 mysql 检索数据(完成了:)),然后例如当我的变量等于 $dia_semana星期日时,它将把星期日的所有数据显示到具有特定 id 的特定 div 中,然后继续


这是我的 php 代码:


<?php

function 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>


江户川乱折腾
浏览 112回答 2
2回答

呼唤远方

这是我的完全有效的代码:$WEEKDAYS = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ResultsArray = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = mysqli_fetch_array($result)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create an array to hold the return values from your DB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $date = $row['dia_semana']; //<--- coming from db holds date values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ResultsArray[$date][] = $row['temperature'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<table style='padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;'>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($WEEKDAYS as $key => $date) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>".$date."</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($x = 0; $x < 1; $x++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Sunday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Monday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Tuesday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Wednesday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Thursday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Friday'][$x]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$ResultsArray['Saturday'][$x]."</td>";echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</table>";

婷婷同学_

定义一个常量/数组来与数组进行比较,并使用键值将星期几设置为从星期日开始。然后使用该常量/数组运行 foreach 来回显从周日开始的每一天的表数据。检查常量中的值是否在返回的数组中,如果是,则回显该天及其也设置为该天的数组中的相应值。define("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);$parseDays = array(&nbsp; &nbsp; 'Frank' => 'Monday',&nbsp; &nbsp; 'Jean' => 'Monday',&nbsp; &nbsp; 'Mike' => 'Sunday',&nbsp; &nbsp; 'Bob' => 'Tuesday',&nbsp; &nbsp; 'Bill' => 'Friday',&nbsp; &nbsp; 'Jack' => 'Sunday',&nbsp; &nbsp; 'George' => 'Friday',&nbsp; &nbsp; 'Dillon' => 'Wednesday');$stmt = '<table>';$stmt .= '<tr>';foreach(WEEKDAYS as $key => $day){&nbsp; &nbsp; if(in_array($day, $parseDays)){&nbsp; &nbsp; &nbsp; &nbsp; if($day === $day){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($parseDays as $name => $days){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($days === $day){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '<br>'.$name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '</td>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}$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&nbsp;// the week concurrentlydefine("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);while($row = mysqli_fetch_array($result)) {&nbsp;&nbsp; &nbsp; // create an array to hold the return values from your DB&nbsp; &nbsp; $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 concurrentlyforeach(WEEKDAYS as $key => $day){&nbsp; &nbsp; // check if the $day value from constant is located in the $date array from db&nbsp; &nbsp; if(in_array($day, $date)){&nbsp; &nbsp; &nbsp; &nbsp; if($day === $day){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // loop through db array and assign key/values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($date as $name => $days){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If value from db array is equal to value from constant, display it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($days === $day){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '<br>'. //enter name values here;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt .= '</td>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}$stmt .= '</tr>';$stmt .= '</div>';echo $stmt;输出:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5