{"id":9,"date":"2009-03-31T06:31:00","date_gmt":"2009-03-31T06:31:00","guid":{"rendered":"http:\/\/www.codeboss.in\/web-funda\/?p=8"},"modified":"2009-03-31T06:31:00","modified_gmt":"2009-03-31T06:31:00","slug":"recursive-function-to-output-category-sub-category-breadcrumb","status":"publish","type":"post","link":"https:\/\/codeboss.in\/web-funda\/2009\/03\/31\/recursive-function-to-output-category-sub-category-breadcrumb\/","title":{"rendered":"Recursive Function to output Category \/ Sub-category Breadcrumb"},"content":{"rendered":"<pre class=\"brush: php;\">\n\/\/\/ Recursive function to output the breadcrumb\nfunction breadcrumb($id,$layer=0,$out=''){\n\/\/\/ If id = 0 outputs that it is Root Category. No need to go any further.\nif($id ==0){\nreturn \"Root Category\";\n}\n\/\/\/ if id > 0 fetches the details of the category first\n$crumbsql = mysql_query(\"Select * From category Where cat_id = '\".$id.\"' \");\n$crumbres = mysql_fetch_array($crumbsql);\n\/\/\/ for the first call (the category which will be displayed last) \n\/\/\/ the layer is 0 to denote it is the current category and wont \n\/\/\/ be having a link. \nif($layer == 0){\n$out = $crumbres['cat_name'];\n} else {\n$out = '<a href=\"breadcrumb.php?catid='.$crumbres['cat_id'].'\">'.$crumbres['cat_name'].'<\/a> > '.$out;\n}\n\/\/\/ The layer increses to ensure all other categories have links.\n$layer++;\n\/\/\/ If the parent category is greater than 0 the function is called again and the same steps are followed. \n\/\/\/ If it reaches 0 then Root Category is printed and the output returned to process.\nif($crumbres['cat_parent_id'] > 0){\n$out = breadcrumb($crumbres['cat_parent_id'],$layer,$out);\n} else {\n$out = '<a href=\"breadcrumb.php?catid=0\">Root Category<\/a> > '.$out;\n}\nreturn $out;\n}\n\n\/\/\/ By default the category is set to 0.\n$catid = 0;\n\/\/\/ Current category id is fetched from the URL Query String\nif(isset($_GET['catid'])){\nif($_GET['catid']!= ''){\n$catid = $_GET['catid'];\n}\n}\n\/\/\/ Output of recursive function\necho breadcrumb($catid);\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>The MySQL table used is below<\/p>\n<pre class=\"brush: sql;\">\n\nCREATE TABLE `category` (\n`cat_id` int(11) NOT NULL auto_increment,\n`cat_name` varchar(255) NOT NULL,\n`cat_parent_id` int(11) NOT NULL,\nPRIMARY KEY  (`cat_id`)\n)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\/ Recursive function to output the breadcrumb function breadcrumb($id,$layer=0,$out=&#8221;){ \/\/\/ If id = 0 outputs that it is Root Category. No need to go any further. if($id ==0){ return &#8220;Root Category&#8221;; } \/\/\/ if id > 0 fetches the details of the category first $crumbsql = mysql_query(&#8220;Select * From category Where cat_id = &#8216;&#8221;.$id.&#8221;&#8216; &#8220;);&hellip; <a class=\"more-link\" href=\"https:\/\/codeboss.in\/web-funda\/2009\/03\/31\/recursive-function-to-output-category-sub-category-breadcrumb\/\">Continue reading <span class=\"screen-reader-text\">Recursive Function to output Category \/ Sub-category Breadcrumb<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[57,62],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-php","tag-recurssion","entry"],"_links":{"self":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeboss.in\/web-funda\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}