[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 '<br />';
foreach($res as $k=>$val) {
    echo "<a href='index.php?cid={$val['id']}'>{$val['catename']}</a>";
}

进一步实现函数封装

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

关注我,关注我的博客,提出你的意见,我会写的更好!

你可能感兴趣的:(PHP,导航)