repeat headers after each two row in my table

Viewed 46

I am using lucidFrame consoletable to create an output using json. my code below is

$table = new ConsoleTable();
    $table
        ->setHeaders($strData->header)
        ->hideBorder();
        foreach ($strData->body as $ins) {
            $items = [];
            foreach ($ins as $key=>$value) {
                array_push($items,$value);
            }
            $table->addRow($items);
            // $i++;
        }
    $table->addBorderLine();
    fwrite($fp, str_replace('</pre>','', str_replace('<pre >','',''. $finalHead . $table->getTable().$finalFooter )));
    fclose($fp);

and my result is

 ** COMP ** STATEMENT as on 12/08/2021   
 colhead1             colhead2
----------------------------------
 CC Limit             1,000 
 Availed Limit        10,621   
 PM Stock Value       75,095   
 RM Stock Value       67,895   
 Product Stock Value  46,456   
 Total Stock Value    1,446 
 75% of stock value   1,835 
 Investment           0           
----------------------------------
 *Report taken by NATH on 20/08/2021 at 12:08:22 in PLACE* 

I want to repeat headers after each two rows. How can I do this ? Please help My desired output will be like this

 ** COMP ** STATEMENT as on 12/08/2021   
 colhead1             colhead2
----------------------------------
 CC Limit             1,000 
 Availed Limit        10,621  
 ** COMP ** STATEMENT as on 12/08/2021 
colhead1             colhead2
----------------------------------
 PM Stock Value       75,095   
 RM Stock Value       67,895  
 ** COMP ** STATEMENT as on 12/08/2021 
colhead1             colhead2
----------------------------------
 Product Stock Value  46,456   
 Total Stock Value    1,446 
 ** COMP ** STATEMENT as on 12/08/2021 
colhead1             colhead2
----------------------------------
 75% of stock value   1,835 
 Investment           0           
----------------------------------
 *Report taken by NATH on 20/08/2021 at 12:08:22 in PLACE* 

thanks in advance

2 Answers

In your main foreach loop, you can check if $i is odd, like this:

($i % 2) === 1

If the above statement is true, print your header. You can read more about the modulo (%) arithmetic operator here.

EDIT:

You can even simplify this, since the result of $i % 2 will be either 0 or 1, you can just put

if($i % 2){ /* print header */ }

The library doesn't support yet to print repeated headers in one table. You can only print separate tables by every two rows, for example,

use LucidFrame\Console\ConsoleTable;

$headers = array('colhead1', 'colhead2');
$data = array();
$data[] = array('CC Limit', 1000);
$data[] = array('Availed Limit', 10621);
$data[] = array('PM Stock Value', 75095);
$data[] = array('RM Stock Value', 67865);
$data[] = array(' Product Stock Value', 46456);
$data[] = array('Total Stock Value', 1446);
$data[] = array('75% of stock value', 1835);
$data[] = array('Investment', 1000);

foreach ($data as $i => $row) {
    if ($i % 2 == 0) {
        if ($i) {
            echo "** COMP ** STATEMENT as on 12/08/2021\n";
            $table->display();
        }
        $table = new ConsoleTable();
        $table->hideBorder();
        $table->addRow($headers);
        $table->addBorderLine();
    }
    $table->addRow($row);
}
echo "** COMP ** STATEMENT as on 12/08/2021\n";
$table->display();

This will print as

** COMP ** STATEMENT as on 12/08/2021
 colhead1       colhead2
-------------------------
 CC Limit       1000
 Availed Limit  10621
** COMP ** STATEMENT as on 12/08/2021
 colhead1        colhead2
--------------------------
 PM Stock Value  75095
 RM Stock Value  67865
** COMP ** STATEMENT as on 12/08/2021
 colhead1              colhead2
--------------------------------
 Product Stock Value   46456
 Total Stock Value     1446
** COMP ** STATEMENT as on 12/08/2021
 colhead1            colhead2
------------------------------
 75% of stock value  1835
 Investment          1000

OR

You can print headers using addRow() and addBorderLine() after each two rows ($i % 2 == 0). For example,

$table = new ConsoleTable();
foreach ($data as $i => $row) {
    if ($i % 2 == 0) {
        if ($i) {
            $table->addBorderLine();
        }
        $table->addRow($headers);
        $table->addBorderLine();
    }
    $table->addRow($row);
}
$table->display();

It will display like this

+----------------------+----------+
| colhead1             | colhead2 |
+----------------------+----------+
| CC Limit             | 1000     |
| Availed Limit        | 10621    |
+----------------------+----------+
| colhead1             | colhead2 |
+----------------------+----------+
| PM Stock Value       | 75095    |
| RM Stock Value       | 67865    |
+----------------------+----------+
| colhead1             | colhead2 |
+----------------------+----------+
| Product Stock Value  | 46456    |
| Total Stock Value    | 1446     |
+----------------------+----------+
| colhead1             | colhead2 |
+----------------------+----------+
| 75% of stock value   | 1835     |
| Investment           | 1000     |
+----------------------+----------+

Unfortunately, this solution can't print the line ** COMP ** STATEMENT as on 12/08/2021. So you could choose the solution of printing separate tables for every two rows.

Related