[英]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.