php - 在 php 中使用循环创建 css/html 元素的想法

标签 php html mysql css loops

<分区>

我需要在网页中放置一大堆(数百个)图形元素。我想使用 html 和 css,但要使用 PHP 脚本(使用循环),这样我就不必手动执行了。 我可以创建一个这样的表(例如,以逗号分隔),我可以导入到 Mysql:

Element,Width,Height,x-position,y-position,image
1,24,26 30,40,Photo1.jpg
2,46,34,50,78,Photo2.jpg

更多的数据点。

我需要生成的实际代码是(例如):

#element1{
height: 26px;
width: 24px;
top: 40px;
right: 30px;
background-image: url(Photo1.jpg);
}

当然会有一堆这样的值,从表中读取值。

我是编程新手。提前感谢您的所有建议和想法。

最佳答案

页面显示会很慢。直接来自 CSV 并使用内联 css 而不是每个元素一组规则

PHP

$csv=file_get_contents('thecsvfile.csv');
$images=explode("\n", $csv);
unset($csv); // don't want this in memory any more

foreach ($images as $image) {
    $bits=explode(',', $image);
    # 0:Element, 1:Width, 2:Height, 3:x-position, 4:y-position, 5:image
    echo '<img src="'. $bits[5] .'" style="position:absolute; width:' 
        . $bits[1] .'px; height:' . $bits[2] .'px; left:'
        . $bits[3] .'px; top: ' . $bits[4] .'px;" alt="' . $bits[0] .'" />';
}

编辑为使用 img 标签

关于php - 在 php 中使用循环创建 css/html 元素的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14095683/

上一篇:jquery - 水平菜单与 css 或 jquery 对齐

下一篇:css - 使用 css 创建一个 facebook 风格的状态更新框?

相关文章:

php - 如何处理删除 JSON :API? 中的关系

php - 数据库登录密码

Mysql查询: Where clause if Exists

php - 循环sql查询时

mysql - 提取具有多行的组

php - 每周编制年度工作规模,无需连续工作两周

php - CodeIgniter $this->load->vars() 函数

html - 响应时两个 Bootstrap 行未正确对齐

html5 - 如何在响应式布局中禁用水平滚动

javascript - 通过 ID 获取动态创建的元素 (Jquery)