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.
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 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.
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
I want to create a (fairly big) Wordpress user index with the users categorized alphabetically, like this:
A
Amy
Adam
B
Bernard
Bianca
and so on.
I've created a custom Wordpress query which works fine for this, except for one problem: It also displays "empty" letters, letters where there aren't any users whose name begins with that letter. I'd be glad if you could help me fix this code so that it only displays the letter if there's actually a user with a name of that letter :) I've tried my luck by checking how many results there are for that letter, but somehow that's not working.
(FYI, I use the user photo plugin and only want to show users in the list who have an approved picture, hence the stuff in the SQL query).
<?php
$alphabet = range('A', 'Z');
foreach ($alphabet as $letter) {
$user_count = $wpdb->get_results("SELECT COUNT(*) FROM wp_users WHERE display_name LIKE '".$letter."%' ORDER BY display_name ASC");
if ($user_count > 0) {
$user_row = $wpdb->get_results("SELECT wp_users.user_login, wp_users.display_name
FROM wp_users, wp_usermeta
WHERE wp_users.display_name LIKE '".$letter."%'
AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
AND wp_usermeta.meta_value = '2'
AND wp_usermeta.user_id = wp_users.ID
ORDER BY wp_users.display_name ASC");
echo '<li class="letter">'.$letter.'';
echo '<ul>';
foreach ($user_row as $user) {
echo '<li>'.$user->display_name.'</li>';
}
echo '</ul></li>';
}
}
?>
Thanks in advance!
Update:
I edited the SQL into this, but now I have the problem that it spits out an </ul> before the first li, which screws up the layout, like this:
</ul>
</li>
<li class="letter">
<div class="letter_head">A</div>
<ul>
<li>Abigail</li>
</ul>
</li>
<li class="letter">
<div class="letter_head">B</div>
<ul>
<li>Bernard</li>
<li>Bianca</li>
</li>
</ul>
It's in general pretty wonky about the ul and li nesting. How can I fix it?
<?php
$qry = "SELECT DISTINCT wp_users.user_login, wp_users.display_name,
LEFT(UPPER(wp_users.display_name), 1) AS first_char
FROM wp_users, wp_usermeta
WHERE UPPER(wp_users.display_name) BETWEEN 'A' AND 'Z'
OR wp_users.display_name BETWEEN '0' AND '9'
AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
AND wp_usermeta.meta_value = '2'
AND wp_usermeta.user_id = wp_users.ID
ORDER BY wp_users.display_name ASC";
$result = mysql_query($qry);
$current_char = '';
while ($row = mysql_fetch_assoc($result)) {
if (!isset($current_char)) {
echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
echo '<ul>';
} elseif ($row['first_char'] != $current_char) {
echo '</ul>';
$current_char = $row['first_char'];
echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
echo '<ul>';
}
echo '<li>'.$row['display_name'].'</li>';
}
?>
A better solution would be to loop through all users in alphabetical order and for every record check if the first letter differs from that of the last record. If different, you echo out the new letter.
You are doing 52 database calls. Is the user table too big to grab all of them in one query? Also, I believe the order by on the count is unnecessary. I do not know if MySQL ignores it or not.
Your problem is get_results returns an single element array every time for the count query. So $user_count > 0 is always returning true. (I don't understand PHP logic most of the time. Is an array greater than zero?). You need to do something like $user_count[0] to get at the actual number.