1

此 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>
4

2 回答 2

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>');
于 2013-09-04T18:49:19.910 回答
0

试试这个:

工作键盘示例

请注意,我替换了您的 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>
于 2013-09-04T19:19:08.427 回答