donglou1866 2013-12-09 22:09
浏览 27
已采纳

PHP Switch案例帮助。 应该简单吗?

i'm creating this banner rotating PHP script (I'm new to PHP) and was wondering why my cases aren't working. I can get the random image to show via the array although I can't seem to make the cases work.

I would like it so if the image from the random array is the 0 image, it will say visit jackhair.co.uk and if the random image from the array is 2 it will say jamiehair.co.uk

<?php
/*Creates an array called banners which holds all the images of banners */
$banners=array("img/reversessh.jpg","img/failtwo.jpg","img/iptable.jpg");
$random = ($banners[(rand(0,2))]);
$visit = $random;

switch($visit)
{
case $visit = 0:
    Echo "Visit www.jackhair.co.uk";
    break;
case $visit = 1:
    Echo "Visit www.lukehair.co.uk";
    break;
case $visit = 2:
     Echo "Visit www.jamiehair.co.uk";
    break;
default:
    null;
}

?>

<img src="<?= $random ?>" alt="Rotating Banners" />

Hope you can help! Thankyou.

  • 写回答

2条回答 默认 最新

  • dqvrlgi3247 2013-12-09 22:12
    关注

    Do:

    $visit = rand(0,2);
    $random = $banners[$visit];
    

    Instead of other way around. That way you keep the int value random in the $visit variable and not a image url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?