1、
<div class="pagelist"> <span id="loadmore" class="btn" style="display: block;">加載更多</span> </div>
找到加載更多的按鈕,設置好id(最好用id,比較容易遍歷)。
2、
<ul id="showajaxnews" style="opacity: 1; top: 0px;"> [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] </ul>
找到列表內容模板的標簽,也設置好id。
3、
<script> $(function () { var i = 1; $('#loadmore').click(function () { $.ajax({ url: '域名/e/action/getmore.php', type: 'POST', data: { "next": i, 'table': 'news', 'classid': '[!--self.classid--]', /*這個標簽獲取的當前id*/ 'action': 'getmorenews', 'limit': 4, 'small_length': 120 }, dataType: 'html', beforeSend: function () { $("#loadmore").show().html( '<img src="/images/loaduai.gif" alt="正在努力加載中...'); $('#loadmore').attr('disabled', 'disabled'); }, success: function (data) { if (data) { $("#showajaxnews").append(data); $("#loadmore").removeAttr('disabled'); $("#loadmore").html('點擊加載更多'); i++; } else { $("#loadmore").show().html("已全部加載完畢!"); $('#loadmore').attr('disabled', 'disabled'); return false; } } }); }); }); </script>
ajax放到加載更多的下邊,記得一定要引入Jquery.
4、
下邊標紅的,我這里做了一個$classid的判斷,意思就是當欄目是父欄目7的時候,調用欄目id 8 和 9 的所有數據。按照自己的情景設置即可。
<?php require('../class/connect.php'); require('../class/db_sql.php'); require('../data/dbcache/class.php'); if ($_POST[action] == 'getmorenews') { $table = htmlspecialchars($_POST[table]); if (empty($_POST[orderby])) { $orderby = 'newstime'; } else { $orderby = htmlspecialchars($_POST[orderby]); } if (empty($_POST[myorder])) { $myorder = 'desc'; } else { $myorder = 'asc'; } if (empty($_POST[limit])) { $limit = 6; } else { $limit = (int) $_POST[limit]; } if (empty($_POST[classid])) { $where = null; } else if ($_POST[classid] == 7) { $where = 'where classid in("8,9")'; } else { $where = 'where classid in(' . $_POST[classid] . ')'; } if (empty($_POST[length])) { $length = 50; } else { $length = (int) $_POST[length]; } if (empty($_POST[small_length])) { $small_length = 500; } else { $small_length = (int) $_POST[small_length]; } // next:第幾頁 // table:調用數據表 // limit:每次調用數量 // small_length:簡介截取字符數 // length:標題截取字符數 // classid:調用欄目,允許多個,如1,2,3,4 特別注意,必須是調用同一數據表的欄目 // orderby:排序,默認是newstime,傳什么就按什么來排序,如 id // myorder:正反序,默認是asc,傳值怎為desc $link = db_connect(); $empire = new mysqlquery(); $num = (int) $_POST['next'] * $limit; if ($table) { $sql = $empire->query("SELECT * FROM `" . $dbtbpre . "ecms_" . $table . "` $where order by $orderby $myorder limit $num,$limit"); while ($r = $empire->fetch($sql)) { if ($r[mtitlepic] == '') { $r[mtitlepic] = $public_r[news . url] . "e/data/images/notimg.gif"; } $oldtitle = stripSlashes($r[title]); $title = sub($oldtitle, '', $length); $smalltext = stripSlashes($r[smalltext]); $smalltext = sub($smalltext, '', $small_length); $classname = $class_r[$r[classid]][classname]; $newsurl = $public_r[newsurl]; $classurl = $newsurl . $class_r[$r[classid]][classpath]; $urls = sys_ReturnBqTitleLink($r); ?> <!-- 以下代碼是顯示列表的標簽模板 ,按照情景修改即可。--> <li> <a href="<?= $urls ?>"> <div class="img"> <img src="<?= $r[titlepic] ?>" class="lazy"></div> <div class="con"> <h2> <?= $r[title] ?> </h2> <p> <?= $r[smalltext] ?> </p> <span> <?= date("Y-m-d", $r[newstime]) ?> </span> </div> <div class="more"> <span></span> </div> </a> </li> <?php } } } db_close(); $empire = null; ?>
建立一個getmore.php文件,把上邊代碼上傳到/e/action/文件中