2016-03-23 47 views
2

我在MySQL下表:PHP的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美元。

我一直在尝试很长一段时间来遍历数组,并得到我的结果在这样一张桌子上没有任何运气正确......我相信这是由于被noob和所有,但我已经江郎才尽,我需要一些很好的指针,没有帮助我做不到......我得到的结果一样一排:

(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> 

任何人都可以请指点我正确的方向?

+0

下面@Technoh提供的解决方案最接近所需的结果。但是,使用循环生成的表格不正确,即使它们包含所有必需的数据,也不会按照上面的示例进行排序......是否有任何人可以跳入并伸出援助之手? – Panos

回答

0

要显示数据,您需要从多个表中选择数据。我建议你阅读JOIN的语法,你可以阅读tutorial on the subject,因为official documentation有时候很难阅读。此外,你的表可以使用一些优化,但这是这个问题旁边。

这就是说,这里是,我想,你正在寻找什么。首先,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> 

这可能是有点circumvoluted,我写的这部分,而在手机上。格式臃肿以帮助您理解,但您可以将其简化。

重要:与您的问题一样,此假定每个person每天只有一个class。如果情况并非如此,那么你需要一种不同的方法。

+0

现货!正是我在找什么。你的代码需要一些改进。有一些错误,比如“$ row ['date]在日期之后缺少''。此外,表创建的应该在while循环中,因为下一个记录仅添加在右侧的一行上。你能发现它吗?我不知道在哪里,因为我仍然试图熟悉我的自我和你的代码。 – Panos

+0

乍看之下,我会在几分钟后再看一次。但是,我确实修改了SQL,以便通过'person'对其进行分组,并修复'$ row ['date']'问题。数据的第一行还是最后一行是额外的''? – Technoh

+0

就像我上面所说的,表格在填充数据时需要一些排序。我现在注意到这行显示,学生姓名(名称下),然后是班级和金额(下周一),然后是学生姓名和班级(周二下),这是错误的,然后数量和名称(周三下)等等。小调整,我们设置! – Panos

0

非常接近结果。

这里是什么ü需要改变:

  1. 通过连接第一则呼应创建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)作为cols标题(4中的数字)

+0

谢谢,但这不是我要找的 – Panos