[php] 面包屑导航

要实现的效果:

图片>美女图片>日韩明星>日本AV>

对应的数据表:
[php] 面包屑导航_第1张图片

db.php

$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'test';
$con = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());
mysql_query('set names utf8') or die(mysql_error());

index.php

header('Content-type: text/html; charset=utf-8');

include "db.php";

function getCatePath($cid, &$result=array()) {
	$sql = 'select * from tp_category where id='.$cid;
	$rs = mysql_query($sql);
	$row = mysql_fetch_assoc($rs);
	if($row) {
		$result[] = $row;
		getCatePath($row['pid'], $result);
	}
	krsort($result);
	return $result;
}
$res = getCatePath(10);
print_r($res);

echo '
'
; foreach($res as $k=>$val) { echo "]}'>{$val['catename']}"; }

进一步实现函数封装

// 封装成函数
function displayCatePath($cid, $url='index.php?cid=') {
	$res = getCatePath($cid);
	$str = '';
	foreach($res as $k=>$val) {
		$str .= "]}'>{$val['catename']}>";
	}
	return $str;
}
echo displayCatePath(10, 'cate.php?page=1&id=');

写到最后,推广下一个不错的分享平台:http://www.techshare100.com/,欢迎大家加入

你可能感兴趣的:(后台程序)