doukanxi4246 2013-09-06 19:37
浏览 25
已采纳

如何让PHP命名这个div?

So I want php to name the div whatever the value of B is. Example: if b = 1, I want the div id to be "imgDiv-1". The code generates 3 divs, and each div has to have a different name. However, this code makes every div id to be "imgDiv-3" Code:

<?php
for ($b=3; $b > 4; $b++) {
    define('RANDOM_IMAGES_COUNT2',3);
    define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$b. '"style="width:170px; height:auto;float:left;text-align:center;top"><img src="%s" style="border-style:solid;border-width:2px;border-color:black;"/><a href="%s" alt="%s" title2="%s">%s</a></div>');
}

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

$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'] );    }
?>
  • 写回答

4条回答 默认 最新

  • dpmfur2635 2013-09-06 19:40
    关注

    Your code has multiple issues.

    Firstly, your loop never runs. You initalize $b to 3 and have it run while $b > 4, which is never the case. You probably wanted this: for ($b=1; $b < 4; $b++)

    Secondly, you need to understand what define() does. It defines a constant, which cannot be changed. So setting it within a loop is a no-no.

    Perhaps this is what you wanted?

    define('RANDOM_IMAGES_COUNT', 3);
    define('RANDOM_IMAGES_FORMAT', '
        <div id="imgDiv-%s" style="width:170px; height:auto; float:left; text-align:center;">
            <img src="%s" style="border-style:solid; border-width:2px; border-color:black;" />
            <a href="%s" alt="%s" title2="%s">%s</a>
        </div>
    ');
    
    $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_COUNT) {
        trigger_error('Not enough images given', E_USER_WARNING);
        exit;
    }
    shuffle($images);
    for ($i=1; $i<=RANDOM_IMAGES_COUNT; $i++) {
        printf(
            RANDOM_IMAGES_FORMAT,
            $i,
            $images[$i]['src2'],
            $images[$i]['href2'],
            $images[$i]['title2'],
            $images[$i]['title2'],
            $images[$i]['text2']
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数