我正在创建一个bootstrap 3图像库,其中有很多重复的div,看起来像这样:
<figure class="col-1 picture-item" data-groups='["groupA"]' data-date-created="2018" data-title="imageA">
<div class="picture-item__inner">
<div class="aspect aspect--60x60">
<div class="aspect__inner">
<img class="myImg" src="imageA.gif" alt="description of image A"/>
</div>
</div>
<div class="picture-item__details">
<div class="picture-item__tags" style="display: none;">image A keywords</div>
<p class="picture-item__title">Image A</p>
</div>
</div>
</figure>
<figure class="col-1 picture-item" data-groups='["groupB"]' data-date-created="2019" data-title="imageB">
<div class="picture-item__inner">
<div class="aspect aspect--60x60">
<div class="aspect__inner">
<img class="myImg" src="imageB.gif" alt="description of image B"/>
</div>
</div>
<div class="picture-item__details">
<div class="picture-item__tags" style="display: none;">image B keywords</div>
<p class="picture-item__title">Image B</p>
</div>
</div>
</figure>
我希望通过从CSV文件中提取数据来创建多个Divs来节省一些时间和编码。不断扩展的画廊已经有一百多个图像。上述CSV数据看起来像这样:
Image A,imageA,groupA,2018,description of image A,image A keywords
Image B,imageB,groupB,2018,description of image B,image B keywords
^等...
我希望提供一些帮助或建议的建议,以实现一个方案,我只需要使用正确的命名约定上传图像并更新电子表格即可。我的知识有限,但是我认为PHP可能是这样做的方法吗?
我希望这个问题有意义。提前致谢。
我设法使此操作:
<?php
$fileHandle = fopen("gallery-data.csv", "r");
while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {
?>
<figure class="col-1 picture-item" data-groups='["<?php echo $row[3] ?>", "<?php echo $row[5] ?>"]' data-date-created="<?php echo $row[2] ?>" data-title="<?php echo $row[4] ?>">
<div class="picture-item__inner">
<div class="aspect aspect--60x60">
<div class="aspect__inner">
<img class="myImg" src="gallery/<?php echo $row[4] . '/' . $row[0] . '.gif" alt="' . $row[6] ?>"/>
</div>
</div>
<div class="picture-item__details">
<div class="picture-item__tags" style="display: none;"><?php echo $row[7] ?></div>
<p class="picture-item__title"><?php echo $row[0] ?></p>
</div>
</div>
</figure>
<?php
}
fclose($file);
?>
PHP具有两个用于Parse CSV的功能:
fgetCSV(文件,长度,分离器,外壳)和str_getcsv(字符串,分离器,外壳,逃生)
FGETCSV样本:
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
http://php.net/manual/en/function.fgetcsv.php