พอดีวันนี้นึงอยากให้ภาพในเว็บ
talk2trip.com มันสุ่มขึ้นมาแบบ ไม่ซ้ำซากจำเจ แบบว่าทำเป็น
slideshow ที่สุ่มภาพขึ้นมาด้วย
เลยเปิด
php manual เจ้าเก่าขึ้นมา หาจากคำว่า
rand ก่อน เพราะเคยใช้คำสั่งนี้ในการสุ่ม
แ่ต่เงื่อนไขในการทำครั้งนี้มีอยู่ว่ามีภาพ 10 ภาพ จะยกตัวอย่างเป็นตัวเลขแล้วกัน จะได้เข้าใจง่ายขึ้น มีเลขอยู่ 5 ตัว
ต้องการสุ่มให้มันเรียงกัน แบบมั่ว ๆ ให้ผลออกมาแบบนี้
4 3 5 1 2
3 1 4 2 5
5 4 1 2 3
ยังไม่เห็นภาพกันใช่มั้ยละ เอานี่ไปดูเลย จะได้เข้าใจมากขึ้น
ตัวอย่าง:
http://talk2trip.com/fade.php *กด refresh เพื่อจะให้เห็นถึงการสุ่มภาพที่ชัดเจนยิ่งขึ้นหลังจากที่ค้นไปค้นมา ก็ไปเจอเจ้า function ที่ชื่อว่า
shuffer ที่มีคำบรรยายไว้ว่า
shuffle
(PHP 3 >= 3.0.8, PHP 4, PHP 5)
shuffle -- Shuffle an array
Description
bool shuffle ( array &array )
This function shuffles (randomizes the order of the elements in) an array.
Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.
Example 1. shuffle() example
<?php
$numbers = range(1, 20);
srand((float)microtime() * 1000000);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
}
?>
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
จากนั้นก็เอามาประยุกค์ เป็น Code ที่ใช้ในการสุ่มภาพ ตามตัวอย่างด้านล่างนี้ครับ<?php
$fade =array(
'<img src="http://talk2trip.com/fadeimg/El-Salva.jpg" border="0" alt="slideIMG" />',
'<img src="http://talk2trip.com/fadeimg/slide-03.JPG" border="0" alt="slideIMG" />',
'<img src="http://talk2trip.com/fadeimg/slide-01.JPG" border="0" alt="slideIMG" /> ',
'<img src="http://talk2trip.com/fadeimg/slide-02.JPG" border="0" alt="slideIMG" /> ',
'<img src="http://talk2trip.com/fadeimg/Nicaragu.jpg" border="0" alt="slideIMG" /> ',
'<img src="http://talk2trip.com/fadeimg/Ethiopia.jpg" border="0" alt="slideIMG" />',
'<img src="http://talk2trip.com/fadeimg/samchuk.jpg" border="0" alt="slideIMG" /> ',
'<img src="http://talk2trip.com/fadeimg/Costa_Ri.jpg" border="0" alt="slideIMG" /> ',
'<img src="http://talk2trip.com/fadeimg/Fair-Tra.jpg" border="0" alt="slideIMG" />',
'<img src="http://talk2trip.com/fadeimg/Guatemal.jpg" border="0" alt="slideIMG"/>'
);
shuffle($fade);
foreach ($fade as $sFade) {
echo $sFade;
}
?>
เอาไปประยุกค์ต่อกันเองเน้อ มีข้อสงสัยอะไรถามได้ที่กระทู้นี้เลยจ้า