2

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);
?>
2
  • 2
    You are setting $color outside of your loop, but never updating the value inside the loop. You could probably just change $color to $data[3] inside the loop and you would be fine. Commented Jul 7, 2014 at 19:55
  • Sure enough, that fixed it...thanks for the input. If you add as an answer I'll accept. Commented Jul 7, 2014 at 20:00

2 Answers 2

2

your $color and $options variables are currently just the column headers.

Change you code to something like this:

<?php
    $handle = fopen("cars.csv", "r");
    $data = fgetcsv($handle, 1000, ",");
    $color = $data[3];
    $options = $data[5];
    echo('<table>');
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $color = $data[3];
        $options = $data[5];
        //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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Scott thanks for the answer - I upvoted but saw Jonathan's first, so I've accepted his
0

You are setting $color outside of your loop, but never updating the value inside the loop. You could probably just change $color to $data[3] inside the loop and you would be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.