【问题标题】:Random displays of members images会员图片随机展示
【发布时间】:2018-07-09 02:00:40
【问题描述】:

如果我有一个包含 50 个点的数组,每个点包含 5 张图像,我想以表格格式随机显示每个点。每行一个点(5 张图像),总共 50 行。不使用数据库!

<?php
$allImages = array('Image1.jpg','Image2.jpg','Image3.jpg','Image4.jpg');
echo '<table>';
// loop for 12 rows
for($j=0; $j<12; $j++) {
    echo '<tr>';
    // loop to make 5 columns, 1 column for each image
    for($i=0; $i<5; $i++) {
        echo '<td>';
        $img = $allImages[rand(0,3)];
        echo '<img src="'.$img.'">';
        echo '</td>';
    } echo '</tr>';
}
echo '</table>';

?>

【问题讨论】:

  • 所以不会为你做功课。你试过什么?
  • $allImages = array('Image1.jpg','Image2.jpg','Image3.jpg','Image4.jpg');回声''; // 循环 12 行 for($j=0; $j'; // 循环生成 5 列,每个图像 1 列 for($i=0; $i'; $img = $allImages[rand(0,3)]; echo '';回声''; } 回声''; } 回声'';
  • 大声笑我喜欢它的措辞甚至完全像一个家庭作业问题。 “不使用数据库!”
  • 而他的代码是来自this post的另一个用户提供的代码...

标签: javascript php html-table


【解决方案1】:

试试这个:

<?php

$allImages = array('Image1.jpg','Image2.jpg','Image3.jpg','Image4.jpg',
                   'Image2.jpg','Image3.jpg','Image4.jpg','Image2.jpg',
                   'Image3.jpg','Image4.jpg','Image2.jpg','Image3.jpg',
                   'Image4.jpg'); 
shuffle ( $allImages );
echo '<table><tr>'; 
for($j=0; $j<count($allImages); $j++) { 
  echo '<td>'; 
  echo '<img src="'.$allImages[$j].'">'; 
  echo '</td>'; 
  if(($j+1)%5==0) echo "</tr><tr>";
} 
echo '</tr></table>';

?>

【讨论】:

  • 感谢您的帮助 Markus,但请记住,数组中列出的每个图像实际上是 5 个图像。我试图显示 5 个图像的组或组,因此每个阵列隔间每个点有 5 个图像,这意味着每组 5 个图像在一行上。再次感谢
  • 好的,我明白了,也许你可以为你的解决方案使用一些部件 ;-)
【解决方案2】:

这个怎么样?

<?php
$allImages = array(
        array ('Image01.jpg', 'Image02.jpg', 'Image03.jpg', 'Image04.jpg', 'Image05.jpg'),
        array ('Image11.jpg', 'Image12.jpg', 'Image13.jpg', 'Image14.jpg', 'Image15.jpg'),
        //.... So on upto 50 slots
        array ('Image501.jpg', 'Image502.jpg', 'Image503.jpg', 'Image504.jpg', 'Image505.jpg')
);

$spot_indexes = range(0,2,1); //in your case limit is 50
shuffle($spot_indexes);

foreach($spot_indexes as $index) {
    $spot_images = $allImages[$index];

    foreach($spot_images as $image) {
        echo '<img src ="' .$image. '" height="150px" width="100px" /> ' ;
    }
    echo '<br>';
}
?>

【讨论】:

  • 这一行有错误 $img = $allImages[$spot_indexes[$j][$i]];
  • @user2012105 现已修复。
  • 这就是答案:”; } ?>
  • @user2012105 是否随机显示图像?
  • @user2012105 How to ask Questions here 请完整阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多