Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php和mysql动态面包屑_Php_Mysql_Breadcrumbs - Fatal编程技术网

php和mysql动态面包屑

php和mysql动态面包屑,php,mysql,breadcrumbs,Php,Mysql,Breadcrumbs,我有一个列出所有食物来源的网站;在我的数据库中,我创建了一个名为[foods] 在我的页面中,当用户单击该类别时,他将获得其所有子类别的列表。就我而言 food -- Plants ---- Vegetable ------ Aubergine ------ Broad_bean ------ Broccoli ------ Carrot ---- Fruits ------ Apple ------ Apricot ------ Banana ------ Cherry

我有一个列出所有食物来源的网站;在我的数据库中,我创建了一个名为
[foods]

在我的页面中,当用户单击该类别时,他将获得其所有子类别的列表。就我而言

food
 -- Plants
 ---- Vegetable
 ------ Aubergine
 ------ Broad_bean
 ------ Broccoli
 ------ Carrot
 ---- Fruits
 ------ Apple
 ------ Apricot
 ------ Banana
 ------ Cherry
 ------ Clementine
 ------ Guava
 -- Animals
现在,我想在页面顶部创建一个动态的面包屑导航,类似于

Food > Plants > Fruits > Banana
因此,用户可以在其中导航

我尝试了几个查询来获得这个结果,但没有成功

每次我只得到类别的第一个父级

因此,如果我在
Banana
页面,我只会得到
Fruits
类别,没有更深的内容

我知道我必须使用
while
循环
where cat\u parent\u id!=0
但我不知道如何以正确的方式实现它

这是我尝试过的一段代码片段

$cat_parent_id = $_REQUEST['cat_id'];

$q = mysqli_query($link, "SELECT * FROM foods WHERE cat_id = $cat_parent_id");
while($r = mysqli_fetch_assoc($q))
{
  echo $name = $r['cat_name'];
}
我非常感谢你在这方面的帮助


提前感谢…

您必须使用递归函数。手段检查

例如:

function show_breadcumb($cat_id)
{   
  $q = mysqli_query($link, "SELECT * FROM foods WHERE cat_id = $cat_id");
  while($r = mysqli_fetch_assoc($q))
  {
     $name = $r['cat_name'];
     $string.= $name .">".$string
  }
  //check if this category has any parent again call this fucntion
  if( has parent  ) { show_breadcumb($cat_id) }
  else return $string;
}

意味着你的循环应该继续查找类别直到第一级。

你应该给出你尝试过的内容,以便可以修复它。总比给你另一个答案好。:)也许你可以在这里得到一些想法:@caCtus,我已经更新了问题。。。thanksWARNING:当使用
mysqli
时,您应该使用参数化查询,并将用户数据添加到查询中。不要使用字符串插值来完成此操作,因为您将创建严重错误。回答问题时请使用英语。像“ur”这样的缩写词不仅让人恼火,而且对那些不懂俚语的人来说非常令人困惑。