代码之家  ›  专栏  ›  技术社区  ›  Nardong Bagsik

如何右对齐除前2列之外的所有列?

  •  0
  • Nardong Bagsik  · 技术社区  · 7 年前

    我有一个有27列的表,我正在使用 fpdf

    我想知道如何使除前2列外的所有列都向右对齐?

    这是我的密码。

    #Create the table
        function BasicTable($header,$data) {
            #Create the header.
            foreach ($header as $col)
                $this->Cell(18,5,$col,1);
                $this->Ln();
    
                #Get the data
                foreach ($data as $row) {
                    foreach ($row as $col) 
                        #$this->Cell(18,5,$col,1,'R');
                        $this->Cell(18,5, $col, 1, 0); 
                    $this->Ln();
                }
            }
        }
    

    更新代码(工作)

     #Create the table
        function BasicTable($header,$data) {
            #Create the header.
            foreach ($header as $col)
                $this->Cell(18,5,$col,1);
                $this->Ln();
    
                #Get the data
        foreach ($data as $row) {
            $cnt = 0;
            foreach ($row as $col) {
                if($cnt < 2){
                  $this->Cell(18,5,$col,1);
                }
                else {
                  $this->Cell(18,5, $col, 1, 0,'R'); 
                }
                $cnt++;
            }
         $this->Ln();   
         }
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Nardong Bagsik    7 年前

    您应该检查每行的列值,

    #Create the table
    
    function BasicTable($header,$data) {
        #Create the header.
        foreach ($header as $col)
            $this->Cell(18,5,$col,1);
        $this->Ln();
    
        #Get the data
        foreach ($data as $row) {
            $cnt = 0;
            foreach ($row as $col) {
                if($cnt < 2){
                  $this->Cell(18,5,$col,1,'R');
                }
                else {
                  $this->Cell(18,5, $col, 1, 0); 
                }
                $cnt++;
            }
         $this->Ln();   
         }
    }
    

    我还在函数中发现了额外的“}”。

    根据上述帖子更新代码

    #Create the table
        function BasicTable($header,$data) {
            #Create the header.
            foreach ($header as $col)
                $this->Cell(18,5,$col,1);
                $this->Ln();
    
                #Get the data
        foreach ($data as $row) {
            $cnt = 0;
            foreach ($row as $col) {
                if($cnt < 2){
                  $this->Cell(18,5,$col,1);
                }
                else {
                  $this->Cell(18,5, $col, 1, 0,'R'); 
                }
                $cnt++;
            }
         $this->Ln();   
         }
            }
        }