本文執行個體分析了thinkPHP商城公告功能開發問題。分享給大家供大家參考,具體如下:
效果如下
1.定在頭部
position: fixed;z-index: 999;top: 0;opacity:1;
2.ajax處理json資料
// 擷取商城公告function getNotice() { // 擷取公告函數 var res; $.ajax({ type: "POST", url: "{sh::U('Store/Mall/ajaxGetNotice',array('mid'=>$mid))}", dataType:'json', // 設為json之後,就能夠很好的處理擷取的json資料,json.status async: false, success: function(json){ res = json; } }); return res;}
設定dataType:'json'之後,json資料就直接可以通過json.的方式處理了。
3.最後載入,頁面更好看。
$(document).ready(function(e) { // 主函數 // 擷取公告 var action_name = "{sh::ACTION_NAME}"; // 頁面使用thinkphp常量 var json = getNotice(); if ( action_name == 'index' && json.status == 1) { // 首頁並且公告存在 $(".top").css("margin-top", "70px"); // jquery設定css $(".main-sidebar").css("top" ,"70px"); var html = ''; $.each(json.info, function(i, n){ // n為常值內容 html += ""+n.content+"" }); $(".top-notice").show(); $('#notice ul').html(""+html); $('#notice').unslider(); // 輪播 }});
4.擷取sql語句的thinkphp處理
// 擷取公告function ajaxGetNotice() { if (IS_AJAX) { $this->mid; // 擷取有效,且結束時間大於目前時間的,或者日期等於0的公告 $mallNoticeModel = M('Mall_notice'); $where['mall_id'] = $this->mid; $where['status'] = 1; $where['endtime'] = array(array('eq',0),array('gt',time()), 'or') ; //SELECT * from sh_mall_notice where mall_id = 9 and status = 1 and (endtime = 0 or endtime>1458354366); $notice = $mallNoticeModel->where($where)->order('sort desc')->select(); if (!empty($notice)) { $this->ajaxReturn(array('status'=>'1','info'=>$notice,'msg'=>"擷取成功"),'JSON'); } else { $this->ajaxReturn(array('status'=>'2','info'=>$notice,'msg'=>"公告不存在"),'JSON'); } }}
$where['endtime'] = array(array('eq',0),array('gt',time()), 'or') ;
巧妙的處理了這種邏輯關係。
更多關於thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)架構進階教程》、《Zend FrameWork架構入門教程》、《smarty模板入門基礎教程》及《PHP模板技術總結》。
希望本文所述對大家基於ThinkPHP架構的PHP程式設計有所協助。