如果有人帮我解决这个问题,我将不胜感激!我知道这一定很简单,但我不能这样做。我得到的是一个基本上查询数据库并将其数据放在这样的表中的脚本:
<?php
require_once('../config.php');
include 'conn.php';
$curso = "UC001";
$data_recebimento = date("d-m-y", time());
$empresa = "ISAT";
$sql = "SELECT * FROM apontamentos WHERE idcurso='".$curso."' ORDER BY licao, tela ASC";
$query = mysql_query($sql) or die("Erro: ".mysql_error());
if(mysql_num_rows($query)==0){
echo "<h2>There are no records for this ".$curso."!</h2>";
}else{
header('Content-Type: text/html; charset=utf-8');
/*header("Content-type: application/vnd.ms-excel");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=apontamentos_".$curso."_".$data_recebimento.".xls");
header("Pragma: no-cache");*/
?>
<table border="1">
<tr>
<th colspan="4">Apontamentos do Curso "<?php echo $curso ?>" (<?php echo $empresa ?>) - <?php echo date("d/m/y, H:i:s", time()); ?></th>
</tr>
<?php
while($row = mysql_fetch_array($query)){
?>
<tr>
<th colspan="4">Lesson <?php echo $row["licao"] ?></th>
</tr>
<tr>
<th colspan="4">Part <?php echo $row["tela"] ?></td>
</tr>
<tr>
<td> </td>
<th>Descrição</th>
<th>Data criação</th>
<th>Data modificação</th>
</tr>
<tr>
<td><strong>Animações</strong></td>
<td><?php echo $row["animacoes"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Exercícios</strong></td>
<td><?php echo $row["exercicios"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Ilustrações</strong></td>
<td><?php echo $row["ilustracoes"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Navegação</strong></td>
<td><?php echo $row["navegacao"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Textos</strong></td>
<td><?php echo $row["textos"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Outros</strong></td>
<td><?php echo $row["outros"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<?php
}
}
mysql_close();
?>
</table>
所以,我需要的是这一行:
<tr><th colspan="4">Lesson <?php echo $row["licao"] ?></th><tr>
这是课程的标题,在打印部件之前出现一次(第1部分,第2部分......)。到目前为止我得到的是每一部分后出现的第1课。解决问题会很简单吗?我试过把ROLLUP,但我不确定它是否正确。我输了。
提前致谢。
答案 0 :(得分:0)
因为这段代码:
<tr>
<th colspan="4">Lesson <?php echo $row["licao"] ?></th>
</tr>
位于while
循环内,因此每次打印Part时都会显示Lesson。据我了解,您希望课程标题只打印一次,对吧?由于我无法很好地了解您的代码,因此我可以快速思考(请查看while
循环):
<?php
require_once('../config.php');
include 'conn.php';
$curso = "UC001";
$data_recebimento = date("d-m-y", time());
$empresa = "ISAT";
$sql = "SELECT * FROM apontamentos WHERE idcurso='".$curso."' ORDER BY licao, tela ASC";
$query = mysql_query($sql) or die("Erro: ".mysql_error());
if(mysql_num_rows($query)==0){
echo "<h2>There are no records for this ".$curso."!</h2>";
}else{
header('Content-Type: text/html; charset=utf-8');
/*header("Content-type: application/vnd.ms-excel");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=apontamentos_".$curso."_".$data_recebimento.".xls");
header("Pragma: no-cache");*/
?>
<table border="1">
<tr>
<th colspan="4">Apontamentos do Curso "<?php echo $curso ?>" (<?php echo $empresa ?>) - <?php echo date("d/m/y, H:i:s", time()); ?></th>
</tr>
<?php
$currentLesson = ''; // ADDED: Declare a 'flag' variable here to keep track what current lesson is inside while loop
while($row = mysql_fetch_array($query)){
?>
<?php
if ($currentLesson != $row["licao"]) { // If currentLesson != current 'licao'
$currentLesson = $row["licao"]; // then assign it to keep track
echo '<tr>
<th colspan="4">Lesson ' . $row["licao"] . '</th>
</tr>'; // Then output current Lesson
// From now on, them same lesson title shouldn't be printed until we meet new lesson title
// I don't know much about PHP and HTML mixing syntax, so I used echo just to be safe. You can do it in a better syntax.
// DONE
}
<tr>
<th colspan="4">Part <?php echo $row["tela"] ?></td>
</tr>
<tr>
<td> </td>
<th>Descrição</th>
<th>Data criação</th>
<th>Data modificação</th>
</tr>
<tr>
<td><strong>Animações</strong></td>
<td><?php echo $row["animacoes"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Exercícios</strong></td>
<td><?php echo $row["exercicios"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Ilustrações</strong></td>
<td><?php echo $row["ilustracoes"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Navegação</strong></td>
<td><?php echo $row["navegacao"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Textos</strong></td>
<td><?php echo $row["textos"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<tr>
<td><strong>Outros</strong></td>
<td><?php echo $row["outros"] ?></td>
<td><?php echo $row["data_criacao"] ?></td>
<td><?php echo $row["data_modificacao"] ?></td>
</tr>
<?php
}
}
mysql_close();
?>
</table>
如果有帮助,请告诉我。