Question

So I downloaded and really enjoyed getting started with Kirby http://www.getkirby.com For those not familiar it is a file based cms.

As standard you can drop a bunch of images in a directory e.g. site/number1 and it will make a gallery for you on a page called site.com/number1. If one of your images is called example.jpg you can make a file called example.txt and put inside information relating to the image such as title, date etc.

I wasn't totally happy with the idea of having a text file for each individual image and having the images scattered around the server in the page folders.

First of all I put a large collection of images in assets and added a variable to the page so I just need to add the image ref e.g. k-45 to a list in the page.txt e.g.

----

Imagelist: a-04, a-08, h-16, i-35

I then added the following snippet:

 <ul class="gallery">
  <?php $imagelist= $page->imagelist();
  if ($imagelist != ''){
$imagelistitem= explode(", ", $imagelist);
foreach ($imagelistitem  as $image): ?>
  <li><img src="<?php echo url('assets/artistswork/450/').$image.'.jpg' ?>" /></li>
      <?php endforeach ;} ?>
    </ul>

This generates an image gallery from the central bank and works great. Sure someone more experienced than me could add some useful validation/error correction but as long as the list is written error free it works fine.

So far this is essentially a Kirby issue, this where my question begins.

Next I wanted to draw meta data for the images from a central csv sheet. E.g title, date, material ect. fields related to the $image variable.

In my csv the first column stock book code will have a figure that is equivalent to the $image variable and I want to use the rest of the information in that row to populate a series of spans that I can format and possibly animate using jquery and parse to fancy box etc.

  1. I need a better piece of code to generate an effective array from my csv. Possibly indexing the results by the stock book number
  2. And a method to query it and parse out the relevant array as a list of variable to use in the injection.

I imagined I could inject the code into the above quoted repeater.

<span class="workTitle">$title</span>
<span class="workDate">$date</span>
<span class="workMaterial">$material</span>
<span class="workDimensions">$dimensions</span>
<span class="workPrice">$price</span>

The first few lines of my csv are:

Stock book code,Date,Work Title,height,width,materials,Ownership
a-04,1962,River Barge,0,0,Oil on Board,Sold
a-08,1962,Thames Bridge Fantasy ,48,36,Oil on Board,Available
h-15,1963,Split Rocks,36,32,Oil on Board,Available
i-35,1964,Moon Dyptych,32,21,Oil on Board,Available

I haven't really got much of a clue how to finish this off. I know it's probably not super difficult but I'm no expert, certainly not when dealing with arrays. The sheet is on the server and using some php I've managed to print the csv-array result but without the header's being the array titles, they were just treated as a line, the first array in the larger array.

So really I'm looking at starting from scratch for the second part of my problem - not expecting a full cut and paste solution by any means but if someone could point me in the right direction it would be great. I haven't used j-query and don't know too much php. However I don't mind doing a lot of reading and spending the time making it work myself but I could do with a point in the right direction.

The in-complete csv-array function I found was as follows. May have to start from stratch with it:

<?PHP
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}


// Set path to CSV file
$csvFile = 'http://localhost/peterliddle/assets/artistswork/imagemeta.csv';

$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>

This out puts this (and 300 more):

Array
(
    [0] => Array
        (
            [0] => Stock book code
            [1] => Date
            [2] => Work Title
            [3] => height
            [4] => width
            [5] => materials
            [6] => Ownership
        )

    [1] => Array
        (
            [0] => a-04
            [1] => 1962
            [2] => River Barge
            [3] => 0
            [4] => 0
            [5] => Oil on Board
            [6] => Sold
        )

    [2] => Array
        (
            [0] => a-08
            [1] => 1962
            [2] => Thames Bridge Fantasy 
            [3] => 48
            [4] => 36
            [5] => Oil on Board
            [6] => Available
        )

    [3] => Array
        (
            [0] => h-15
            [1] => 1963
            [2] => Split Rocks
            [3] => 36
            [4] => 32
            [5] => Oil on Board
            [6] => Available
        )
Was it helpful?

Solution

Firstly I'd say if you don't need to edit the files after they are created, and you're just starting out, then using csv files is a valid way to go. Databases introduce their own set of problems, like security, which are much easier to avoid with csv files. Just make sure all the files are read only, and you'll be fine.

Secondly, I'd be tempted to use the inbuilt php function fgetcsv to parse your csv files. I don't know for sure, but I'd hazard a guess it's probably quicker.

As far as I can see there are two ways of going about doing this. The first is to simply extract the data you need into an associative array containing the values you need, using hte header rows as the key values. So, having got your array of values from the csv file, lets call it $imageMeta, you might do something like;

foreach ($imageMeta as $row => $meta)
{
    if ($row === 0 && is_array($meta)) //Reading the first line with your header rows
    {
        $tempMeta = $meta; //A temporary holder for the field headers.
    }else{
        foreach ($meta as $key => $field) //Now we go through each meta row and extract the field data
        {
            $finalMeta[$row-1][$tempMeta[$key]] = $field;
        }
    }
}

That's a brute force approach, I'll try and come up with something more elegant for you to look at.

OTHER TIPS

Looks like Joe provided a good solution also got the some code from https://gist.github.com/jaywilliams/385876 to work by using a relative path rather than an absolute via localhost/...

If I have a specific problem I'll post again for that and close this general one

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top