【问题标题】:repeat headers after each two row in my table在我的表中每两行之后重复标题
【发布时间】:2021-08-20 07:40:22
【问题描述】:

我正在使用 lucidFrame 控制台表来创建使用 json 的输出。我下面的代码是

$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);

我的结果是

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

我想在每两行之后重复标题。我怎样才能做到这一点 ?请帮忙 我想要的输出是这样的

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

提前致谢

【问题讨论】:

    标签: php console phplucidframe


    【解决方案1】:

    在您的主 foreach 循环中,您可以检查 $i 是否为奇数,如下所示:

    ($i % 2) === 1

    如果上述陈述为真,请打印您的标题。 您可以阅读有关modulo (%) 算术运算符here 的更多信息。

    编辑:

    你甚至可以简化这个,因为$i % 2的结果要么是0要么是1,你可以放

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

    【讨论】:

      【解决方案2】:

      该库尚不支持在一个表中打印重复的标题。您只能按每两行打印单独的表格,例如,

      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();
      

      这将打印为

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

      您可以在每两行($i % 2 == 0) 之后使用addRow()addBorderLine() 打印标题。例如,

      $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();
      

      会这样显示

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

      很遗憾,此解决方案无法打印** COMP ** STATEMENT as on 12/08/2021 行。因此,您可以选择每两行打印单独的表格的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        相关资源
        最近更新 更多