如何将这些数组图像放入单独的div中?

时间:2013-09-04 18:45:16

标签: php image html

这个PHP代码生成三张图片,带有三个引号。过去几天我一直在努力,所以如何让它将每个图像和文本生成一个新的div标签? 这是代码:

<div id="randown" style="width: 475px; left: 10px; top:  15px; height: 89px;"class="style4">
<?php
define('RANDOM_IMAGES_COUNT2',3);
define('RANDOM_IMAGES_FORMAT2', '<img src="%s" style="width:177px;height:100px;border-   style:solid;border-width:2px;border-color:black;" /><a href="%s" alt="%s" title2="%s">%s</a>');

#------------------------------------------------------------------------------

$images = array (
    array ( 'title2' => 'Test 2', 'src2' => 'pic2.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic7.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic9.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ), 
    array ( 'title2' => 'Test 2', 'src2' => 'pic5.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),     
    array ( 'title2' => 'Test 2', 'src2' => 'pic3.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello3' )
);

#------------------------------------------------------------------------------

if ( count($images) < RANDOM_IMAGES_COUNT2 ) {
    trigger_error('Not enough images given', E_USER_WARNING);
    exit;
}

#------------------------------------------------------------------------------

for ($i = 0; $i < RANDOM_IMAGES_COUNT2; $i++) {
    shuffle($images);

    $tmp = array_shift($images);
    printf( RANDOM_IMAGES_FORMAT2, $tmp['src2'], $tmp['href2'], $tmp['title2'],     $tmp['title2'],$tmp['text2'] );    }
?>
</div>

2 个答案:

答案 0 :(得分:1)

在常量定义中添加周围的div标签:

define('RANDOM_IMAGES_FORMAT2', '<div><img src="%s" style="width:177px;height:100px;border-   style:solid;border-width:2px;border-color:black;" /><a href="%s" alt="%s" title2="%s">%s</a></div>');

答案 1 :(得分:0)

试试这个:

Working codepad example

请注意,我更换了你的href和jpeg图像的名称,以便在我的系统上测试/解决。我很信任,很容易改回来。

<?php
    $images = array (
        array ( 'title1' => 'Test 2', 'src2' => '50x50', 'href2' => 'http://placehold.it/','text2' => 'Hello1' ),
        array ( 'title2' => 'Test 2', 'src2' => '75x75', 'href2' => 'http://placehold.it/','text2' => 'Hello2' ),
        array ( 'title3' => 'Test 2', 'src2' => '100x100', 'href2' => 'http://placehold.it/','text2' => 'Hello3' ), 
        array ( 'title4' => 'Test 2', 'src2' => '150x150', 'href2' => 'http://placehold.it/','text2' => 'Hello4' ),     
        array ( 'title5' => 'Test 2', 'src2' => '200x200', 'href2' => 'http://placehold.it/','text2' => 'Hello5' )
    );

    $cnt = count($images);


    $r = '';
    for ($i=0; $i<=$cnt; $i++) {
        $r .= '<div id="imgDiv-' .$i. '" style="padding:10px;">';
        $r .= '<img id="img-' .$i. '" src="';
        $r .= $images[$i]['href2'] . $images[$i]['src2'];
        $r .= '" />';
        $r .= '</div>';
    }

?>

<html>

    <body>

        Here is the PHP: <br>
        <?php echo $r; ?>

    </body>
</html>