Abdul Rahman Abdul Rahman - 3 months ago 14
HTML Question

Need to display record horizontally by date

i have the following mysql table with data in it. Table with data is shown in the photo below:

enter image description here

I want to display the record in html table like this:

enter image description here

While it displays record like the following:

enter image description here

I have the following code for this:

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

Answer

This might work. With minimal changes, it groups the dates, then loops foreach date.

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