gpt4 book ai didi

php - 我怎样才能让 PHP 命名这个 div?

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:08 25 4
gpt4 key购买 nike

因此,无论 B 的值是多少,我都希望 php 为 div 命名。示例:如果 b = 1,我希望 div id 为“imgDiv-1”。代码生成 3 个 div,每个 div 必须有不同的名称。但是,此代码使每个 div id 都为“imgDiv-3”代码:

<?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'] ); }
?>

最佳答案

您的代码有多个问题。

首先,您的循环永远不会运行。你初始化 $b到 3 并让它在 $b > 4 时运行,但事实并非如此。你可能想要这个:for ($b=1; $b < 4; $b++)

其次,你需要了解什么是 define() 做。它定义了一个不能更改的常量。所以在循环中设置它是不行的。

也许这就是你想要的?

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']
);
}

关于php - 我怎样才能让 PHP 命名这个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18665346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com