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

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

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

Related

How to fill array in sequence?

I have the following iterration:
foreach ($filelines as $indexrow => $line) {
$columns = explode($splitter, $line);
$row = [];
foreach ($columns as $columnindex => $column) {
$column = $this->prepareValue($column);
if (RegexValidation::isNum($column)) {
$row[] = $this->setValue('num', $column, $indexrow, $columnindex);
} else {
if (RegexValidation::isMMYY($column)) {
$row[] = $this->setValue("mmyy", $column, $indexrow, $columnindex);
} else {
if (RegexValidation::isZip($column)) {
$row[] = $this->setValue("zip", $column, $indexrow, $columnindex);
}
}
}
}
$dataset[$indexrow] = $row;
}
I need to fill $row in a specific sequence, first is "zip," then "mmyy," then "num," etc. If there is no field, fill it as empty.
How to do this in the existing loop? Now I have a solution sort result array, but It needs to loop again.
I would use a special key and sort by the key. And - there is an elseif.
foreach ($filelines as $indexrow => $line) {
$columns = explode($splitter, $line);
$row = [];
foreach ($columns as $columnindex => $column) {
$column = $this->prepareValue($column);
$counter = 0;
if (RegexValidation::isNum($column)) {
$row['C_'.$counter++] = $this->setValue('num', $column, $indexrow, $columnindex);
} elseif (RegexValidation::isMMYY($column)) {
$row['B_'.$counter++] = $this->setValue("mmyy", $column, $indexrow, $columnindex);
} elseif (RegexValidation::isZip($column)) {
$row['A_'.$counter++] = $this->setValue("zip", $column, $indexrow, $columnindex);
} else { // If there is no field, fill it as empty
$row['Z_'.$counter++] = '';
}
}
ksort($row);
$dataset[$indexrow] = array_values($row);
}
Use A for the group you want to have first, then B for the group you want next, and so on. If you want to preserve the order within the group, you should have leading zeros on the counter, because A_10 ist sorted before A_9. Change that to A_09, or A_000009 depending on the size of your dataset.

Bold the last row in table

How can I make the last row Bold in fpdf?
here is my code
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
Updated Code : Added conditions for the rows until reach the last row.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}
The sample code above creates the row and column my target is to make the last row bold
Simply, you can count() rows and then check if current processed row is the last one.
$totalRows = count($data);
$currentRow = 1;
foreach ($data as $row) {
// (...)
if ($currentRow === $totalRows) {
// this is the last row
}
$currentRow++;
}
Here is the working code.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}

Searching in Excel with PHP

I am trying to find the last cells value of a row in an Excel table by the first cells value using PHPExcel. The user enters the value of the first cell in the row (which is the "command" parameter) and if it exists in the table my function is supposed to jump to the last cell of the row and return the value. This is my code:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
foreach ($row->getCellIterator() as $cell) {
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
if(strcmp($head, $command)) {
for ($i = 0; $i == 8; $i++) {
$celliterator->next();
}
return $cell->getValue();
}
}
}
}
return false;
}
For some reason it always returns wrong.
I solved it myself, this code functions correctly:
public function search($command, $excelobj) {
foreach ($excelobj->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$rowiterator = $worksheet->getRowIterator();
$celliterator = $row->getCellIterator();
$celliterator->setIterateOnlyExistingCells(true);
$head = $celliterator->current()->getValue();
while (strcmp($head, $command) !== 0 && $rowiterator->valid()) {
$rowiterator->next();
$row = new PHPExcel_Worksheet_Row($worksheet, $rowiterator->key());
$celliterator = $row->getCellIterator();
$head = $celliterator->current()->getValue();
}
for ($i = 0; $i <= 7; $i++) {
$celliterator->next();
}
return $celliterator->current()->getValue();
}
}
return false;
}

how to select specific column order in PHPExcel

I want to select and write specific column names as I want how can I do that ?
the situation is :
$stockQuery = mysql_query("SELECT stokId,urunAdi,tlFiyat,stokMiktari,tlFiyat*stokMiktari,urunGrup,maliyetTipi FROM proje_stok where projeID =".$projeID);
but I do want to select lik
select * from proje_stok where ......
and my current writing code is
while($row_data = mysql_fetch_assoc($stockQuery)) {
$col = 0;
if ($col == 0) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order);
$col++;
}
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
$order++;
}
Instead of that I want to use the code like
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value['HEREWHATIWANTTOPUTCOLUMNNAME']);
$col++;
}
is there any suggestion to deal with ?
regards
$col = $row = $order = 1;
if ($row_data = mysql_fetch_assoc($stockQuery))
{
foreach ($row_data as $key => $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $key);
do
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col = 1, ++$row, $order++);
foreach ($row_data as $value)
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(++$col, $row, $value);
}
while ($row_data = mysql_fetch_assoc($stockQuery));
}

Getting the headers in PHPExcel

I am using this piece of code in PHP to query a database and import the data to an excel file. Currently I am getting the data from the database, but I can't get the headers.
Can anyone tell me how to get the headers from the database?
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
$objPHPExcel = new PHPExcel();
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$row = 1;
$col++;
}
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}

Categories