2016-08-20 2 views
1

데이터가있는 다음과 같은 mysql 테이블이 있습니다. 그것은 다음과 같은 기록을 표시하는 동안날짜로 레코드를 수평으로 표시해야합니다.

enter image description here

:

enter image description here

내가 같은 HTML 테이블의 레코드를 표시하려면 : 데이터 테이블은 아래 사진에 표시됩니다

enter image description here

다음 코드를 가지고 있습니다.

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

왜 유 넣어 [ 'bench_sno'] == '1') {**? –

+0

나는 bench_sno에 대한 레이블을 포함하는 다른 테이블을 가지고 있습니다. 어디에 레이블 "PHC"에 대한 sno = 1, sno 2 Abbottabad 등 –

+0

당신의 테이블 구조가 귀하의 PHP와 조건이 맞지 않는 것 같아요. –

답변

1

최소한의 변경으로 날짜를 그룹화 한 다음 foreach 날짜를 반복합니다. ? 또한

<?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는 경우

<?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 ?> 
+0

print_r()은 0,1,6 만 반환한다. - –

+0

버그를 고쳤다. 죄송합니다. 그게 뭐죠? 무엇의 print_r? 9 년의 경험으로 디버깅 할 수 있어야합니까? – PaulH

+0

당신은 멋진 직업을 가졌습니다. 각각의 if (isset ($ row [ '1'])에서 잊어 버린 버그가 하나뿐입니다.) rocks. 고마워요. –

관련 문제