Table with mix display data(vertical & horizontal) HTML & PHP

Can anyone help me on this one. I need to produce table like picture Result.png, but as you can see the first row I need to display it vertically but the second column I need to display it as horizontal. Plus, the ‘Jumlah’ at the end column is the total of each row and I need to put another total below all the row to calculate the total by column. I have tried for a week and got stuck. Please help.

below is my code:

<?php 

$total_pkutip2 = 0;
$total_amt2 = 0;
$jum_kutip = 0;
$jum_amt = 0;

while (($row = oci_fetch_assoc($stid)) != false) {

    $kmn_produk = $row['PRODUK'];
    $kmn_state  = $row['STATE'];
    $kmn_branch = $row['BRANCHNAME'];
    $kmn_kutip  .= '<td align="right">'.number_format ($row['PKUTIP'],2).'<br>'.number_format ($row['AMOUNT'],2).'</td>';
    $kmn_mth    .= '<td>'.$row['MTH'].'</td>';

    $jum_kutip += $row['PKUTIP']; 
    $jum_amt   += $row['AMOUNT']; 
    //$jum_kutip[$kmn_mth] = $jum_kutip[$kmn_mth] + $kmn_kutip; //ni equivalent line atas ni. 

    $total_pkutip2 = $total_pkutip2 + $kmn_kutip;
    $total_amt2 = $total_amt2 + $kmn_amt;
}

echo '
    <h3>LAPORAN KUTIPAN MENGIKUT NEGERI DAN KAWASAN BAGI TAHUN '.$getyear.' </h3>
    <h4>PRODUK : '.$kmn_produk.'</h4>

    <table width="100%" border="1" cellspacing="0" cellpadding="0" id="table1" class="display">

        <thead>
            <tr>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col" align="center">NEGERI | KAWASAN<br></th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">JAN '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">FEB '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">MAC '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">APR '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">MEI '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">JUN '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">JUL '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">AUG '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">SEP '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">OKT '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">NOV '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">DEC '.$getyear.'<br>PATUT KUTIP<br>KUTIPAN</th>
                <th style="text-align:center" bgcolor="#CCCCCC" scope="col">JUMLAH</th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td><b>'.$kmn_state.'</b><br>&nbsp;&nbsp&nbsp'.$kmn_branch.'</td>
                '.$kmn_kutip.'
                <td align="right">'.number_format ($jum_kutip,2).'<br>'.number_format ($jum_amt,2).'</td>
            </tr>
        </tbody>
    </table>
';
?>

data :

result:

I’m confused as to what you’re trying to achieve. The result.png image only seems to show the top (header) row on my screen - what should be below it, as I presume that’s the part you’re having trouble with? Can you show an image of how the table should appear with some of the data filled in?

here is the example result :

here is another one :

It seems as if you’re trying to put all the data content into a single table row, as you scan through the database records and build up the $kmn_kutip and $kmn_mth variables for all records. I guess this is to remove the cell divisions, but I’d suggest you drop that approach and style the table using CSS afterwards to make it look like you want it to.

So I would change your code to output the headers, then loop around the data to display each row as required, build up the per-row total to display in the right-hand column, and build up the totals for the bottom row(s) at the same time.

Another thing I think you’ll need to take into account is what happens if there isn’t a result for a particular branch name / month combination - there are many ways you could do that, and if you’re absolutely 100% sure that your data could never have a missing record, then you could ignore them. But it might be better to deal with it at the start.

Any time the branch name changes, I would probably create an array with 12 elements, and feed the row data into the correct element based on the month number. Then when the name changes again, that’s when you output the html for that table row.

while ($row = fetchdata($db)) {  // pseudo-code, not proper PHP
  if ($row['branch-name'] != $lastbranch) { 
    if ($lastbranch != "") { 
      // output a <tr>, branch name, monthly totals, row total, </tr>
      // possibly add the row total to the grand total here, or maybe do it in the main loop code
      // clear the array
      }
    // create a new array for the monthly totals
    // zero the row total
    }
  // stick the new monthly total into the correct element
  // add the value to the row total
  $lastbranch = $row['branch-name'];
  }

or something like that. Basically as you want to display a nicely-formatted HTML table, stick to using the proper HTML tags and then style it to taste afterwards.

1 Like

sorry but im a beginner and i dont really understand what you proposed. :frowning:

Well, break it down into steps. Move the loop in your code to after the header is output, and output each monthly figure for each branch. Once you’ve got that working, add in code to keep the totals for each row, and then add the overall totals. Keep building it up, then worry about removing borders and so on afterwards.

I can’t tell whether you’re a beginner at programming in general, or have experience in programming but new to PHP. The code you posted suggests you have some PHP knowledge.

1 Like

thanks, i will try… btw, the code is coming from my friend. :smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.