I want to have breadcrumb on my website looks like this: https://jsfiddle.net/pk88b9nb/
My categories table structure is like that: id -- name -- parentid
When i use php code below:
<ol>
<li>Home</li>
<li>
<?php
$query = $handler->query("SELECT * from categories where id=".$_GET['id']."");
while($r = $query->fetch())
{
echo "$r[name]";
}
?>
</li>
</ol>
outcome is: Home / Category.
even subcategories outcome is: Home / Subcategory.
I want to make it like Home / Category / Subcategory
how should i change my php code?
You can get the category is there is a parentid with a subquery
<ol>
<li>Home</li>
<?php
$query = $handler->query("SELECT *, (SELECT name FROM categories as s WHERE s.id = c.parentid) as category from categories as c where id= ".$_GET['id']."");
while($r = $query->fetch())
{
if (!empty($r['category'])) echo "<li>$r[category]</li>";
echo "<li>$r[name]</li>";
}
?>
</ol>
You should also escape the $_GET variable in the sql query to avoid injection attacks.
Related
i have two tables Category and sub category in php i want to show them in accordion
but my code is not working please help. below are the columns on two tables.
category = c_id, category_name.
sub_category = sub_cat_id, sub_cat_name, c_id
<?php
$sql="select * from category";
$res=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($res))
{
?>
<li class="subMenu"><a> <?php echo $row["category_name"]?> [840] </a>
<ul style="display:none">
<?php
$sql1="SELECT * FROM category INNER JOIN sub_category ON
sub_category.cat_id = category.cat_id;";
$res1=mysqli_query($con,$sql1);
while($row1=mysqli_fetch_array($res1))
{
?>
<li><a href="products.html"><i class="icon-chevron-right"></i><?php echo
$row1["sub_cat_name"]?></a></li>
<?php } // while loop
?>
</ul>
</li>
<?php
}
Hi just used this query in my second while loop and now its working fine
Query $sql1="select * from sub_category WHERE cat_id = ".$row['cat_id']."";
I know this question has been asked 1000s of times before but please bear with me as I have a slightly different scenarion.
Basically I have 2 MYSQL tables that I join (one called category and the other is products) and I create categories and subcategories menu in my PHP page using the following code:
<?php
$subcategories="";
$sql3="SELECT products.di_cat, GROUP_CONCAT(products.di_name) as di_name
FROM products LEFT JOIN category ON products.di_cat=category.names
GROUP BY products.di_cat";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {
while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){
$make_names = $row3["di_cat"];
$make_model = $row3["di_name"];
$subcategories = explode(",",$make_model);
print '<div class="col-md-3">
<span class="mega-menu-sub-title">'.$make_names.'</span>
<ul class="sub-menu">
<li>
<ul class="sub-menu">';
foreach($subcategories as $sub){
print '<li>'.$sub.'</li>';
}
print '</ul></li></ul></div>';
}
}
?>
This works fine and as it should.
Now I have a column in my products table called url.
This column is for creating clean URLs and the content of is basically the products names without any spaces or special characters.
I would like to use this in my code above but I don't understand why every time I try to use the follwoing code, I only get 1 subcategory/product from each category being displayed in my PHP page even though I have 100s of products under each category!
This what i tried:
<?php
$subcategories="";
$sql3="SELECT products.di_cat, products.url, GROUP_CONCAT(products.di_name) as di_name
FROM products LEFT JOIN category ON products.di_cat=category.names
GROUP BY products.di_cat";
$query3 = mysqli_query($db_conx, $sql3);
$existCount3 = mysqli_num_rows($query3);
if ($existCount3!=0) {
while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){
$make_names = $row3["di_cat"];
$make_model = $row3["di_name"];
$make_url = $row3["url"];
$subcategories = explode(",",$make_url);
print '<div class="col-md-3">
<span class="mega-menu-sub-title">'.$make_names.'</span>
<ul class="sub-menu">
<li>
<ul class="sub-menu">';
foreach($subcategories as $sub){
print '<li>'.$sub.'</li>';
}
print '</ul></li></ul></div>';
}
}
?>
Can someone please advice on this issue?
Any help would be appreciated.
I want to have breadcrumb on my website looks like this: Home / Category / Sub Category / Article example: https://jsfiddle.net/10kLs0rc/
table structures
categories: id -- parentid -- title
articles: id -- categoryid -- title -- content
*
It's easy to print article's title to breadcrumb. But I also want to print category of article. (even subcategory if article has it. as you can see the example above.)
I think i need php code with double query, one for "categories" and other for "articles" table.
*
EDIT
Page i want to build called "article.php" user will visit: "article.php?id=1"
I can fetch all data I want, except articles parent categories.
Here is the code I simply have, and I want to create another query for fetching parent categories of article but I'm stuck.
<?php
$id = $_GET['id'];
$query = $handler->query("SELECT * FROM article WHERE id='$id'");
while($r = $query->fetch()) {
echo "
<h3>$r[title]</h3>
<p>$r[content]</p>
";
}
?>
This is the query you want :
select
`a`.`title` as `art_title`,
`a`.`content` as `art_content`,
`c1`.`title` as `cat_title`,
`c2`.`title` as `cat_title_parent`
from `article` as `a`
left join `categories` as `c1`
on `c1`.`id` = `a`.`categoryid`
left join `categories` as `c2`
on `c2`.`id` = `c1`.`parentid`
where `a`.`id`=2;
And Here is the POC on [SQLFiddle
So in your php :
while($r = $query->fetch()) {
echo '<i>';
if(isset($r['cat_title_parent']))
echo '/'.$r['cat_title_parent'];
if(isset($r['cat_title']))
echo '/'.$r['cat_title'];
echo '</i><br />';
echo '<h3>'.$r['art_title'].'</h3>';
echo '<p>'.$r['art_content'].'</p>;
}
i was given help previously on how to join multiple tables together to get this navigation list to work, as you can see i have done this, but now i am trying to output the navigation in my list, but it is duplicating the top and bottom categories based on how many products are in those categories: this is previous link that shows my table setup:
Joining 2 tables with foreign key id
Here is my code trying to echo out the navigation correctly.
try
{
$result = $pdo->query('SELECT product.*, bottom_category.bottom_name, top_category.top_name
FROM product
INNER JOIN bottom_category ON bottom_category.id = product.bottom_category_id
INNER JOIN top_category ON top_category.id = bottom_category.top_category_id
ORDER BY top_category.id,bottom_category.id');
} // end try
catch (PDOException $e)
{
echo 'There was a error fetching the products.' . $e->getMessage();
exit();
} // end catch
$products = array();
foreach ($result as $row)
{
$products[] = array('top_name' => $row['top_name'],
'bottom_name' => $row['bottom_name']);
}
?>
<div class="sidebar">
<h4 class="sidebar-header">Select Products</h4>
<form class="nav-search-form">
<input type="search" name="search" placeholder="Search products">
</form>
<nav class="sidebar-links">
<ul>
<li><a id="red" href="/semtronics/index.php">New Products</a></li>
<?php
foreach ($products as $product):
?>
<li><?php echo htmlspecialchars($product['top_name']);?>
<ul>
<li><?php echo htmlspecialchars($product['bottom_name']);?></li>
</ul>
</li>
<?php endforeach; ?>
</ul>
</nav>
</div><!-- sidebar -->
Now it all works the only problem is it is duplicating the navigation list based on how many products are linked to that category.
I think you need another solution for this task.
You don't need product here
Query what you need is:
select bottom_category.name as bottom_name, top_category.name as top_name
from bottom_category
inner join top_category on top_category.id = bottom_category.top_category_id
order by top_category.id, bottom_category.id
I was thinking that you need something from product table in your code, but if no - use SQL query above.
Or you need data from product table ?
If you need from product you can run
select bottom_category.name as bottom_name, top_category.name as top_name
from product
inner join bottom_category on bottom_category.id = product.bottom_category_id
inner join top_category on top_category.id = bottom_category.top_category_id
group by product.bottom_category_id
order by top_category.id, bottom_category.id
But be careful, you don't know which row from product would be used in this case
Ok so i am having a problem with figuring this problem out. I have a dynamic page that has records pulled out of a mysql database. The php code works great. The code is below
$catagory = $_GET['type'];
$query = "SELECT p.name, p.price, pc.quantity, p.image, p.descr FROM products ... where category_name = $catagory ";
$result = mysql_query($query);
$related = array();
while($row_r = mysql_fetch_assoc($result)){
$related[] = $row_r;
}
later on down the page I loop through them
<div class="main-blue">
<div class="blue-items">
<?php foreach ($related as $row_r) { ?>
<span class="digit"><span class="digit-r"><?php print $row_r["quantity"] ?></span></span>
<h3><?php print $row_r['name'] ?></h3>
.....
The problem is I have a
<div class="dropdown">
<ul>
<li>Grocery</li>
<li>Pizza</li>
<li>Quick Service</li>
<li>Retail</li>
<li>Salon</li>
<li>Bar</li>
</ul>
I need to be able to select one of the dropdowns like for example Bar I need to regrab the record from mysql and make the page load with the records for Bar without refreshing .
like
$catagory = "Bar";
$query = "SELECT p.name, p.price, pc.quantity, p.image, p.descr FROM products ... where category_name = $catagory ";
You need to use some sort of AJAX implementation to get this to work.
It will allow Js to query your PHP script to get a table or new JSON data to repopulate the table.
It doesn't look like you have any jQuery at the moment, give it a go to start with!
This question is very similar