Answer the question
In order to leave comments, you need to log in
How to loop through table columns in PHP?
Task: loop through all columns. This code works great as long as it $last_column
contains one character.
$last_column = 'AM';
for ($column = 'A'; $column <= $last_column; $column++) {
echo "$column ";
}
$last_column
2 characters, instead of:ABC ... XYZ AA AB AC ...AK AL AM
A
Answer the question
In order to leave comments, you need to log in
$last_column = 'AM';
$column_number = 0;
do {
$column_name = (($t = floor($column_number / 26)) == 0 ? '' : chr(ord('A')+$t-1)).
chr(ord('A')+floor($column_number % 26));
echo "{$column_name} ";
$column_number++;
} while ($column_name != $last_column);
Surely the author is not relevant), but for history, let it be ...
$last_column = 'AM';
for ($column = 'A'; $column != $last_column; $column++) {
echo "$column ";
}
//или
$column = 'A';
$last_column = 'AM';
while ($column != $last_column) {
echo $column.' ';
$column++;
}
//а в частном случае обычно я использую так
//$columnNamesArr массив с именами столбцов
$column_name = 'A';
foreach ($columnNamesArr as $columnName) {
$objPHPExcel->getActiveSheet()->SetCellValue($column_name.'1', $columName);
$objPHPExcel->getActiveSheet()->getColumnDimension($column_name)->setAutoSize(true);
$column_name++;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question