ปกติ SMF มันจะมีไฟล์ SSI.php ซึ่งรวมฟังชั่นไว้ในนั้นค่อนข้างเยอะครับ หนึ่งในนั้น มี ฟังชั้นที่ชื่อว่า ssi_recentAttachments อยู่ ซึ่งจะแสดงไฟล์แนบล่าสุดครับ เราสามารถปรับตัวนี้มาใช้ได้เลย
วิธีการใช้ก็แค่ include ไฟล์ SSI.php แล้วเรียกใช้ฟังชั่นได้เลย
แต่ถ้าหากไม่ซีเรียสเรื่อง permission ของแต่ละบอร์ดก็เขียนบ้านๆ แบบนี้ก็ได้ครับ
เขียนในไฟล์ SSI.php ก็ได้ครับง่ายดี จะได้ประมาณว่า
<?php
function AttRAND($num = 1) {
global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;
$request = $smcFunc['db_query']('', '
SELECT m.id_topic, m.subject, att.id_attach, att.id_thumb, att.id_msg
FROM {db_prefix}attachments AS att
INNER JOIN {db_prefix}messages AS m ON (att.id_msg = m.id_msg)
WHERE att.id_thumb = 0
ORDER BY RAND()
LIMIT {int:num}',
array(
'num' => $num,
)
);
$attachments = array();
while ($row = $smcFunc['db_fetch_assoc']($request)){
$attachments[] = array(
'id' => $row['id_attach'],
'src' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach']. ';image',
'img' => '<img src="'. $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach']. ';image" alt="" />',
'href' => $scripturl . '?topic=' . $row['id_topic'],
'link' => '<a href="'. $scripturl . '?topic=' . $row['id_topic'] .'" target="_blank">' . $row['subject'] . '</a>'
);
}
return $attachments;
}
?>
เวลาใช้งานก็ เหมือนที่บอกข้างต้น
<?php
include "SSI.php"; // path SSI.php
$att = AttRAND(2); // 2 Num Attachments
print_r($att);
?>
ที่เหลือก็แล้วแต่การวาง css แล้วล่ะครับ
