Hi
My goal is to import/export the data from a structured field from/to an datasheet like Excel.
With babysteps I try to get closer to the goal.
I tried to follow this instructions:
First I created the helper in the plugins folder like mentioned in the instructions above.
/site/plugins/helpers/index.php
<?php
function csv(string $file, string $delimiter = ','): array
{
$lines = file($file);
$lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);
$csv = array_map(function($d) use($delimiter) {
return str_getcsv($d, $delimiter);
}, $lines);
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv);
return $csv;
}
Then I added this:
/site/models/home.php
<?php
class AnimalsPage extends Page
{
public function children()
{
$csv = csv($this->root() . '/home.csv', ';');
$children = array_map(function ($animal) {
return [
'slug' => Str::slug($animal['Scientific Name']),
'template' => 'animal',
'model' => 'animal',
'num' => 0,
'content' => [
'title' => $animal['Scientific Name'],
'commonName' => $animal['Common Name'],
'description' => $animal['Description'],
]
];
}, $csv);
return Pages::factory($children, $this);
}
}
The home.csv is placed to /content/home/.
Question here: Can home.csv have any name or does it have the same name as the page?
Then I added this to /site/templates/home.php
<ul class="animals">
<?php foreach ($page->children() as $animal): ?>
<li>
<a href="<?= $animal->url() ?>">
<?= $animal->title() ?>
</a>
</li>
<?php endforeach ?>
</ul>
But the ul-tag is empty.
What do I do wrong?
Edit:
Just found the mistake.
The Class in the Model has to be named like “PagenamePage”.
Hm, sorry to critize the documentation again. But why is this not mentioned there?
For a beginner who does not yet see the big picture, the documentation is quite difficult to follow, there are always knowledgegaps
If you think this is because a lack of my IQ, then please let me know and I will take back this ctritic
Kind regards
Alain