thinkPHP商城公告功能开发问题详解

墨辰丷
发布: 2023-03-28 12:22:01
原创
1543 人浏览过

这篇文章主要介绍了thinkPHP商城公告功能开发问题,结合实例形式分析了基于thinkPHP实现商城公告功能所涉及的ajax交互及数据库操作相关技巧,需要的朋友可以参考下

效果如下

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;) ;
登录后复制

巧妙的处理了这种逻辑关系。

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php中登录超时检测功能实例详解

php base64 编码与解码实例详解

php实现连接mysql数据库的方法

以上是thinkPHP商城公告功能开发问题详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!