Breadcrumbs PHP MySql

I'm new to PHP/MySql and learning on the fly so forgive me if I'm doing this wrong.

I've created a function for a shopping cart that returns a breadcrumb navigation of the categories like this:

Products  > Jane Iredale > Eyes  > Eye Liner > Cream to Powder

The code below returns all the category names correctly (i.e. $data['name']) but does not return all the ids correctly (i.e. $data['id']). In fact, it only returns the id for $data['id'] & $data1['id'].

MySql table is id | name | parent.

Any help would appreciated.

function getParentCatName($cat) {

$sql = "SELECT * FROM ddcart_categories WHERE id = '".$cat."'";
$result = mysql_query($sql);
$data = mysqL_fetch_assoc($result);

$sql1 = "SELECT * FROM ddcart_categories WHERE id = '".$data['parent']."'";
$result1 = mysql_query($sql1);
$data1 = mysqL_fetch_assoc($result1);

$sql2 = "SELECT * FROM ddcart_categories WHERE id = '".$data1['parent']."'";
$result2 = mysql_query($sql2);
$data2 = mysqL_fetch_assoc($result2);

$sql3 = "SELECT * FROM ddcart_categories WHERE id = '".$data2['parent']."'";
$result3 = mysql_query($sql3);
$data3 = mysqL_fetch_assoc($result3);

$sql4 = "SELECT * FROM ddcart_categories WHERE id = '".$data3['parent']."'";
$result4 = mysql_query($sql4);
$data4 = mysqL_fetch_assoc($result4);

$sql5 = "SELECT * FROM ddcart_categories WHERE id = '".$data4['parent']."'";
$result5 = mysql_query($sql5);
$data5 = mysqL_fetch_assoc($result5);

$sql6 = "SELECT * FROM ddcart_categories WHERE id = '".$data5['parent']."'";
$result6 = mysql_query($sql6);
$data6 = mysqL_fetch_assoc($result6);

$sql7 = "SELECT * FROM ddcart_categories WHERE id = '".$data6['parent']."'";
$result7 = mysql_query($sql7);
$data7 = mysqL_fetch_assoc($result7);

$sql8 = "SELECT * FROM ddcart_categories WHERE id = '".$data7['parent']."'";
$result8 = mysql_query($sql8);
$data8 = mysqL_fetch_assoc($result8);

$sql9 = "SELECT * FROM ddcart_categories WHERE id = '".$data8['parent']."'";
$result9 = mysql_query($sql9);
$data9 = mysqL_fetch_assoc($result9);

$list .= '<a href="index.php?cat=0">Products</a>';

if ($data6['id'] != 0) {
$list .= ' > <a href="index.php?cat="'.$data6['id'].'">'.$data6['name'].'</a>';
if ($data5['id'] != 0) {
$list .= ' > <a href="index.php?cat="'.$data5['id'].'">'.$data5['name'].'</a>';
if ($data4['id'] != 0) {
$list .= ' > <a href="index.php?cat="'.$data4['id'].'">'.$data4['name'].'</a>';
if ($data3['id'] != 0) {
$list .= ' > <a href="index.php?cat="'.$data3['id'].'">'.$data3['name'].'</a>';
if ($data2['id'] != 0) {
$list .= ' > <a href="index.php?cat="'.$data2['id'].'">'.$data2['name'].'</a>';

if ($data1['id'] != 0) {
$list .= ' > <a href="index.php?cat='.$data1['id'].'">'.$data1['name'].'</a>';
if ($data['id'] != 0) {
$list .= ' > <a href="index.php?cat='.$data['id'].'">'.$data['name'].'</a>';

return $list;


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi there

sorry to begin like this, but you are indeed doing this wrong (however there's no reason to apologise - you're still learning). Cases like this are the best example of why we're using recursion and loops.
If you don't know what recursion is, it's simply put a piece of code that's executes itself until certain criteria have been met. Like having function abc() which calls itself over and over until you tell it to stop.
Loops are... loops. For loops, while loops etc.

Your issue can be solved both ways. A while loop is certainly the fastest way to do this for me. However, if you want to do it using a recursive function, we can do that too.
Before I provide any code, please let me know which way you want it. And also what's the first level category's ParentID. Is it 0? Null?
irocwebsAuthor Commented:
Hi theremon,

I would say what ever way is going to get this to work. :) If a while loop is the fastest - let's do it that way.

Below I mapped out the table for:

Products > Jane Iredale > Eyes  > Eye Liner > Cream to Powder

"Jane Iredale" would be the parent category in this case.
|id |name           |parent|
|22 |Jane Iredale   |0     |
|101|Eyes           |22    |
|74 |Eye Liner      |101   |
|102|Cream to Powder|74    |

Open in new window

Ok cool - while loop it is. Check out the following code. What it does is take the current category's ID and work its way up till it reaches a category with parent=0. In the process it builds up a sring (that's variable $path) and once done, it sends it back to the caller.
So all you need to do is call:

echo getBreadcrumbs($CURRENTID);
function getBreadcrumbs($cat) 
	$div=" &gt; ";
	while ($cat!=0)
		$rs=mysql_query("Select * from ddcart_categories where id=$catid");
		if ($row)
			$path=$div . '<a href="index.php?cat="'.$data2['id'].'">' . $row["name"] . . '>' . $path;
	if ($path!="")
		{ $path=substr($path,strlen($div)); }
	return $path;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

irocwebsAuthor Commented:
Thanks theremon!

Ok this is making more sense... I knew there was an easier way to do this! Though, I needed to modify the code a bit [see below], I understood exactly what you were doing. :)

There is still one problem I was not able to work out. It does not return the id for $row['id'].

Any ideas?
function getBreadcrumbs($cat) {
	$path = "";
	$div = " &gt; ";
	while ($cat != 0) {
		$rs = mysql_query("SELECT * from ddcart_categories WHERE id= '".$cat."'");
		$row = mysql_fetch_array($rs);
		if ($row) {
			$path = $div.'<a href="index.php?cat="'.$row['id'].'">'.$row['name'].'</a>'.$path;
			$cat = $row['parent'];
	if ($path != "") {
		$path = substr($path,strlen($div)); }

        return $path;

Open in new window

irocwebsAuthor Commented:
Never mind! I got it to work! The above code had an extra quote in it.

$path = $div.'<a href="index.php?cat="'.$row['id'].'">'.$row['name'].'</a>'.$path;

Should be:

$path = $div.'<a href="index.php?cat='.$row['id'].'">'.$row['name'].'</a>'.$path;

Open in new window

You're right - it was my fault. I'm using double quote when I code and didn't remove the one in cat=" when posting your code. My apologies.
In any case, thanks for the points. Glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.