无限级分类是所有程序开发中会碰到的一个问题,下面我来介绍php+mysql实现的一个无限级分类程序,有需要的朋友可参考参考。
下面给大家看看我的数据库结构吧:数据库的名字为:fa_category
Field |
Type |
Comment |
cid |
int(11) |
分类id |
catename |
varchar(255) |
分类名字 |
catetype |
int(1) |
分类类型,1为单页面,2为普通分类 |
catdir |
varchar(255) |
英文目录 |
display |
int(1) |
1为显示,2为不显示 |
keywords |
varchar(255) |
栏目关键字 |
description |
text |
栏目描述 |
ctime |
int(11) |
创建时间 |
parentid |
int(11) |
父节点id,最高节点父节点为0 |
我们使用一个parentid字段来记录父节点的id,如果parentid为0,则为root。不多说,我们看看代码怎么写吧。我们要实现的功能就是如图片所示:
怎么样把它这样显示呢?这个问题我想了很久,先看看这段SQL语句吧,
代码如下 |
复制代码 |
SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade ,c.sort_order, COUNT( s.cat_id ) AS has_children
FROM ecs_category AS c
LEFT JOIN ecs_category AS s ON s.parent_id = c.cat_id
GROUP BY c.cat_id
ORDER BY c.parent_id, c.sort_order ASC
|
用左连接连接一个表,返回一个字段 has_children,这个字段是记录有多少子节点。
看看代码吧,
代码如下 |
复制代码 |
public function getCategory($catid=0,$re_type = true,$selected=0)
{
$db = new Public_DataBase();
$sql = 'select c.cid,c.catename,c.catetype,c.ctime,c.parentid,count(s.cid) as has_children from '.
__MYSQL_PRE.'category as c left join '.
__MYSQL_PRE.'category as s on s.parentid=c.cid group by c.cid order by c.parentid asc';
$res = $db->selectTable($sql);
$cateInfo = self::getChildTree($catid,$res);
if($re_type==true)
{
$select = '';
foreach($cateInfo as $val)
{
$select .= '<option value="' . $val['cid'] . '" ';
$select .= ($selected == $val['cid']) ? "selected='ture'" : '';
$select .= '>';
if($val['level']>0)
{
$select .= str_repeat(' ', $val['level'] * 4);
}
$select .= htmlspecialchars(addslashes($val['catename']), ENT_QUOTES) . '</option>';
}
return $select;
}
else
{
foreach($cateInfo as $key=>$val)
{
if($val['level']>0)
{
$cateInfo[$key]['catename'] = "|".str_repeat(' ', $val['level'] * 8)."└─".$val['catename'];
}
}
return $cateInfo;
}
}
/**
* 通过父ID递归得到所有子节点树
* @param int $catid 上级分类
* @param array $arr 含有所有分类的数组
* @return array
*/
public function getChildTree($catid,$arr)
{
$level = $last_cat_id = 0;
while (!empty($arr))
{
foreach($arr as $key=>$value)
{
$cid = $value['cid'];
if ($level == 0 && $last_cat_id == 0)
{
if ($value['parentid'] > 0)
{
break;
}
$options[$cid] = $value;
$options[$cid]['level'] = $level;
$options[$cid]['id'] = $cid;
$options[$cid]['name'] = $value['catename'];
unset($arr[$key]);
if ($value['has_children'] == 0)
{
continue;
}
$last_cat_id = $cid;
$cat_id_array = array($cid);
$level_array[$last_cat_id] = ++$level;
continue;
}
if ($value['parentid'] == $last_cat_id)
{
$options[$cid] = $value;
$options[$cid]['level'] = $level;
$options[$cid]['id'] = $cid;
$options[$cid]['name'] = $value['catename'];
unset($arr[$key]);
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cid;
$cat_id_array[] = $cid;
$level_array[$last_cat_id] = ++$level;
}
}
elseif ($value['parentid'] > $last_cat_id)
{
break;
}
}
$count = count($cat_id_array);
if ($count > 1)
{
$last_cat_id = array_pop($cat_id_array);
}
elseif ($count == 1)
{
if ($last_cat_id != end($cat_id_array))
{
$last_cat_id = end($cat_id_array);
}
else
{
$level = 0;
$last_cat_id = 0;
$cat_id_array = array();
continue;
}
}
if ($last_cat_id && isset($level_array[$last_cat_id]))
{
$level = $level_array[$last_cat_id];
}
else
{
$level = 0;
}
}
return $options;
|
}用smarty的一个循环就可以把它显示出来 效果和上面图片的一样!大家有什么问题可以给我留言。
在php中截取字符串最简单的办法就是利用substr()函数来实现,但是substr函数只能截取英文,如果是中文不会是乱码哦,那么有朋友说可使用mb_substr()来截取,这个方法又不能截取中文英混合的字符。
此函数用于截取gb2312编码的中文字符串:
代码如下 |
复制代码 |
<?php
// 说明:截取中文字符串
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
?>
|
Utf-8、gb2312都支持的汉字截取函数
截取utf-8字符串函数
为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象,编写如下的UTF-8字符串截取函数
关于utf-8的原理请看 UTF-8 FAQ
UTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。
代码如下 |
复制代码 |
<?php
// 说明:Utf-8、gb2312都支持的汉字截取函数
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
if($code == 'UTF-8')
{
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
$str = "abcd需要截取的字符串";
echo cut_str($str, 8, 0, 'gb2312');
?>
|
注意明:
代码如下 |
复制代码 |
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
'((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
|
可单独截取uft8字符串哦。
程序说明:
1. len 参数以中文字符为标准,1len等于2个英文字符,为了形式上好看些
2. 如果将magic参数设为false,则中文和英文同等看待,取绝对的字符数
3. 特别适用于用htmlspecialchars()进行过编码的字符串
4. 能正确处理GB2312中实体字符模式()
程序代码:
代码如下 |
复制代码 |
function FSubstr($title,$start,$len="",$magic=true)
{
/**
* powered by Smartpig
* mailto:d.einstein@263.net
*/
$length = 0;
if($len == "") $len = strlen($title);
//判断起始为不正确位置
if($start > 0)
{
$cnum = 0;
for($i=0;$i<$start;$i++)
{
if(ord(substr($title,$i,1)) >= 128) $cnum ++;
}
if($cnum%2 != 0) $start--;
unset($cnum);
}
if(strlen($title)<=$len) return substr($title,$start,$len);
$alen = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i<strlen($title);$i++)
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == "'")
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/&#(d+);/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=128)
{
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
$alen++;
}
}
}
if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}
|
一个简单php和mysql数据分页程序有需要的朋友可参考一下。
代码如下 |
复制代码 |
<?php
// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries(both queries)
// This script is in tutorial form and is accompanied by the following video:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
mysql_select_db("DB_Name_Here") or die (mysql_error());
////////////// QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
//$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
$pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 10;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
$pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. ' ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
// Build the Output Section Here
$outputList = '';
while($row = mysql_fetch_array($sql2)){
$id = $row["id"];
$firstname = $row["firstname"];
$country = $row["country"];
$outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />';
} // close while loop
?>
<html>
<head>
<title>Adam's Pagination</title>
<style type="text/css">
<!--
.pagNumActive {
color: #000;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
color: #000;
text-decoration: none;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
-->
</style>
</head>
<body>
<div style="margin-left:64px; margin-right:64px;">
<h2>Total Items: <?php echo $nr; ?></h2>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html>
|
效果
Page 6 of 39 Back Next
本文章利用Ajax分页来简单讲述一下如何利用php与ajax实现数据无刷新分页功能,有需要的朋友可参考一下。
简单的mysql数据表结构
代码如下 |
复制代码 |
CREATE TABLE messages
(
msg_id INT PRIMARY KEY AUTO_INCREMENT,
message VARCHAR(150)
);
|
JavaScript 代码
这里是ajax前段利用jquery来处理
代码如下 |
复制代码 |
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
function loading_show()
{
$('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');
}
function loading_hide()
{
$('#loading').fadeOut();
}
function loadData(page)
{
loading_show();
$.ajax
({
type: "POST",
url: "load_data.php",
data: "page="+page,
success: function(msg)
{
$("#container").ajaxComplete(function(event, request, settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1); // For first time page load default results
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
});
</script>
|
load_data.php
这里是获取由ajax发送的数据然后经过php查询mysql返回信息
代码如下 |
复制代码 |
<?php
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15; // Per page records
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"db.php";
$query_pag_data = "SELECT msg_id,message from messages LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data))
{
$htmlmsg=htmlentities($row['message']); //HTML entries filter
$msg .= "<li><b>" . $row['msg_id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
/* -----Total count--- */
$query_pag_num = "SELECT COUNT(*) AS count FROM messages"; // Total records
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
/* -----Calculating the starting and endign values for the loop----- */
//Some Code. Available in download script
}
?>
|
下面举了三个实现来介绍在php正则替换中变量的指定字符,有需要学习的机友可参考参考。
三种方法.
1.
代码如下 |
复制代码 |
<?php
$str = preg_quote('(银子)');
$txt = '我的呢称(银子)';
echo preg_replace("/($str)/","<span style='color:#f00;'>$1</span>",$txt);
?>
|
2.
代码如下 |
复制代码 |
<?php
$str = quotemeta('(银子)');
$txt = '我的呢称(银子)';
echo preg_replace("/($str)/","<span style='color:#f00;'>$1</span>",$txt);
?>
|
3.
代码如下 |
复制代码 |
<?php
$str = '(银子)';
$txt = '我的呢称(银子)';
echo preg_replace("/(Q$strE)/","<span style='color:#f00;'>$1</span>",$txt);
?>
|
三种方法都返回同样结果.. PHP中的Perl风格正则与Perl完全一样.连quotemeta也是通用的..
一些其它关于正则的实例
例子:
代码如下 |
复制代码 |
$text = “foobar123fooabcbar”;
$text = preg_replace(“/foo(?=bar)/”, “***”, $text);
|
//匹配bar前面的位置 ***bar123fooabcbar
代码如下 |
复制代码 |
$text = “foobar123fooabcbar”;
$text = preg_replace(“/(?<=bar)123/”, “***”, $text);
|
//匹配bar后面的位置 foo***123fooabcbar
代码如下 |
复制代码 |
$text = “foobar123fooabcbar”;
$text = preg_replace(“/foo(?!bar)/”, “***”, $text);
|
//匹配后面跟的不是bar的位置 foobar123***abcbar
代码如下 |
复制代码 |
$text = “foobar123fooabcbar”;
$text = preg_replace(“/(?<!foo)bar/”, “***”, $text);
|
//匹配前面不是foo的位置 foobar123fooabc***