我在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>
谁能告诉我正确的方向吗?
非常接近结果。
以下是您需要更改的内容:
- 通过先连接后回显来创建一个html字符串。示例:
<?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; ?>
你需要做的第二件事是研究结构。列数<没有行,只是建议
::u可以使天(数字为7)成为行。和人(阶级,amt)作为列标题(数字为4)
要在表示数据时显示数据,需要从多个表中选择数据。我建议你阅读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
。如果不是这样,那么你需要一种不同的方法。