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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.