你能帮我完成这个面包屑导航工作吗。我的php不好。我还在学习。
这是我的猫在mysql
cats_id cats_position cats_parentid
1 1> 0
2 1>2> 1
3 3> 0
4 1>2>4> 2
我试过这样的方法,但不是正确的方法。你能告诉我正确的路吗。
$pieces = explode(">", $position);
if ($pieces[0] != "")
{
$result = mysql_query("SELECT * FROM cats
WHERE cats_id='$pieces[0]'");
while($row = mysql_fetch_array($result))
{
$piecesid0 = $row['cats_id'];
$piecesname0 = $row['cats_name'];
$piecesposition0 = $row['cats_position'];
}
}
if ($pieces[1] != "")
{
$result = mysql_query("SELECT * FROM cats
WHERE cats_id='$pieces[1]'");
while($row = mysql_fetch_array($result))
{
$piecesid1 = $row['cats_id'];
$piecesname1 = $row['cats_name'];
$piecesposition1 = $row['cats_position'];
}
}
if ($pieces[2] != "")
{
$result = mysql_query("SELECT * FROM cats
WHERE cats_id='$pieces[2]'");
while($row = mysql_fetch_array($result))
{
$piecesid2 = $row['cats_id'];
$piecesname2 = $row['cats_name'];
$piecesposition2 = $row['cats_position'];
}
}
if ($pieces[3] != "")
{
$result = mysql_query("SELECT * FROM cats
WHERE cats_id='$pieces[3]'");
while($row = mysql_fetch_array($result))
{
$piecesid3 = $row['cats_id'];
$piecesname3 = $row['cats_name'];
$piecesposition3 = $row['cats_position'];
}
}
?>
<a href="index.php">Index</a> >
<a href="cats.php?cat=<?=$piecesid0;?>&parent=0&position=<?=$piecesposition0;?>"><?=$piecesname0;?></a>
<?
If ($piecesid1 != "")
{
?>
> <a href="cats.php?cat=<?=$piecesid1;?>&parent=<?=$piecesid0;?>&position=<?=$piecesposition1;?>"><?=$piecesname1;?></a>
<?
}
?>
<?
If ($piecesid2 != "")
{
?>
> <a href="cats.php?cat=<?=$piecesid2;?>&parent=<?=$piecesid1;?>&position=<?=$piecesposition2;?>"><?=$piecesname2;?></a>
<?
}
?>
<?
If ($piecesid3 != "")
{
?>
> <a href="cats.php?cat=<?=$piecesid3;?>&parent=<?=$piecesid2;?>&position=<?=$piecesposition3;?>"><?=$piecesname3;?></a>
<?
}
?>
看看这里的嵌套集模型:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
这就是CakePHP实现Tree行为的方式,这也是我用于面包屑的方式。他们添加了parent_id列作为嵌套集的增强版本。
如果权重太重,可以使用相邻列表模型:http://kod34fr33.wordpress.com/2008/05/06/adjacency-list-tree-on-mysql/