我报告的问题更简洁一些。 (删除其他)
我有一个我建立的时间报告系统,我正在尝试创建一个遵守类型报告。
基本上我正在尝试执行以下操作:
从那里,我需要显示提交的所有名称(来自上面的#3)以及未完成的名单(通过比较上面的#1和#2)
到目前为止,我可以获得名单。但是,我需要使用数据库中的每个人(Region,Timestamp等)显示其他数据。
期望输出将是这样的:
的提交
的 EMEA
约翰J
Wally R
北美
戴维斯D
鲍勃C
约翰Y
的杰出
的 EMEA
凯尔D
北美
Cory T
休R
约翰J
的 LATAM
等....
到目前为止,这就是我所拥有的......我确信我会以错误的方式解决这个问题。
<?php
$con = mysqli_connect("localhost","xxx","xxx!","xxx");
//Get distinct names from practice
if ($con) {
$SQL = "SELECT DISTINCT fn,ln FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";
}
$result = mysqli_query($con,$SQL);
if (!$result) die('Couldn\'t fetch records');
while ( $db_field = mysqli_fetch_assoc($result) ) {
$name[] = trim($db_field['fn']. " " .$db_field['ln']);
}
//Get distinct names for current week practice
if ($con) {
$SQL2 = "SELECT DISTINCT fn,ln FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";
}
$result2 = mysqli_query($con,$SQL2);
if (!$result2) die('Couldn\'t fetch records Again');
while ( $db_field = mysqli_fetch_assoc($result2) ) {
$name2[] = trim($db_field['fn']. " " .$db_field['ln']);
}
//$SQL3 = "SELECT fn,ln,week_start_date,region FROM $table WHERE fn IN ( '".implode("', '", $differences)."' )");
mysqli_close($con);
?>
<TABLE><TR><TD>
<B>All Names</B><BR>
<?php
foreach ( $name as $item ) {
echo $item . "<br/>";
}
?>
</TD>
<TD>
<B>Names for Week 8</B><BR>
<?php
foreach ( $name2 as $item2 ) {
echo $item2 . "<br/>";
}
?>
</TD>
<TD>
<B>Outstanding</B><BR>
<?php
$results = array_diff($name, $name2);
foreach($results as $val) {
echo $val ." - ".$val2."<BR>";
}
?>
</TD>
</TR>
</TABLE>
答案 0 :(得分:1)
我认为你应该在MySQL级别解决这个问题。
// All names with all data
$SQL = "SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";
// Names for specific week with all data
$SQL = "SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";
// Outstanding rows with all data
$SQL = "SELECT * FROM (SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes') AS `subquery` WHERE `name` NOT IN (SELECT DISTINCT CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes') ORDER BY region,fn,ln";
这样您就不需要在PHP级别对数据进行排序和处理。无法测试这个,因为我没有数据库环境,但它应该工作,可能需要一些小的改动。