我有 2020 年颶風(fēng)名稱(chēng)和所有州。我想遍歷我的數(shù)據(jù)庫(kù)并查看是否選擇了選項(xiàng)。如果是,請(qǐng)用名稱(chēng)填充該位置。如果不是,請(qǐng)?zhí)砑印?-”。它看起來(lái)幾乎就像一個(gè)數(shù)據(jù)電子表格。當(dāng)我提交數(shù)據(jù)時(shí),它沒(méi)有填充到正確的位置并添加了一堆空單元格。這是我的查詢(xún):// DB Connection$db_connection = mysqli_connect('localhost','root','','hurricane_bowl'); $i = "SELECT * FROM users RIGHT JOIN states USING (state_id) GROUP BY (state_name) ORDER BY state_name ASC";$state = mysqli_query($db_connection, $i);$g = "SELECT * FROM users RIGHT JOIN hurricanes USING (hurricane_id) GROUP BY (hurricane_name) ORDER BY hurricane_name ASC";$hurricane = mysqli_query($db_connection, $g);這是我的循環(huán):<table class="table table-striped table-bordered"> <thead> <tr> <th scope="col">Hurricane Name</th> <?php foreach($state as $stval) { echo '<th scope="col">' . $stval['state_name'] . '</th>'; } ?> </tr> </thead> <tbody> <?php foreach($hurricane as $h) { echo '<tr>'; echo '<th scope="row">' . $h['hurricane_name'] . '</th>'; foreach($state as $st) { if($st['state_id'] != NULL && $h['hurricane_id'] != NULL) { echo '<td>' . $st['username'] . '</td>'; } else { echo '<td>--</td>'; } } echo '</tr>'; } ?> </tbody> </table>
PHP使用foreach循環(huán)在表中顯示數(shù)據(jù)
拉風(fēng)的咖菲貓
2023-04-02 10:17:51