Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-数组和Div?_Php_Html - Fatal编程技术网

PHP-数组和Div?

PHP-数组和Div?,php,html,Php,Html,我怎样才能赚到一美元。div id-取决于它的值 当我检查源代码时,div id始终是ImgDiv-0,如何将其设置为1,2,3 顺便说一下,这段代码生成3个div。这只是代码的一部分。printf在末尾,我只需要编辑这个块 如果你需要剩下的代码,我很乐意发布 这些解决方案都不管用:也许我可以发布完整的代码 <?php $max = 3; for ($i=0; $i<= $max; $i++) { define('RANDOM_IMAGES_COUNT2',3); define('R

我怎样才能赚到一美元。div id-取决于它的值

当我检查源代码时,div id始终是ImgDiv-0,如何将其设置为1,2,3

顺便说一下,这段代码生成3个div。这只是代码的一部分。printf在末尾,我只需要编辑这个块

如果你需要剩下的代码,我很乐意发布

这些解决方案都不管用:也许我可以发布完整的代码

<?php
$max = 3;
for ($i=0; $i<= $max; $i++) {
define('RANDOM_IMAGES_COUNT2',3);
define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$i. '"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'] );    }
?>

我认为问题可能在于您没有正确使用for循环

For循环具有以下语法:

for (expr1; expr2; expr3)
    statement
在哪里

expr1在循环之前按条件执行。 expr2在迭代之前求值,如果求值结果为true,则执行该语句。 expr2在每个循环结束时执行。 因此,试试看

<?php
$max = 3;
for ($i=0; $i<= $max; $i++) {
    define('RANDOM_IMAGES_COUNT2',3);
    define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$i. '"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>');
}
?>
如果您知道应该显示的div数量,只需取出$max并使用一个固定数字:


编辑:for循环中的进一步信息:

您的for循环不完整,可能会抛出一个解析器错误关于for循环,正确的语法是:for$i=0$你在用IDE吗?只是修改了一些不正确的代码。谢谢你。