قبلا در همین تاپیک آموزش قرار دادن آخرین ارسال های انجمن مای بی بی را برای تمامی سیستم ها قرار داده بودیم ، اما در این پست میخواهیم آخرین ارسالی های انجمن را بصورت آژاکس بهتون آموزش بدهیم . تقریبا بیشتر آموزش شبیه قبل هست .
اما بريم سراغ آموزش :
در ابتدا يك استايل براى خروجى مطالب طراحى ميكنيم و نام آنرا FB.css ميگذاريم .
کد:
#MTForumBlock
{
padding:5px;
}
#MTForumBlock TABLE
{
width:100%;
direction:rtl;
}
#MTForumBlock TR
{
background-image:url(MTForumBlock_row.png);
background-repeat:repeat-x;
height:18px;
}
#MTForumBlock TR.MTForumfirst
{
background-image:url(MTForumBlock_row_over.png);
}
#MTForumBlock TR:hover
{
background-image:url(MTForumBlock_row_over.png);
}
#MTForumBlock TD
{
font-family:Tahoma;
font-size:12px;
border:1px solid #e6e9ec;
color:#666666;
}
.MTForumtitle
{
text-align:center;
}
.MTForumanswer
{
width:40px;
text-align:center;
}
.MTForumview
{
width:40px;
text-align:center;
}
.MTForumlast
{
width:90px;
text-align:center;
}
.MTForumrowtitle
{
padding-right:5px;
}
#MTForumBlock A
{
float:right;
width:100%;
height:100%;
color:#666666;
text-decoration:none;
}
#MTForumBlock A:hover
{
text-decoration:underline;
}
#MTForumBlockControl TABLE
{
height:22px;
font-family:Tahoma;
font-size:12px;
color:#999999;
}
#MTForumBlockControl A
{
text-decoration:none;
color:#999999;
float:right;
width:49px;
height:22px;
line-height:20px;
}
.MTForumButton
{
width:49px;
background-image:url(MTForumBlock_button.png);
background-position:right;
background-repeat:no-repeat;
text-align:center;
}
.MTForumButton:hover
{
background-image:url(MTForumBlock_button.png);
background-position:left;
background-repeat:no-repeat;
}
#MTFloader
{
width:40px;
text-align:center;
}
.MTForumtitle1
{
text-align:center;
width:40px;
}
.MTForumanswer1
{
width:40px;
text-align:center;
}
.MTForumview1
{
width:40px;
text-align:center;
}
.MTForumlast1
{
width:40px;
text-align:center;
}
.MTForumrowtitle1
{
padding-right:5px;
}
يك فايل index.html خالى هم ايجاد كنيد . سپس یک فایل به نام last.php ایجاد کنید و کدهای زیر را در آن قرار بدهید :
کد php:
<?php
### By: Nullers.Ir
function safe( $value ){
$value = trim( $value );
$value = mysql_escape_string( $value );
$value = htmlspecialchars($value);
$value = strip_tags($value);
$value = str_replace(array("<",">","'","ی","&","ۜ"),array("<",">","'","ي","&","ي"),$value);
return $value;
}
echo '
<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="FB.css" rel="stylesheet" type="text/css" media="screen">
<div id="MTForumBlock">
<table id="webalfa-lastpost">
<tr>
<td>عنوان</td>
<td>پاسخ</td>
<td>بازديد</td>
<td>توسط</td>
</tr>';
## CUSTOMIZE SETTINGS FOR YOUR SITE ##
$db_host = "localhost"; // Change this if your MySQL database host is different.
$db_name = "database_name"; // Change this to the name of your database.
$db_user = "database_user"; // Change this to your database username.
$db_pw = "password"; // Change this to your database password.
$db_pre = "mybb_"; // Tables prefix
$forum_url = "http://www.your-site.com/"; // Change this to reflect to your forum's URL.
$limit = "15"; // Number of posts displayed.
$des = safe($_POST["des"]);
$txtlimit = "100"; // This is the character limit.
#######################################
// Connecting to your database
mysql_connect($db_host, $db_user, $db_pw );
mysql_select_db($db_name);
mysql_query("SET NAMES utf8");
$thread_sql = mysql_query("SELECT tid,subject,lastposter,replies,views FROM ".$db_pre."threads order by lastpost DESC LIMIT $des,$limit");
while($thread_get=mysql_fetch_array($thread_sql))
{
$replycount = $thread_get['replies'];
$views = $thread_get['views'];
$threadid = $thread_get['tid'];
$poster = $thread_get['lastposter'];
$title = $thread_get['subject'];
if(strlen($title) > ($txtlimit-4)){
$title = substr($title,0,$txtlimit).'...';
}
echo "
<tr>
<td class=\"MTForumrowtitle\"><a target=\"_blank\" href=\"".$forum_url."showthread.php?tid=$threadid&action=lastpost\"><img src=\"FBarrow.gif\" border=\"0\"> $title</a></td>
<td class=\"MTForumanswer\">$replycount</td>
<td class=\"MTForumview\">$views</td>
<td class=\"MTForumlast\">$poster</td>
</tr>";
}
echo " </tr>
</table>
</div>
<div id=\"MTForumBlock\">
</div>
<div id=\"MTForumBlockControl\" align=\"center\">
<table cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td class=\"MTForumButton\"><a href=\"j<strong></strong>rest()\"> بازگشت</a></td>
<td id=\"MTFloader\"></td>
<td class=\"MTForumButton\"><a href=\"j<strong></strong>send()\">بعد <img border=\"0\" src=\"MTForumBlock_buttonNarrow.png\" /></a></td>
</tr>
</table>
";
?>
نکات مهم :
در هنگام فراخوانى استايل دقت كنيد كه آدرس last.css را به درستى وارد كنيد .
کد:
<link href="FB.css" rel="stylesheet" type="text/css" media="screen">
تنظیمات مربوط به انجمن مای بی بی را به صورت دقیق وارد کنید :
کد php:
$db_host = "localhost"; // همین نام را درج کنید
$db_name = ""; // نام ديتابيس مربوط به فروم ماي بي بي
$db_user = ""; // نام كاربري مربوط به ديتابيس فروم ماي بي بي
$db_pw = ""; // پسورد ديتابيس فروم ماي بي بي
$db_pre = "mybb_"; // Tables prefix
$forum_url = "http://"; // آدرس فروم ماي بي بي
$limit = "30"; // تعداد پستهاي نمايش داده شده
$txtlimit = "100"; // محدودیت تعداد کاراکترها
فایل دیگری با نام last-fid.php بسازید و کدهای زیر را درون آنها قرار بدهید .
کد php:
<?php
### By: Nullers.Ir
function safe( $value ){
$value = trim( $value );
$value = mysql_escape_string( $value );
$value = htmlspecialchars($value);
$value = strip_tags($value);
$value = str_replace(array("<",">","'","ی","&","ۜ"),array("<",">","'","ي","&","ي"),$value);
return $value;
}
echo '
<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="FB.css" rel="stylesheet" type="text/css" media="screen">
<div id="MTForumBlock">
<table id="webalfa-lastpost">
<tr>
<td>عنوان</td>
<td>پاسخ</td>
<td>بازديد</td>
<td>توسط</td>
</tr>';
## CUSTOMIZE SETTINGS FOR YOUR SITE ##
$db_host = "localhost"; // Change this if your MySQL database host is different.
$db_name = "database_name"; // Change this to the name of your database.
$db_user = "database_user"; // Change this to your database username.
$db_pw = "password"; // Change this to your database password.
$db_pre = "mybb_"; // Tables prefix
$forum_url = "http://www.your-site.com/"; // Change this to reflect to your forum's URL.
$limit = "15"; // Number of posts displayed.
$des = safe($_POST["des"]);
$fid = safe($_POST["fid"]);
$txtlimit = "100"; // This is the character limit.
#######################################
// Connecting to your database
mysql_connect($db_host, $db_user, $db_pw );
mysql_select_db($db_name);
mysql_query("SET NAMES utf8");
$thread_sql = mysql_query("SELECT tid,subject,lastposter,replies,views FROM ".$db_pre."threads WHERE `fid`= '$fid' order by lastpost DESC LIMIT $des,$limit");
while($thread_get=mysql_fetch_array($thread_sql))
{
$replycount = $thread_get['replies'];
$views = $thread_get['views'];
$threadid = $thread_get['tid'];
$poster = $thread_get['lastposter'];
$title = $thread_get['subject'];
if(strlen($title) > ($txtlimit-4)){
$title = substr($title,0,$txtlimit).'...';
}
echo "
<tr>
<td class=\"MTForumrowtitle\"><a target=\"_blank\" href=\"".$forum_url."showthread.php?tid=$threadid&action=lastpost\"><img src=\"FBarrow.gif\" border=\"0\"> $title</a></td>
<td class=\"MTForumanswer\">$replycount</td>
<td class=\"MTForumview\">$views</td>
<td class=\"MTForumlast\">$poster</td>
</tr>";
}
echo " </tr>
</table>
</div>
";
?>
تنظیمات مربوط به دیتابیس و انجمن را در فایل بالا مطابق فایل last.php انجام دهید .
و اما فایل دیگری با نام ajax.php بسازید و کدهای زیر را در آنها قرار بدهید :
کد php:
<html>
<head>
<link href="FB.css" rel="stylesheet" type="text/css" media="screen">
<title> آخرین مطالب ارسالی انجمن بصورت آژاکس </title>
<script type="text/javascript">
function creatajax()
{
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
return XMLHttpRequestObject;
}
var count1 = 0;
function send()
{
document.getElementById("loading").style.display ='inherit';
var XMLHRO = creatajax();
var obj = document.getElementById("resultinfo");
if(XMLHRO)
{
XMLHRO.open("POST","last.php");
XMLHRO.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHRO.onreadystatechange = function()
{
if((XMLHRO.readyState == 4) || (XMLHRO.Status == 200))
{
document.getElementById("loading").style.display ='none';
obj.innerHTML = XMLHRO.responseText;
}
}
}
XMLHRO.send("des="+count1);
count1 = count1 + 10;
}
function rest()
{
document.getElementById("loading").style.display ='inherit';
var XMLHRO = creatajax();
var obj = document.getElementById("resultinfo");
if(XMLHRO)
{
XMLHRO.open("POST","last.php");
XMLHRO.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHRO.onreadystatechange = function()
{
if((XMLHRO.readyState == 4) || (XMLHRO.Status == 200))
{
document.getElementById("loading").style.display ='none';
obj.innerHTML = XMLHRO.responseText;
}
}
}
XMLHRO.send("des="+0);
}
function other(fid)
{
document.getElementById("loading").style.display ='inherit';
var XMLHRO = creatajax();
var obj = document.getElementById("resultinfo");
if(XMLHRO)
{
XMLHRO.open("POST","last-fid.php");
XMLHRO.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHRO.onreadystatechange = function()
{
if((XMLHRO.readyState == 4) || (XMLHRO.Status == 200))
{
document.getElementById("loading").style.display ='none';
obj.innerHTML = XMLHRO.responseText;
}
}
}
XMLHRO.send("des="+0+"&fid="+fid);
}
</script>
</head>
<body onload="send()">
<center><img src="loading.gif" alt="loading" style="display:none" id="loading"><center>
<div id="MTForumBlock">
<table id="webalfa-lastpost">
<tr>
<td><a href="javascript:rest()">آخرین ارسال ها</a></td>
<td><a href="javascript:other(560)">اخبار سایت</a></td>
<td><a href="javascript:other(564)"> تالار ویژه</a></td>
<td><a href="javascript:other(411)">مقالات تجاری</a></td>
<td><a href="javascript:other(6)">آموزش html </a></td></tr></table>
</div>
<div id="resultinfo"></div>
</body>
</html>
فایل بالا ترکیبی از یک جاوا اسکریپت و html هست که مطالب انجمن را بر اساس کلاسهایی که در انتهای کد تعریف میکنید باز خوانی میکند .
کد:
<table id="webalfa-lastpost">
<tr>
<td><a href="javascript:rest()">آخرین ارسال ها</a></td>
<td><a href="javascript:other(560)">اخبار سایت</a></td>
<td><a href="javascript:other(564)"> تالار ویژه</a></td>
<td><a href="javascript:other(411)">مقالات تجاری</a></td>
<td><a href="javascript:other(6)">آموزش html </a></td></tr></table>
نحوه ی پیدا کردن id یک انجمن برای قرار دادن در کد بالا :
فرض کنید شما یک انجمن با نام تفریح و سرگرمی دارید که آدرسش شبیه لینک زیر هست :
your-site.com/
forum-561.html ==> یعنی با کلیک کردن بر روی این لینک شما وارد تالار تفریح و سرگرمی می شوید . پس کدی که شما برای قرار دادن در فایل ajax.php احتیاج دارید بصورت زیر هست :
کد:
<td><a href="javascript:other(561)">تفریح و سرگرمی</a></td>
تمامی فایلهایی که در بالا ساختید وارد یک پوشه به نام ajax-post کنید و درون سایت آپلود کنید . و اما عکسها و فایلهای دیگری هم این اسکریپت نیاز داره که میتوانید کل فایلها را از آدرس ضمیمه دانلود کنید .
[
attachment=7480]
برای نمایش آخرین مطالب ارسالی هم باید آدرسی مطابق آدرس پایین درج کنید :
کد:
http://www.your-site.com/ajax-post/ajax.php