首頁  >  文章  >  後端開發  >  thinkPHP商城公告功能開發問題分析

thinkPHP商城公告功能開發問題分析

不言
不言原創
2018-05-03 16:44:171264瀏覽

這篇文章主要介紹了thinkPHP商城公告功能開發問題,結合實例形式分析了基於thinkPHP實現商城公告功能所涉及的ajax交互及數據庫操作相關技巧,需要的朋友可以參考下

#本文實例分析了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 += "<li><strong>"+n.content+"</strong></li>"
    });
    $(".top-notice").show();
    $(&#39;#notice ul&#39;).html(""+html);
    $(&#39;#notice&#39;).unslider(); // 轮播
  }
});

4.取得sql語句的thinkphp處理

// 获取公告
function ajaxGetNotice() {
    if (IS_AJAX) {
      $this->mid;
      // 获取有效的,且结束时间大于当前时间的,或者日期等于0的公告
      $mallNoticeModel = M(&#39;Mall_notice&#39;);
      $where[&#39;mall_id&#39;] = $this->mid;
      $where[&#39;status&#39;] = 1;
      $where[&#39;endtime&#39;] = array(array(&#39;eq&#39;,0),array(&#39;gt&#39;,time()), &#39;or&#39;) ;
      //SELECT * from sh_mall_notice where mall_id = 9 and status = 1 and (endtime = 0 or endtime>1458354366);
      $notice = $mallNoticeModel->where($where)->order(&#39;sort desc&#39;)->select();
      if (!empty($notice)) {
        $this->ajaxReturn(array(&#39;status&#39;=>&#39;1&#39;,&#39;info&#39;=>$notice,&#39;msg&#39;=>"获取成功"),&#39;JSON&#39;);
      } else {
        $this->ajaxReturn(array(&#39;status&#39;=>&#39;2&#39;,&#39;info&#39;=>$notice,&#39;msg&#39;=>"公告不存在"),&#39;JSON&#39;);
      }
    }
}

$where[&#39;endtime&#39;] = array(array(&#39;eq&#39;,0),array(&#39;gt&#39;,time()), &#39;or&#39;) ;

巧妙的處理了這種邏輯關係。

相關推薦:

Thinkphp5微信小程式取得使用者資訊介面的實例詳解_

以上是thinkPHP商城公告功能開發問題分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn