简体   繁体   English

协调具有动态行和列的表的问题

[英]Issue with coordinating a table with dynamic rows and columns

Bear with me here--this may be a bit confusing. 在这里忍受-这可能有点令人困惑。

I am retrieving two sets of data with SQL. 我正在使用SQL检索两组数据。 Here's the code, with the query. 这是带有查询的代码。 I'm using Zend Framework. 我正在使用Zend Framework。

$assignments = $db->fetchAll("SELECT id,name,class FROM assignments");
foreach($assignments as $a) {
    $assignmentID = $a['id'];
    $studentData = $db->fetchAll(
        "SELECT student,assignment,status,assignmentID FROM student_assignments WHERE assignmentID='$assignmentID'"
    );

    echo "<th>".$a['name']."</th>";

    foreach($studentData as $s) {
        $bottom .= "<tr><td>" . $s['student'] . " " . $s['assignmentID'] 
                   . " ".$s['status'] . "</td></tr>";
        $i++;
    }
}
echo "</tr>$bottom;";

Here's what the output looks like in the HTML: 这是HTML中的输出结果:

|Assignment on 07/07/2012|  |Assignment on 07/12/2012|   |Assignment on 07/15/2012|
117 1 Y
332 1 N
36 1 N
420 1 N
332 1 Y
326 2 N
212 2 N
461 2 N
117 2 N
212 2 N
212 3 N
326 3 N
117 3 Y
420 3 Y

Now the top part is working great -- it's dynamically showing each assignment in the database. 现在,顶部工作得很好-它动态地显示了数据库中的每个分配。 But I've been trying to figure out a way to get the appropriate data to show under those columns, to no effect. 但是我一直在试图找到一种方法来获取适当的数据以显示在这些列下,而没有任何效果。 This is the closest I've gotten to making it look somewhat correct. 这是我得到的最接近的结果,它看起来有些正确。

Essentially, the data that has "2" and "3" in the middle should go into the 2nd and 3rd columns, respectively. 本质上,中间具有“ 2”和“ 3”的数据应分别进入第二列和第三列。 But this isn't happening because all the data is stored into the $bottom variable, rather than the data for each assignment. 但这不会发生,因为所有数据都存储在$ bottom变量中,而不是每个分配的数据中。

Does anyone have any suggestions? 有没有人有什么建议? This is driving me crazy, and I feel like the solution is staring me in the face. 这让我发疯,我觉得解决方案正盯着我。

Thanks! 谢谢!

First you want to iterate through every of your student assignments, and left join the assignments table to it so you can know the name of the assignment that it is related to. 首先,您要遍历每个学生的作业,然后将作业表加入其中,这样您就可以知道与之相关的作业的名称。

$students = $db->fetchAll('SELECT sa.student,sa.assignment,sa.status,sa.assignmentID,a.name
                           FROM student_assignments AS sa
                           LEFT JOIN assignments AS a ON sa.assignmentID=a.id');

Then with the results, you can build an array to regroup everyone with the same assignment: 然后使用结果,您可以构建一个数组以将每个具有相同分配的人重新分组:

$columns = Array();
foreach($students as $s) {
    $row = '<tr><td>'.$s['student'].' '.$s['assignmentID'].' '.$s['status'].'</td></tr>';
    array_push($columns[$s['name']], $row);
}

Then with this array, you can finally print your content: 然后,使用此数组,您最终可以打印内容:

foreach ($columns as $key=>$value) {
    echo '<th>'.$key.'</th>';
    foreach ($value as $v) {
        echo $v;
    }
}

Of course this can be more compact (reduced into nested loops), and I have no way to fully test it, but it should help you in your process ;) 当然,它可以更紧凑(减少为嵌套循环),并且我无法对其进行全面测试,但是它将在您的过程中为您提供帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM