-1

我需要在网页中放置一大堆(数百个)图形元素。我想使用 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);
}

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

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

4

2 回答 2

2

页面显示会很慢。直接来自 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 标签

于 2012-12-30T23:28:33.230 回答
0

内联样式更容易,例如: <div style="height:40px;width:60px;..."></div>

最好按 id 为每个元素创建单独的 css 规则

于 2012-12-30T23:22:04.670 回答