채택완료
sub 페이지가 열릴 때 마다 mt_rand()를 이용하여 나오게 할 수 있을까요?
아래 그림처럼 Main Page(index)일 때는 좌우측배너가 side1, side2가 나오도록 하고
나머지는 모두 side3, side4 페이지가 나오도록 하였습니다.
아래 코드로 출력을 하면 페이지 오픈 때마다 랜덤값이 출력되는데요
<?php echo 'mt_rand(1, 4) : '.mt_rand(1, 4).'<br>'; ?>
mt_rand(1,4) 이 함수를 이용하면 될듯한데 어떻게 적용을 하면 될까요?

Copy
<?php if(defined('_INDEX_')) { // index에서만 실행 ?><div style="position:absolute; width:160px; left:50%; margin-left:-710px; margin-top:55px; z-index:999;"> <iframe src="/banner/side2.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div><div style="position:absolute; width:160px; left:50%; margin-left:580px; margin-top:55px; z-index:999;"> <iframe src="/banner/side1.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div><?php } else { ?><div style="position:absolute; width:160px; left:50%; margin-left:-710px; margin-top:55px; z-index:999;"> <iframe src="/banner/side3.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div><div style="position:absolute; width:160px; left:50%; margin-left:580px; margin-top:55px; z-index:999;"> <iframe src="/banner/side4.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div><?php } ?>
|
답변 1개
채택된 답변
+20 포인트
artlotz
2017-02-22 (수) 15:39:48
혹시 서브 페이지에서는 side1 ~ side4까지 랜덤하게 나오게 하신다는 건가요?
이렇게 하면 어떨까요~
Copy
<?php$banner = array(2, 1);if (!defined('_INDEX_')) { $banner = array(); while (count($banner) < 2) { $index = mt_rand(1, 4); if (!in_array($index, $banner)) { $banner[] = $index; } }}?><div style="position:absolute; width:160px; left:50%; margin-left:-710px; margin-top:55px; z-index:999;"> <iframe src="/banner/side<?=$banner[0]?>.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div><div style="position:absolute; width:160px; left:50%; margin-left:580px; margin-top:55px; z-index:999;"> <iframe src="/banner/side<?=$banner[1]?>.html" width="160" height="600" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe>
답변을 작성하려면 로그인이 필요합니다.