你能帮我搞这个痕迹导航吗?我的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>
<?
}
?>
答案 0 :(得分:1)
看一下嵌套集模型: 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/