PHP常用函数整理
1、
/** * 获取指定日期的月份开始和结束时间。 * */ public function getMonthRange($date) { $ret=array(); $timestamp=strtotime($date); $mdays=date('t',$timestamp); $ret['sdate']=date('Y-m-1 00:00:00',$timestamp); $ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp); return $ret; }
2、数组转树形结构。
/** * 生成树形结构数据。 * @param $data 树形结构列表 * @param $parent_id 父类ID * @return int */ function toTree($data, $parent_id) { $tree = array(); for ($i = 0; $i< count($data); $i++) { if($data[$i]['parent_id'] == $parent_id) { $temp = toTree($data, $data[$i]['id']); if(count($temp) > 0) { $data[$i]['children'] = $temp; } array_push($tree, $data[$i]); } } return $tree; }
3、创建多级菜单,无限级菜单
/** * 创建三级选择菜单。 * @param $tree 菜单的树型数据 * @aram $list 菜单的列表型数据 * @return string */ function createMenu($tree, $list) { $menuList = '<ul class = "am-pagination am-nav-pills" style="height:0px;margin:0px 2px 0px 2px;">'; foreach ($tree as $value) { // 存在子栏目,则不允许点击进入。 $findSub = false; foreach ($list as $sub_value) { if ($sub_value['parent_id'] == $value['id']) { $findSub = true; } } if ($value['parent_id'] == 0) { $showCss = ''; } else { $showCss = 'style="display:none;"'; } if ($findSub) { $menuList .= '<li id="' . $value['id'] . '" class="menuList" '. $showCss . '> <a>' . $value['title'] . '</a>'; } else { $menuList .= '<li id="' . $value['id'] . '" class="menuList" '. $showCss . '> <a id="' . $value['id'] . '" href="#">' . $value['title'] . '</a>'; } if (!empty($value['children'])) { $menuList .= createMenu($value['children'], $list); } $menuList .= '</li>'; } $menuList .= '</ul>'; return $menuList; }
对应的菜单调用,以及JS
<div class="am-u-sm-8"> <div style="z-index:1000;position:absolute;"> <?php echo createMenu($tree, $list); ?> </div> </div>
对应的菜单调用JS
<script type="text/javascript"> $(function () { // 处理菜单的隐藏和显示。 $(".menuList").hover(function(){ $(this).find("li").click(function(event) { return false; }) $(this).find("[href='#']").click(function() { // 跳转到指定页面。 var id = $(this).attr('id'); window.location.href = 'URL' + '&ID=' + id; return false; }) if($(this).hasClass("shows")) { $(this).removeClass("shows"); $(this).find("li").find("ul").removeClass("shows"); $(this).find("li").hide(); } else { $(this).addClass("shows"); $(this).find("li").fadeIn(); $(this).find("li").find("ul").find("li").hide(); $(this).fadeIn(); } }); }); </script>

浙公网安备 33010602011771号