Jump to content

breadcrumb page from category


nuttycoder

Recommended Posts

Hi i have a breadbrumb system working for my category's and sub category's using this code

 

<?php
$result = mysql_query("SELECT * FROM cats WHERE name='{$_GET['category']}'");
$info = mysql_fetch_array($result);
$str = $info['name'];

while ($info['parent_id'] != 0) {
$info = mysql_fetch_array(mysql_query("SELECT * FROM cats WHERE id='".$info['parent_id']."'"));
$str = $info['name']." > " .$str ;
}

echo "<p><a href=\"index.php\">Home</a>";
if (isset($str)) { echo " > $str </p>"; } else echo "</p> "; 
?>

 

here is my table scheme for my category's table

`cats` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL default '',
  `parent_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)

 

 

and this is my table scheme for my tutorials

tutorials` (
  `tutorialID` int(4) NOT NULL auto_increment,
  `tutorialTitle` varchar(255) NOT NULL default '',
  `tutorialDesc` text,
  `tutorialCont` text,
  `category` varchar(40) NOT NULL default '',
  `tutorialDate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `tutorialviews` varchar(30) NOT NULL default '0',
  PRIMARY KEY  (`tutorialID`)

 

What i'd like is some advice how I would include pages into the breadcrumb

 

for example as the code stands i can navigate to home > windows xp > email >

but as soon as i click on a tutorial i lose the breadcrumb and just see home > as that part is static.

 

I know i need to change how the code is laid out but not sure what i need to do any advice would be very useful

 

cheers

Link to comment
Share on other sites

when u click a tutorial u mess the breadcrumb maybe cos u loose the the url variable 'category' which is used in the query. Just guessing anyway. Also dont forget to clean your input data, as with your current code, one can inject code as much as he likes.

 

$category = mysql_real_escape_string($_GET['category']);
$result = mysql_query("SELECT * FROM cats WHERE name='$category'");

Link to comment
Share on other sites

try this:

<?php
function crumbs($id, $level=0) {  
	$sql = "SELECT parentid, name FROM cats WHERE id = $id";
	$result = mysql_query($sql);
	list ($p, $n) = mysql_fetch_row($result);

	if ($p != '0') crumbs($p, $level+1);
	echo ($p == '0') ? "$n " : "» $n ";   
}
?>

 

hope that helps

Link to comment
Share on other sites

Surely, saying "bump" every 2 posts doesnt help. U could consider my answer as even if it was pure bullsh** i was trying to help.

 

Yes your right am sorry if I offended you it was not intended, I do need to clean my code I first want to try and find a solution to my problem then clean the code I tend to just create my sits then clean them up once they function probly not the best way to work  :-\

Link to comment
Share on other sites

try this:

<?php
function crumbs($id, $level=0) {  
	$sql = "SELECT parentid, name FROM cats WHERE id = $id";
	$result = mysql_query($sql);
	list ($p, $n) = mysql_fetch_row($result);

	if ($p != '0') crumbs($p, $level+1);
	echo ($p == '0') ? "$n " : "» $n ";   
}
?>

 

hope that helps

 

thanks for that but am not sure that will work as the tutorials are in a table called tutorials and the cats table is just the actual category's,

 

so when i click on a tutorial the breadcrumb code doesen't know where I am I think relate the tutorials with the categorys but in a way they already are as when you click on a category you see all tutorials for that category.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.