Jump to content

Breadcrumbs displayed using table info from MySQL


dmfgkdg

Recommended Posts

I have a crude eCommerce website i am building (the-jewelry-district.com/store) and i want to create a breadcrumbs script. here is my product.php page. im not sure if i need the script on my index.php as well.

 

right now i have a simple echo of the categories and sub categories in the table from the database. 7 fields to be exact. Here they are as defined in the script below:

 

$category = $row["category"];

$subcategory = $row["subcategory"];

$sub_subcategory_1 = $row["sub_subcategory_1"];

$sub_subcategory_2 = $row["sub_subcategory_2"];

$sub_subcategory_3 = $row["sub_subcategory_3"];

$sub_subcategory_4 = $row["sub_subcategory_4"];

$sub_subcategory_5 = $row["sub_subcategory_5"];

 

The problem is if the product only requires "category", "subcategoty" & "sub_subcategory_1" only then the end result is something like this:

 

Category | Subcategory | Sub-Subcategory_1 | | | | |

 

I use " | " as the heirachy separator so at the end, the echo still prints these.

 

I based my site off of a 20 video ecommerce php & MySQL tutorial on youtube by a gentleman by the name of Adam Khoury in case you guys are wondering where I got this code from.

 

Here is the entire product.php script I have. The while loop pretty much describes the table I use for my products.

 

 

 

 

 

<?php

include "storescripts/script_error_reporting.php";

 

/*Check to see the URL variable is set and that it exists in the database*/

 

if (isset($_GET['id'])) {

 

// Connect to the MySQL database

include "storescripts/connect_to_mysql.php";

 

$id = ($_GET["id"]);

 

/*Use this var to check to see if this ID exists, if yes then get the product details, if no then exit this script and give message why*/

 

$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");

$productCount = mysql_num_rows($sql); // count the output amount

if ($productCount > 0) {

 

/*get all the product details*/

 

while($row = mysql_fetch_array($sql)){

 

$product_name = $row["product_name"];

$browser_tab_title = $row["browser_tab_title"];

$url_name = $row["url_name"];

$price = $row["price"];

$details = $row["details"];

$category = $row["category"];

$subcategory = $row["subcategory"];

$sub_subcategory_1 = $row["sub_subcategory_1"];

$sub_subcategory_2 = $row["sub_subcategory_2"];

$sub_subcategory_3 = $row["sub_subcategory_3"];

$sub_subcategory_4 = $row["sub_subcategory_4"];

$sub_subcategory_5 = $row["sub_subcategory_5"];

$image_a = $row["image_a"];

$image_b = $row["image_b"];

$image_c = $row["image_c"];

$image_d = $row["image_d"];

$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

}

}

else {

echo "That item does not exist.";

exit();

}

}

else {

echo "Data to render this page is missing.";

exit();

}

mysql_close();

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><?php echo $browser_tab_title; ?></title>

<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />

</head>

<body>

<?php include_once("template_header.php");?>

 

 

 

<div id="breadcrumbs">

 

 

<p>

 

 

<?php

echo "$category | $subcategory | $sub_subcategory_1 | $sub_subcategory_2 | $sub_subcategory_3 | $sub_subcategory_4 | $sub_subcategory_5";

?>

<br />

 

 

</p>

 

 

</div><!--breadcrumbs-->

 

 

 

 

 

<!--PERMANENT ABOVE-->

<div id="prodcont">

<div id="product">

<div id="productContent">

<div class="prodimgl">

<img src="inventory_images/<?php echo $image_a; ?>.jpg" width="300" height="300" >

 

<img src="inventory_images/<?php echo $image_b; ?>.jpg" width="300" height="300" >

<!--<img src="inventory_images/<?php echo $image_c; ?>.jpg" width="300" height="300" >-->

<p>View Full Size Image</p>

<a href="inventory_images/<?php echo $image_a; ?>.jpg" target="_blank">A</a>

<a href="inventory_images/<?php echo $image_b; ?>.jpg" target="_blank">B</a>

<!--<a href="inventory_images/<?php echo $image_c; ?>.jpg" target="_blank">C</a>-->

 

</div><!--prodimg1-->

<div class="proddesc">

<h1>

<?php echo $product_name; ?>

</h1>

<div id="productDescription">

<?php

echo $details;

?>

<br />

<?php

echo "$".$price;

?>

 

<br />

<br />

<br />

 

</p>

<form id="form1" name="form1" method="post" action="cart.php">

<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />

<input type="submit" name="button" id="button" value="Add to Shopping Cart" />

</form>

 

</div><!--productDescription-->

</div><!--proddesc-->

</div><!--productContent-->

</div><!--product-->

</div><!--prodcont-->

<?php include_once("template_footer.php");?>

</body>

</html>

Link to comment
Share on other sites

Well, the obvious answer is to check if "sub_subcategory_X" is empty before you display it.

 

But I have to point out that that is some awful database design. Please read this article, which shows a more logical way to work with hierarchical data.

 

EDIT: Also, please use


tags next time to display your code.

Edited by scootstah
Link to comment
Share on other sites

Thats the thing. I want to be able to leave "sub_subcategory_X" empty and not display the "|" after it. And you have to understand that the categories are not dynamically created. I will be hand typing in each category, subcategory, etc... in excel, uploading a csv file to MySQL. So I font need the MySQL structure changed. Thanks for trying to help!

Link to comment
Share on other sites

MySQL is not a spreadsheet. Trying to contort it into one is just going to end in a headache. But, if you feel that system is adequate, then by all means.

 

The way your code is now, you'd have to run a conditional for each variable. So something like:

if (!empty($category)) {
   echo $category . '| ';
}

 

You might consider using an array instead, that way you could loop over it or just implode it.

$breadcrumbs = array($category, $subcategory, $sub_subcategory_1, ...);

echo implode(' | ', $breadcrumbs);

Link to comment
Share on other sites

Now we are getting somewhere. Thanks. So Both of these work, I would prefer to use the array. However, it still echos out the end "|" several times for the empty variables. How do I make the implode function only display the variables that are not empty? I did this:

 


$breadcrumbs = array($category, $subcategory, $sub_subcategory_1, $sub_subcategory_2, $sub_subcategory_3, $sub_subcategory_4, $sub_subcategory_5);

echo implode(' | ', $breadcrumbs);

Edited by dmfgkdg
Link to comment
Share on other sites

Thanks for all of your help. This ended up working for me:

 


$breadcrumbs = array($category, $subcategory, $sub_subcategory_1, $sub_subcategory_2, $sub_subcategory_3, $sub_subcategory_4, $sub_subcategory_5);

echo implode(' | ', array_filter($breadcrumbs));

 

although

 

echo join(' | ', array_filter($breadcrumbs));

 

works too.

 

 

I forgot to ask about the hyperlinks. If I want to make these breadcrumbs clickable, how would I do that?

 

Any suggestions would be greatly appreciated.

Edited by dmfgkdg
Link to comment
Share on other sites

Having reviewed your original post (which I would have read properly had you used code tags) I've realised your database design is not good at all. Your table for categories should be completely separate from the products table with a field indicating its parent category if any. If its a main category you can set the "parentCategoryID" equal to NULL.

 

I strongly recommend you redo your database else you'll have bad code throughout your application which is what your previous post has.

Link to comment
Share on other sites

I only did it this way to have the backend of my site have a page where an employee can easily fill out the fields through a simple add product page instead of having them go into MySQL. I will look into putting the categories in another table, It's just going to be a pain to redo that backend page because I dont know this stuff. All in a days work I guess. Thank you.

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.