Jump to content

How to extract data from CSV and Assign to Variables in PHP script?


mahogan

Recommended Posts

I am very new to php and I have seen many posts on how to write to a csv file and several on how to fgetcsv data from a file, but none of it really makes sense and I cannot find a relevant example of what I am attempting to do.  I know that most will say, use mySQL, but for this example, I really want to know how to use a csv as a data source.  

 

I have a php script that creates an image gallery from the folder that it is placed.  It also gets and displays the embedded image attributes, filename, file dimensions, file size.  

 

Rather than display the file dimensions and file size, I want a corresponding csv file to display an image Category and other details from the csv.

 

 

The full original php script can be viewed here: http://www.mahogan.com/gallery.txt

The modified working model of this gallery can be viewed here: http://www.mahogan.com/sandbox/thumbs/gallery.php

Here is a php script that gets the data from the csv: http://www.mahogan.com/sandbox/thumbs/gallery-fgetcsv.php

echo "<html><body><table>\n\n";
$f = fopen("gallery-data.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "<tr>\n";
}
fclose($f);
echo "\n</table></body></html>";

In my examples of the csv, this just prints the entire csv in an html table as in the file.  

 

What I do not understand is this:

1) How to Match the Filename with the thumbnail filename in the folder so that all the data in that row will stay with that filename.

2) How to assign/replace this csv data to print these in place of the image attributes.

 

Any advice with using this CSV as a data source would be very appreciated as I really want to learn how to do this!  Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.