I'm using fgetcsv() to read a csv file of this format...
Make, Model, Origin, Color, Miles, Options
Chevy, Tahoe, Delaware, Black, 10000, LX
Ford, F150, Texas, Red, 5000, S
Chevy, Corvette, Utah, Red, 12000, SE
Mazda, Miata, Florida, Blue, 90000, LX
...and then generate an HTML table. Although I can get each data value into an individual cell, I'd like to list the model's color and options values as HTML data-attributes. So my current output looks like this:
<table>
<tr>
<td>Chevy</td>
<td>Tahoe</td>
<td>Delaware</td>
<td>Black</td>
<td>10000</td>
<td>LX</td>
</tr>
<tr>
<td>Ford</td>
<td>F150</td>
<td>Texas</td>
<td>Red</td>
<td>5000</td>
<td>S</td>
</tr>
</table>
When I'd like it to look like this:
<table>
<tr data-color="black" data-options="LX">
<td>Chevy</td>
<td>Tahoe</td>
<td>Delaware</td>
<td>10000</td>
</tr>
<tr data-color="red" data-options="s">
<td>Ford</td>
<td>F150</td>
<td>Texas</td>
<td>5000</td>
</tr>
</table>
However, the PHP I'm using now just supplies the data-attribute value of "color" (from the labels row) as the attribute on every <tr>. For example,
<tr data-color="Color" data-options="Options">
<td>Ford</td>
<td>F150</td>
<td>Texas</td>
<td>5000</td>
</tr>
Does anyone have recommendations to get the appropriate value from the array as the data-attribute for each row? Here's my current PHP:
<?php
$handle = fopen("cars.csv", "r");
$data = fgetcsv($handle, 1000, ",");
$color = $data[3];
$options = $data[5];
echo('<table>');
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//generate HTML
echo('<tr data-color="' . $color . '" data-options="' . $options . '">');
foreach ($data as $index=>$val) {
echo('<td>');
echo htmlentities($val, ENT_QUOTES);
echo('</td>');
}
echo('</tr>');
}
echo("</table>");
fclose($handle);
?>
$coloroutside of your loop, but never updating the value inside the loop. You could probably just change$colorto$data[3]inside the loop and you would be fine.