drd99007 2012-02-19 20:24
浏览 37

如何用PHP随机化图像数组

I am after a little help here. I need to randomize an image array's order.

For example, if I have 3 images, I need them to reload each time in a different order.

Also, this is the instructions given to me on how to implement the code:

If this is PHP code, use a return statement to send your output to the block. For example:
$my_output = 'Hello, world.';
return $my_output;

My current code is HTML, and goes like this:

<div class="ad">

<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png"      alt="linkedsitename" /></a>

<br />
<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>

<br />
<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png"      alt="linkedsitename" /></a>

</div>

What I need to do is somehow turn my HTML code into a PHP code that allows the images order to be randomized, while maintaining the original instructions to "Use an output statement" to send my code to the sidebar.

Thanks! and I wish I knew more

  • 写回答

2条回答 默认 最新

  • dongyizhui0616 2012-02-19 20:31
    关注

    If you really need a solution before really understanding the PHP basics, here it is:

    <?php
    $images =   array(
                '<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>',
                '<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>',
                '<a href="http://www.linkedsite.com/" target="_blank"><img src="myimagesdirectpath/img.png" alt="linkedsitename" /></a>'
            );
    
    shuffle($images); // Randomize images array
    return '<div class="ad">'.implode('<br />', $images).'</div>';
    ?>
    

    But to be honest the suggestion is always to read some PHP tutorial like this.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效