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']."";
Related
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: 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.
So I'm making a portfolio website where I can post and edit blogs.
I'm making the blog edit page and I'm having trouble with combining two table together.
The first table is for the blogs and the second table holds all the different blog categories.
Here's how I'm getting the blog posts:
$qStr = "SELECT post_title, post_content, post_description, post_active, category_id FROM posts WHERE post_id = {$post_id}";
Here's how I'm getting the blog categories:
$qStr = "SELECT category_name FROM categories WHERE category_id = {$category_id}";
So in my edit blog post page I have a dropdown box that I need to show which category that blog post is in, and be able to change it. I have a category_id in my blog table. My question is how do I get the dropdown to show which category the post is under?
Right now my drop down code looks like this (note: Right now I'm just populating the dropdown with all the categories)
<div class="form-group">
<label for="categorySelect" class="col-lg-2 control-label">Category</label>
<div class="col-lg-3">
<select class="form-control" id="categorySelect">
<?php
foreach ($categories as $cat) {
echo("<option>{$cat['category_name']}</option>");
}
?>
</select>
</div>
</div>
You need to set up the selected attribute for select (option) element, something like this:
<?php
foreach ($categories as $cat) {
if ($cat['category_name'] == $category_of_your_post)
echo "<option selected=\"selected\">{$cat['category_name']}</option>";
else
echo "<option>{$cat['category_name']}</option>";
}
?>
BTW, I think that you have the wrong queries (if I understood correctly your question). Basically you need to retrieve the category_id of your post and perform an equality question against the array of categories:
$qStrBlog = "SELECT post_title, post_content, post_description, post_active, category_id FROM posts WHERE post_id = {$post_id}";
$qStrCat = "SELECT category_id, category_name FROM categories";
And then:
<?php
foreach ($categories as $cat) {
if ($cat['category_id'] == $blog_data['category_id'])
echo "<option selected=\"selected\">{$cat['category_name']}</option>";
else
echo "<option>{$cat['category_name']}</option>";
}
?>
You have two options:
Add a selected attribute with an if-condition in the foreach loop:
foreach ($categories as $cat) {
echo '<option';
if ($cat['category_id'] == $post['category_id']) echo ' selected';
echo '>'.$cat['category_name'].'</option>';
}
Or use an INNER JOIN on the post SELECT query:
$qStr = "SELECT p.post_title, p.post_content, p.post_description, p.post_active, p.category_id, c.category_name FROM posts INNER JOIN categories c ON c.category_id = p.category_id WHERE post_id = {$post_id}";
and throw the first option element as the "default currently selected" option:
<select class="form-control" id="categorySelect">
<option value="<?= $post['category_id'] ?>"><?= $post['category_name'] ?> (current)</option>
<?php
foreach ($categories as $cat) {
echo("<option>{$cat['category_name']}</option>");
}
?>
</select>
I've made the assumption that you've correctly loaded your result from the first SELECT query into $post. Note that if it's possible to not set a Category to a Post, you should use LEFT JOIN instead and wrap the first option element output contingent on $post['category_id'] not being empty.
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
I'm joining 4 tables and want to create a while loop that spits out results nested under different categories.
My Tables
categories
id | category_name
pages
id | page_name | category
page_content
id | page_id | image_id
images
id | thumb_path
My current SQL join
<?php $all_photos = mysql_query("
SELECT * FROM categories JOIN pages ON pages.category = categories.id
JOIN image_pages ON image_pages.page_id = pages.id
JOIN images ON images.id = image_pages.image_id
");?>
The result I want from a while loop
I would like to get something like this....
Category 1
page 1
Image 1, image 2, image 3
page 2
Image 2, image 4
Category 2
page 3
image 1
page 4
image 1, image 2, image 3
Each image can fall under multiple pages and each page can fall under multiple categories.
at the moment I have 2 solutions, one which lists each category several times according to the the amount of pages inside them:
eg. category 1, page 1, image 1 - category 1, page 1, image 2 etc
One that uses a while loop inside another while loop inside another while loop, resulting in 3 sql queries.
<?php $all_categories = mysql_query("SELECT * FROM categories");?>
<?php
while($all_category = mysql_fetch_array($all_categories)) {
?>
<h4><?=$all_category['category_name']?></h4>
<?php $all_pages = mysql_query("SELECT * FROM pages WHERE category = " . $all_category['id'] . "");?>
<?php
while($all_page = mysql_fetch_array($all_pages)) {
?>
<p><?=$all_page['page_name']?></p>
<?php $all_images = mysql_query("SELECT * FROM images JOIN image_pages ON image_pages.page_id = " . $all_page['id'] . " AND image_pages.image_id = images.id");
?>
<div class="admin-images-block clearfix">
<?php
while($all_image = mysql_fetch_array($all_images)) {
?>
<img src="<?=$all_image['thumb_path']?>" alt="<?=$all_image['title']?>"/>
<?php
}
?>
</div>
<?php
}
}
?>
I think second solution is better and straight forward.
You can do this like:
Update:
<?php
$all_cats=mysql_query("select * from categories");
while($all_cat = mysql_fetch_array($all_cats)){
//print your cat title
$check = mysql_query("select * from images i, pages p, page_content pc categories c where c.id = p.category and p.id = pc.page_id and pc.image_id=i.id");
if(mysql_num_rows($check) > 0){
$all_pages=mysql_query("select * from pages where category=".$all_cat['id']);
while($all_page = mysql_fetch_array($all_pages)){
//print your page
echo "<p>".$all_page['page_name']."</p>";
?>
<div class="admin-images-block clearfix">
<?php
$all_images=mysql_query("select * from images where id=".$all_page['image_id']);
while($all_image = mysql_fetch_array($all_images)) {
//print your img
?>
<img src="<?=$all_image['thumb_path']?>" alt="<?=$all_image['title']?>"/>
<?php}?>
</div>
<?php
}
}
}
?>
Best of luck :)