[英]php mysql result multidimensional array to custom table
我在mysql上有以下表格:
daily : daily_id, date, student_id, class_id, amount1, amount2, is_done
students : student_id, person_id, ....
persons : person_id, person_name,....
classes : class_id, class
現在,我可以從該表中選擇一個范圍,其中日期介於2個值之間(假設是一周)。 我猜這為我提供了一個多維數組(我是菜鳥,可惜...)
我希望能夠擴展結果並將它們分組在這樣的表上:
Sunday | Monday | Tuesday | ........
personname1 | class | amount1 | class | amount1 | class | amount1 | ........
personname2 | class | amount1 | class | amount1 | class | amount1 | ........
現在,如果某個學生在本周的特定日期不在場,或者他沒有設法籌集到任何款項,那么我們應該在該日顯示“缺席”和$ 0.00之類的信息。
我已經嘗試了很長時間,要遍歷數組並在沒有運氣的情況下在這樣的表上正確獲取結果...我確信這是由於菜鳥和其他原因,但是我已經用盡了所有想法,我需要一些好的指針,沒有幫助,我將無能為力...我得到的只是一行結果,例如:
(row1) daily_id, date, student_id, class_id, amount1, .....
(row2) daily_id, date, student_id, class_id, amount1, ..... etc
到目前為止,我的代碼:
<?php
$query = "SELECT * FROM daily WHERE date BETWEEN '2016-03-13' AND '2016-03-19'";
$selected_result = mysqli_query($connection, $query);
?>
<table>
<? php
while($row = mysqli_fetch_assoc($selected_result)) {
$daily_id = $row['daily_id'];
$date = $row['date'];
$class_id = $row['class_id'];
$student_id = $row['student_id'];
$amount1 = $row['amount1'];
$amount2 = $row['amount2'];
$is_done = $row['is_done'];
?>
<tr>
<td><?php echo $daily_id; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $student_id; ?></td>
<td><?php echo $class_id; ?></td>
<td><?php echo $amount1; ?></td>
<td><?php echo $amount2; ?></td>
<td><?php echo $is_done; ?></td>
</tr>
<?php
} // closing the while loop
?>
</table>
有人能指出我正確的方向嗎?
要按照您的意願顯示數據,您需要從多個表中選擇數據。 我建議您閱讀JOIN
語法,因為有時可能很難閱讀官方文檔 ,所以您可以閱讀有關該主題的教程 。 另外,您的表可以使用一些優化方法,但這與該問題無關。
話雖如此,我想這就是您想要實現的目標。 首先是SQL:
SELECT
persons.person_name,
daily.date,
daily.amount1
classes.class
FROM
persons LEFT JOIN
students ON persons.person_id = students.person_id RIGHT JOIN
daily ON students.student_id = daily.student_id LEFT JOIN
classes ON daily.class_id = classes.class_id
WHERE
daily.date BETWEEN '2016-03-13' AND '2016-03-19'
ORDER BY
persons.person_id ASC,
daily.date ASC
然后是您的HTML / PHP代碼
<table>
<tr>
<th>Name</th>
<th colspan="2">Monday</th>
<th colspan="2">Tuesday</th>
<th colspan="2">Wednesday</th>
<th colspan="2">Thursday</th>
<th colspan="2">Friday</th>
<th colspan="2">Saturday</th>
<th colspan="2">Sunday</th>
</tr>
<tr>
<?php
$current_date = strtotime( '2016-03-13' );
$maximum_date = strtotime( '2016-03-19' );
$one_day = 24 * 3600; // 24 hours of 3600 seconds each.
$current_person = '';
while( $row = mysqli_fetch_assoc( $selected_result ) ) :
if( $current_person != $row['person_name'] && $current_person != '' ) :
// Fill in the empty days at the end of the week.
while( $current_date <= $maximum_date ) :
?>
<td>Absent</td>
<td>$0.00</td>
<?php
endwhile;
$current_date = strtotime( '2016-03-13' );
?>
</tr>
<tr>
<?php
endif;
$row_date = strtotime( $row['date'] );
?>
<td><?php echo $row['person_name']; ?></td>
<?php
// Fill in an empty day.
if( $row_date > $current_date ) :
?>
<td>Absent</td>
<td>$0.00</td>
<?php
else :
?>
<td><?php echo $row['class']; ?></td>
<td><?php echo $row['amount1']; ?></td>
<?php
endif;
$current_date += $one_day;
$current_person = $row['person_name'];
?>
<?php
endwhile;
?>
</tr>
</table>
我可能在電話中寫了部分內容,這可能有些麻煩。 該格式過大以幫助您理解,但您可以簡化它。
重要提示 :這就像您在問題中所做的那樣,假設每個person
每天只有一class
。 如果不是這種情況,那么您需要一種不同的方法。
非常接近結果。
這是您需要更改的內容:
<?php $html="<table>"; while($row = mysqli_fetch_assoc($selected_result)) { $html.="<tr><td>$row['daily_id']</td><td>$row['class_id']</td></tr>"; // ad more inside that } $html.="</table>"; echo $html; ?>
您需要做的第二件事是進行結構設計。 列數<行數,只是建議
::您可以將days(number of 7)換行。 和person(class,amt)作為cols標題(數量為4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.