I am trying to see if I can convert a for loop to a foreach loop. Reason: Because I would like to make this code more generic and stay away from magic numbers. Although I know the numbers of column in the dataset, I would prefer to make the code more generic. I have tried using the end() and next() functions to try and detect the last element in the DOMNodeList, but I am not having success.
My final output will be in a CSV format with enclosures like so,
"Value 1","Value 2","Value 3","Value 4","Value 5","Value 6","Value 7","Value 8"
Here is my original for loop:
$cols = $row->getElementsByTagName("td");
$printData = true;
// Throw away the header row
if ($isFirst && $printData) {
$isFirst = false;
continue;
}
for ($i = 0; $i <= 8; $i++) {
$output = iconv("UTF-8", "ASCII//IGNORE", $cols->item($i)->nodeValue);
$output2 = trim($output);
if ($i == 8) {
// Last Column
echo "\"" . $output2 . "\"" . "\n";
} else {
echo "\"" . $output2 . "\"" . ",";
}
}