HTML Formatted table - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of HTML Formatted table.php

  1. <?php
  2.     $data = array(
  3.         array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),
  4.         array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '004', 'quantity' => '3','cost' => '2000'),
  5.         array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '005', 'quantity' => '4','cost' => '2000'),
  6.         array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '006', 'quantity' => '5','cost' => '2000'),
  7.         array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '008', 'quantity' => '1','cost' => '2000'),
  8.         array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '013', 'quantity' => '2','cost' => '2000'),
  9.         array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '020', 'quantity' => '3','cost' => '2500'),
  10.         array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '022', 'quantity' => '4','cost' => '2500'),
  11.         array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '007', 'quantity' => '1','cost' => '2500'),
  12.         array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '015', 'quantity' => '7','cost' => '2500')
  13.     );
  14.        
  15.         $symbol='£';
  16. ?>
  17. <!doctype html>
  18. <html>
  19.         <head>
  20.                 <meta charset='utf-8' />
  21.                 <title>HTML table based upon PHP array data</title>
  22.                 <style>
  23.                         table{ border:1px solid gray;font-family:calibri,verdana,arial;float:none;margin:auto; }
  24.                         th{background:gray;color:white;padding:0.5rem;}
  25.                         td{padding:0.5rem;border:1px dotted gray;}
  26.                         td[colspan]{background:whitesmoke;}
  27.                         .currency:before{
  28.                                 content:'<?=$symbol;?>';
  29.                                 color:green;
  30.                                 font-weight:bold;
  31.                         }
  32.                 </style>
  33.         </head>
  34.         <body>
  35.                 <table>
  36.                         <tr>
  37.                                 <th>transaction_number</th>
  38.                                 <th>date</th>
  39.                                 <th>item_number</th>
  40.                                 <th>desc</th>
  41.                                 <th>variant_code</th>
  42.                                 <th>quantity</th>
  43.                                 <th>cost</th>
  44.                         </tr>
  45.                         <?php
  46.                        
  47.                                 $trans=array();
  48.                                 $dates=array();
  49.                                
  50.                                 $total=new stdClass;
  51.                                 $total->qty=0;
  52.                                 $total->cost=0;
  53.                                
  54.  
  55.                                 foreach( $data as $index => $a ){
  56.                                         /*
  57.                                                 Transaction number & date variables
  58.                                                 - empty unless not in array
  59.                                         */
  60.                                         $tn='';
  61.                                         $dt='';
  62.                                        
  63.                                         /* check if current transaction is already in the array - if not add it and create a new subtotal object */
  64.                                         if( !in_array( $a['transaction_number'], $trans ) ) {
  65.                                                 /* assign `$dt` variable to newly discovered transaction and add to array */
  66.                                                 $tn = $trans[] = $a['transaction_number'];
  67.                                                
  68.                                                 $subtotal=new stdClass;
  69.                                                 $subtotal->qty=0;
  70.                                                 $subtotal->cost=0;
  71.                                         }
  72.                                         /* Check if the date is in it's array - if not, add it */
  73.                                         if( !in_array( $a['date'], $dates ) ) {
  74.                                                 /* assign `$dt` var to newly discovered date and add to array */
  75.                                                 $dt = $dates[] = $a['date'];
  76.                                         }
  77.                                        
  78.                                         /* update subtotals */
  79.                                         $subtotal->qty += floatval( $a['quantity'] );
  80.                                         $subtotal->cost += floatval( $a['cost'] );
  81.                                        
  82.                                        
  83.                                         /* output the table row with data including vars defined above */
  84.                                         printf('
  85.                                         <tr>
  86.                                                 <td>%s</td>
  87.                                                 <td>%s</td>
  88.                                                 <td>%s</td>
  89.                                                 <td>%s</td>
  90.                                                 <td>%s</td>
  91.                                                 <td>%s</td>
  92.                                                 <td>%s</td>
  93.                                         </tr>', $tn, $dt, $a['item_number'], $a['desc'], $a['variant_code'], $a['quantity'], $a['cost'] );
  94.                                        
  95.                                        
  96.                                         /* Show the sub-total for current transaction number */
  97.                                         if( ( $index < count( $data ) - 1 && $trans[ count( $trans )-1 ] != $data[ $index + 1 ]['transaction_number'] ) or $index==count( $data )-1 ){
  98.                                                 printf('
  99.                                                 <tr>
  100.                                                         <td colspan=4> </td>
  101.                                                         <td>SUB-TOTAL</td>
  102.                                                         <td>%s</td>
  103.                                                         <td class="currency">%s</td>
  104.                                                 </tr>', $subtotal->qty, $subtotal->cost );
  105.                                                
  106.                                                 $total->qty += floatval( $subtotal->qty );
  107.                                                 $total->cost += floatval( $subtotal->cost );
  108.                                         }
  109.                                 }
  110.                                
  111.                                 /* Show the final totals */
  112.                                 printf('
  113.                                 <tr><td colspan=7> </td></tr>
  114.                                 <tr>
  115.                                         <td colspan=4> </td>
  116.                                         <td>TOTAL</td>
  117.                                         <td>%s</td>
  118.                                         <td class="currency">%s</td>
  119.                                 </tr>', $total->qty, $total->cost );
  120.                        
  121.                         ?>
  122.                 </table>
  123.         </body>
  124. </html>
File Description
  • HTML Formatted table
  • PHP Code
  • 10 Aug-2018
  • 4.75 Kb
You can Share it: