เพื่อให้กระทู้ดีๆ จะไม่พลาดสำหรับเพื่อนสมาชิกครับ ติดตามไปอ่านกัน
ที่นี่...
http://nevikup.blogspot.com/2012/06/smf.htmlตรงนี้มีภาพ! แต่ท่านจะมองไม่เห็น , ท่านต้อง
สมัครสมาชิก หรือ
ลงชื่อเข้าระบบมาดูการนำไปใช้งานเลยดีกว่านะครับ
ไฟล์ที่เกี่ยวข้องมี 3 ไฟล์ และ 1 ใน 3 ไฟล์นั้นต้องสร้างขึ้นมาใหม่1. Display.template.php
// เพิ่มปุ่มโหวต หรือปุ่มเลือกกระทู้ในไฟล์นี้2. BoardIndex.template.php
// สำหรับแสดงกระทู้โหวต และ3. voterec.php
// สำหรับโหวตหรือเลือกกระทู้ไฟล์
Display.template.php ค้นหาคำว่า
if (!$message['approved'] && $message['member']['id'] != 0 && $message['member']['id'] == $context['user']['id'])
echo '
<div class="approve_post">
', $txt['post_awaiting_approval'], '
</div>';
พบแล้ววางคำสั่งด้านล่างนี้ต่อท้ายไป if(empty($message['counter']))
echo '
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script>
<script type="text/javascript">
function voterec(id, ele){
$(ele).unbind("click")
$(".whs-rec-desc").html("โปรดรอ...")
$.get("',$settings['theme_url'],'/voterec?act=vote&id="+id, {}, function(d){
if(d!="OK")
$(".whs-rec-desc").html(d)
else
$(".whs-rec-desc").html("โหวตเรียบร้อยแล้วครับ ")
})
}
</script>
<div class="whs-rec" style="float: right; text-align: center; font-size: 9pt;">
<a href="javascript:ubuntuclub();" onclick="voterec(',$context['current_topic'],', this); return false;">
<img src="',$settings['default_theme_url'],'/images/recommend.png" />
[/url]
<div class="whs-rec-desc" style="margin-top: -20px;">
โหวตกระทู้นี้ ห้เป็นกระทู้แนะนำ
</div>
</div>';
- ไฟล์
BoardIndex.template.php ให้ค้นหาคำว่า
/* Each category in categories is made up of:
id, href, link, name, is_collapsed (is it collapsed?), can_collapse (is it okay if it is?),
new (is it new?), collapse_href (href to collapse/expand), collapse_image (up/down image),
and boards. (see below.) */
พบแล้วให้วางโค๊ดด้านล่างนี้ด้านบน ..
// WHS's Mod -> Recommend Topic
print <<<WHSDOC
<div class="tborder marginbottom categoryframe">
<h3 class="catbg headerpadding">กระทู้แนะนำ</h3>
<table cellspacing="1" class="bordercolor boardsframe">
<tr>
<td class="windowbg icon"><img src="{$settings['default_theme_url']}/images/recommend.png" alt="กระทู้แนะนำ" border="0" /><div style="margin-top: -20px;">กระทู้แนะนำ</div></td>
<td class="windowbg"><iframe src="/voterec?act=view" style="width:100%; border: none;"></iframe></td>
</tr>
</table>
</div>
WHSDOC;
และสุดท้ายสร้างสร้างไฟล์ voterec.php และวางชุดคำสั่งนี้ลงไป<?php
include "smf/SSI.php";
// no trespasser!
if($context['user']['is_guest']){
die('ต้องสมัครสมาชิกก่อนทำการโหวตครับ');
}
function boardName($bid){
global $smcFunc;
$req = $smcFunc['db_query']('', "
SELECT name
FROM {db_prefix}boards
WHERE id_board = {int:bid}
LIMIT 1
", array(
'bid' => $bid,
));
$row = $smcFunc['db_fetch_row']($req);
return $row[0];
}
// yeah, I cheat.
// this using the [t=..] by the DtTvB <img src="http://www.auto-nkp.com/Smileys/Emotion/wanwan022.gif" alt=":)" title="Smiley" class="smiley" /> ported to SMF 2's
// Get topic title from Zone-IT, with member color link!
function ubuntuclub_topic($message){
global $smcFunc, $memberContext, $txt, $scripturl;
$req = $smcFunc['db_query']('', "
SELECT subject, id_member, id_board, id_topic
FROM {db_prefix}messages
WHERE id_topic = {int:idt}
ORDER BY id_msg ASC LIMIT 1
", array(
'idt' => $message,
));
$row = $smcFunc['db_fetch_row']($req);
if($row[1] != 0){
// I don't reinvent the wheel, I'm lazy. I know this is slow but since we use iframe that doesn't matter.
loadMemberData($row[1], false, 'minimal');
@loadMemberContext($row[1]); // SHUT UP!
$member = $memberContext[$row[1]]['link'];
}else{
$member = $txt['guest_title']; //I'm Fedora's l10n team after all.
}
// get the board's name!
$bn = boardName($row[2]);
return array($row[0], $member, "<a href='{$scripturl}/board,{$row[2]}.0.html'>$bn[/url]");
}
if($_GET['act'] == "vote"){
$topic = $_GET['id'];
// is that thing really, exists?
if (!ubuntuclub_topic($topic)){
$smcFunc['db_free_result']($request);
die("ไม่พบกระทู้ที่เรียก");
}
$smcFunc['db_free_result']($request);
// well, so now it's exists. Just inject that into the table
// but we need to know how much vote does it having?
$request = $smcFunc['db_query']('', '
SELECT * FROM {db_prefix}recommend WHERE ID_TOPIC = {int:idt}
', array(
'idt' => $topic
));
// hmm, PHP's DBAPI is clearly a mess, python's DBAPI is really simple
$recommend = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
if(!$recommend){
$request = $smcFunc['db_query']('', '
INSERT INTO {db_prefix}recommend VALUES ({int:idt}, 1, "'.$context['user']['id'].'")
', array(
'idt' => $topic
));
die("OK");
}else{
// PHP showing it's suckness again, I think context.user.id in recommend[2].split(",") is more readable
if(in_array($context['user']['id'], split(",", $recommend[2]))){
die("คุณโหวตไปแล้ว");
}
$recommend[2] .= ",".$context['user']['id'];
$recommend[1] += 1;
// commit!
$request = $smcFunc['db_query']('', '
UPDATE {db_prefix}recommend SET count = {int:count}, voters = "'.$recommend[2].'"
WHERE ID_TOPIC = {int:idt}
', array(
'idt' => $topic,
'count' => $recommend[1]
));
die("OK");
}
}else if($_GET['act'] == "view"){
$req = $smcFunc['db_query']('', "
SELECT ID_TOPIC FROM {db_prefix}recommend
ORDER BY count DESC LIMIT 5
", array());
while($row = $smcFunc['db_fetch_row']($req)){
$tt = ubuntuclub_topic($row[0]);
// hard coding to prevent beggars to use this
print "<div><a href='$scripturl/topic,{$row[0]}.0.html' target='_parent'>{$tt[0]}[/url] {$txt['started_by']} {$tt[1]} {$txt['in']} {$tt[2]}</div>";
}
}
เซฟไว้ในโฟล์เดอร์ธีมของเรา
สุดท้ายต้องไปสร้างตารางไว้ในฐานข้อมูลด้วยครัับCREATE TABLE `smf_recommend` (
`ID_TOPIC` int(11) NOT NULL,
`count` int(11) NOT NULL default '0',
`voters` longtext NOT NULL,
PRIMARY KEY (`ID_TOPIC`),
KEY `count` (`count`)
)
ทั้งหมดนี้ผมเอาโค๊ดจากลิงค์ที่อ้างอิงด้านบนมา โดยไม่ได้ปรับแต่งหรือแก้ไขแต่อย่างใด ทั้งนี้เพื่อให้เครดิตแก่ผู้เขียนครับ
แต่ถ้าจะเอาไปใช้งานจริงก็ลองปรับดูให้เข้ากับบอร์ดของเรา จะให้สมาชิกสามารถโหวตได้ด้วย หรือให้เฉพาะแอดมินเท่านั้นโหวตก็ไปลองปรับกันใช้ดูครับ