我有以下mysql表,其中包含数据。包含数据的表格如下图所示:
我想在html表中显示这样的记录:
虽然它显示如下记录:
我有以下代码:
<?php
$year = intval($_POST['year']);
$month = (intval($_POST['month']) < 10 && strlen($_POST['month']) < 2) ? '0'.intval($_POST['month']):intval($_POST['month']);
$sql = "SELECT sno,file_path,bench_sno,causelist_date,date_entry
FROM phc_causelists
WHERE YEAR(causelist_date) = :year AND MONTH(causelist_date) = :month ORDER BY causelist_date DESC";
$aryList = array(':year'=>$year,':month'=>$month);
if($db->dbQuery($sql,$aryList)){
?>
<strong>Causelists for the month of <?php echo($method->getMonthName(intval($month)).' '.intval($year)); ?> </strong>
<table class="table table-bordered">
<?php
foreach($db->getRecordSet($sql,$aryList) as $row){
?>
<tr>
<td><?php echo($row['causelist_date']); ?></td>
<td><?php if($row['bench_sno'] == '1'){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['file_path']); ?>">Peshawar High Court</a>
<?php } ?></td>
<td><?php if($row['bench_sno'] == '2'){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['file_path']); ?>">Abbottabad</a>
<?php } ?></td>
<td><?php if($row['bench_sno'] == '3'){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['file_path']); ?>">D.I.Khan</a>
<?php } ?></td>
<td><?php if($row['bench_sno'] == '4'){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['file_path']); ?>">Mingora</a>
<?php } ?></td>
<td><?php if($row['bench_sno'] == '5'){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['file_path']); ?>">Bannu</a>
<?php } ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
答案 0 :(得分:1)
这可能有用。通过最小的更改,它将日期分组,然后循环到日期。
<?php
foreach($db->getRecordSet($sql,$aryList) as $row){
// build an array of dates
// containing for each date an array of bench_sno
$dates[$row['causelist_date']][$row['bench_sno']] = $row['filepath'];
}
foreach($dates as $date=>$row) {
?>
<tr>
<td><?php echo($date); ?></td>
<td><?php if(isset($row['1'])){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['1']); ?>">Peshawar High Court</a>
<?php } ?></td>
<td><?php if(isset($row['2'])){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['2']); ?>">Abbottabad</a>
<?php } ?></td>
<td><?php if(isset($row['3'])){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['3']); ?>">D.I.Khan</a>
<?php } ?></td>
<td><?php if(isset($row['4'])){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['4']); ?>">Mingora</a>
<?php } ?></td>
<td><?php if(isset($row['5'])){ ?>
<a href="<?php echo($method->baseURL()); ?>/causeLists/<?php echo($row['5']); ?>">Bannu</a>
<?php } ?></td>
</tr>
<?php
}
?>
或者,我喜欢避免重复代码并在模板中使用替代控制语法:
<?php
foreach($db->getRecordSet($sql,$aryList) as $row) {
// build an array of dates
// containing for each date an array of bench_sno
$dates[$row['causelist_date']][$row['bench_sno']] = $row['filepath'];
}
$bench_snos[
'1' => 'Peshawar High Court',
'2' => 'Abbottabad',
'3' => 'D.I.Khan',
'4' => 'Mingora',
'5' => 'Bannu',
];
foreach($dates as $date=>$row) :
?>
<tr>
<td><?php echo $date ?></td>
<?php foreach ($bench_snos as $no=>$bench_sno) : ?>
<td><?php if (isset($row[$no])) : ?>
<a href="<?php echo $method->baseURL() ?>/causeLists/<?php echo $row[$no] ?>"><?php echo $bench_sno ?></a>
<?php endif //isset ?>
</td>
<?php endfor //$bench_snos ?>
</tr>
<?php endfor // $dates ?>