Nardong Bagsik
Nardong Bagsik

Reputation: 228

How to align right all columns except the 1st 2?

I have a table that has 27 columns and I am using fpdf to create pdf file.

I wonder how can I make all the columns align right except the 1st 2?

Here is my code.

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

Update Code (Working)

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

Upvotes: 0

Views: 53

Answers (1)

Saurabh
Saurabh

Reputation: 131

You should check col value on each row,

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

I also found extra "}" in your function.

Updated code based on post above

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

Upvotes: 2

Related Questions