r/PHPhelp • • Nov 13 '17

csv to php with html table

I have a csv file with product data in it. I have this code written that returns only the columns I need.

I would like to have it so I could restrict the products shown by "type"($data[8]) but I'm struggling with where to put the correct statement.

<?php $handle = fopen("products.csv", "r"); ?> 
<table> 
    <thead> 
        <th> 
            SKU 
        </th> 
        <th> 
            Type
        </th> 
        <th> 
            Name
        </th> 
        <th> 
            Variation 1
        </th> 
    </thead> 
    <tbody> 

<?php 
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE): ?> 
    <tr> 
        <td><?= $data[2] ?></td> 
        <td><?= $data[8] ?></td> 
		<td><?= $data[6] ?></td> 
		<td><?= $data[10] ?></td>         
    </tr> 
<?php endwhile; ?> 

    </tbody> 
</table>                        
7 Upvotes

3 comments sorted by

3

u/sveach Nov 13 '17

Inside your while loop (which is retrieving rows from the csv file) throw an IF loop in there - if $data[8] == "whatever" then write out the row, otherwise don't write out the row of data.

3

u/Scuz Nov 13 '17

You know when you look at stuff and over do it, yeah that happened. Thanks mate

2

u/sveach Nov 13 '17

No worries! I'm the type that needs to see it once or twice before I figure it out. :)