• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2535
  • Last Modified:

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

0
irocwebs
Asked:
irocwebs
  • 3
  • 3
1 Solution
 
theremonCommented:
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?
0
 
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

0
 
theremonCommented:
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);
<?php
function getBreadcrumbs($cat) 
	{
	$path="";
	$div=" &gt; ";
	while ($cat!=0)
		{
		$rs=mysql_query("Select * from ddcart_categories where id=$catid");
		$row=mysql_fetch_array($rs);
		if ($row)
			{
			$path=$div . '<a href="index.php?cat="'.$data2['id'].'">' . $row["name"] . . '>' . $path;
			$cat=$row["parent"];
			}
		}
	
	if ($path!="")
		{ $path=substr($path,strlen($div)); }
		
	return $path;
	}
?>

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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

0
 
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

0
 
theremonCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now