Hello I just a little but help.
What I am trying to do is create a menu with a submenu by getting the data from 2 different mysql tables.
The issue I am having is the submenu will not display all results from the "Sub Category" database depending on what page I am on
Table 1: Category "ID, Name"
Table 2: Sub Category "ID, Name, Cparent, filename (Image for sub category)"
I have the following code:
<ul>
<?php
$catmenu_sql = 'select category.id AS catid, category.name AS catname, scategory.cparent AS scparent, scategory.name AS scname
from category
left join scategory on category.id = scategory.cparent
group by category.name'; // Select data from database
$result = mysql_query($catmenu_sql);
while($rows = mysql_fetch_array($result)) { ?>
<!-- Begin Category list -->
<li class="menu">
<?php echo($rows['catname']); ?>
</li>
<!-- End Category List -->
<?php
if (isset($_GET['id']) && is_numeric($_GET['id'])) // get the 'id' variable from the URL and match it with scategory parent in database
$id = $_GET['id'];
$sid = $rows['scparent'];
if ( $id == $sid ) {
?>
<!-- Begin Sub Category List -->
<ul>
<li class="menu"><?php echo $rows['scname']; ?>
</li>
</ul>
<!-- End Sub category List -->
<?php }} ?>
</ul>'
Any help will be appreciated. Thank you
you can try replace your code with this
<?php
$catmenu_sql = 'select category.id AS catid, category.name AS catname, scategory.cparent AS scparent, scategory.name AS scname
from category
left join scategory on category.id = scategory.cparent
group by category.name'; // Select data from database
$result = mysql_query($catmenu_sql);
$sHTML="<ul>";
while($rows = mysql_fetch_assoc($result))
{
$sHTML="<ul>"
<!-- Begin Category list -->
$sHTML .= '<li class="menu">' .
'' . $rows['catname'] .'' .
'</li>' ;
<!-- End Category List -->
$subquery = "SELECT sec_name FROM tbl_user_sec WHERE `sec_group` = '" . mysql_real_escape_string($row_secs['sec_group'] . "'";
$subresult = mysql_query($subquery);
<!-- Begin Sub Category List -->
if (isset($_GET['id']) && is_numeric($_GET['id'])) // get the 'id' variable from the URL and match it with scategory parent in database
$id = $_GET['id'];
$sid = $rows['scparent'];
if ( $id == $sid )
{
$sHTML .="<ul>";
while($row = mysql_fetch_assoc($subresult) ) {
$sHTML .= '<li class="menu">' . $rows['scname'] . '' .
'</li>' ;
}
$sHTML .="</ul>";
}
<!-- End Sub category List -->
}
$sHTML .= '</ul>'
echo $sHTML
?>
If i understand your problem correctly, this line:
if ( $id == $sid ) {
filters the sub categories depending of the page you're, given that $id is being initialized as:
$id = $_GET['id'];
which is the value of a parameter named id that is being passed to the url, possibly like this:
http://www.site.com/script.php?id=1
that's what making you see only a sub set of the categories, to see them all you should not pass the id parameter to the url.
Related
Hello i'm working on this all day today and i'm actually really lost i have a table like this at the moment.
id | category | parent_id
id=5 category=Laptop parent_id=0
id=7 category=Houses parent_id=0
id=8 category=HP parent_id=5
id=9 category=Lenovo parent_id=5
so what i'm trying to do is get this as a category sub category style something like;
Laptop
HP
Lenovo
Houses
until know what i have;
<?php
include_once("dir/db.php");
$result = $db->query("SELECT * FROM categories WHERE parent_id = 0");
?>
<ul>
<?php
while($row = $result->fetch(PDO::FETCH_ASSOC)):
?>
<li><?php echo $row['category'] ?></li>
<?php endwhile;?>
</ul>
output;
<ul>
<li>Laptops</li>
<li>Houses</li>
</ul>
but i'm trying to get my Lenovo and HP under Laptop i'd be really happy if you can help me! Thanks.
<?php
include_once("dir/db.php");
$result = $db->query("SELECT * FROM categories WHERE parent_id = 0");
echo "<ul>";
while($row = $result->fetch(PDO::FETCH_ASSOC)):
echo "<li>" . $row['category'] . "</li>";
$sub_result = $db->query("SELECT * FROM categories WHERE parent_id = " . $row['id']);
echo "<ul>";
while($sub_row = $sub_result->fetch(PDO::FETCH_ASSOC)):
echo "<li>" . $sub_row['category'] . "</li>";
endwhile;
echo "</ul>";
endwhile;
echo "</ul>";
?>
I have two tables - home and posts;
home.artid is equal to posts.id.
Want select id, artid, inde from home PLUS title from posts
where home.pos is slider:
$items = "";
$st = $db->query("select home.id, home.artid, home.inde
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['posts.title'] . "</div>\n";
}
echo $items;
Error:
Undefined index posts.title...
Any help?
If you want to select posts.title then select it
$st = $db->query("select home.id, home.artid, home.inde,
posts.title
from home
join posts on home.artid = posts.id
where home.pos = 'slider'
order by home.inde asc");
// and it would be called just `title`
while ($row = $st->fetch()){
$items .= "<div class='slidertitle'>" . $row['title'] . "</div>\n";
}
echo $items;
I am joining three tables, I prefixed each column name with the table so each column name is distinct. The query gives me the result I want in MySQL but when I attempt to echo it out specific field name in php, they won't display. I have tried both mysqli_fetch_arrary() and mysqli_fetch_assoc().
<?php
$query = "SELECT * ";
$query .= "FROM plans ";
$query .= "INNER JOIN plans_items ON plans.plan_id = plans_items.fk_plan_id ";
$query .= "INNER JOIN items ON plans_items.fk_item_id = items.item_id ";
$item_set = mysqli_query($connection, $query);
confirm_query($item_set);
?>
<ul class="pages">
<?php
while($single_item = mysqli_fetch_array($item_set)) {
?>
<li><?php echo "Name :" .$row['item_name']. "."; ?></li>
<?php
}
?>
<?php mysqli_free_result($item_set); ?>
</ul>
I want to list Main categories and subcategories but dont know how ?
What I want to do is like this
Main cat 1
-subcat 1
-subcat 2
-subcat others
Main cat 2
-subcat 1
-subcat 2
-subcat others
I have two part codes which is printing only main categories like this,
Main cat1
Main cat2
Main cat3
Here is the total codes Trying separate ways but cant do it.
$sql_select_cats_list = $db->query("SELECT category_id, items_counter, hover_title FROM " . DB_PREFIX . "categories WHERE parent_id=0 AND hidden=0 AND user_id=0 AND enable_auctions=1 ORDER BY order_id ASC, name ASC");
$cats = array();
while($row=$db->fetch_array($sql_select_cats_list)) {
$cats[$row['category_id']] = $row;
}
$cat_ids = implode(', ', array_keys($cats));
$sql_select_subcats_list = $db->query("SELECT sub.category_id, sub.items_counter, sub.hover_title, parent.order_id FROM " . DB_PREFIX . "categories sub LEFT JOIN " . DB_PREFIX . "categories parent ON (parent.category_id=sub.parent_id) WHERE parent.category_id IN (" . $cat_ids . ") AND sub.hidden=0 AND sub.user_id=0 AND sub.enable_auctions=1 ORDER BY parent.order_id ASC, sub.order_id ASC, sub.name ASC");
while($row=$db->fetch_array($sql_select_subcats_list)) {
if(!isset($subs[$row['parent_id']])) {
$subs[$row['parent_id']] = array();
}
$subs[$row['parent_id']][] = $row;
}
$template->set('cats_list', $cats);
$template->set('subs_list', $subs);
$category_box_content = $template->process('categories_box.tpl.php');
$template->set('category_box_content', $category_box_content);
I am trying to list categories and sub categories with this code:
<?php
foreach($cats as $parent_id => $cats_header_details) {
$category_link = process_link('categories', array('category' => $category_lang[$cats_header_details['category_id']], 'parent_id' => $cats_header_details['category_id']));
// subcategory links:
if(isset($subs_list[$parent_id])) {
foreach($subs_list[$parent_id]) as $sub) {
// do something with your subcategory
}
}
?>
<li><a style="font-size: 11px; font-weight: normal;" href="<?=$category_link;?>"
<?=((!empty($cats_header_details['hover_title'])) ? 'title="' . $cats_header_details['hover_title'] . '"' : '');?> alt="">
<img src="themes/<?=$setts['default_theme'];?>/img/arrow.gif" border="0" hspace="4"><?=$category_lang[$cats_header_details['category_id']];?>
<?=(($setts['enable_cat_counters']) ? (($cats_header_details['items_counter']) ? '(<strong>' . $cats_header_details['items_counter'] . '</strong>)' : '') : '');?></a> </li> <?
}
?>
I get this Warning: Think foreach is returning empty ! cant find the way how to solve it.
Warning: Invalid argument supplied for foreach() in C:\AppServ\www\site\themes\temamiz\templates\categories_box.tpl.php on line 14
And print Main categories on the page with the code below.
<?=$category_box_content;?>
Basically change your pre-template code to first get all category_ids from your categories. Then get a list of subcats which matches that list.
Now process the subs, ordering them to easily be found by their parent id (2nd loop).
Then instead of passing an sql resource to your template (bad form), you hand out the 2 arrays (cats and subcats).
In your template, you then loop through the categories, then within that loop, you have a 2nd loop which loops only over the subcats for that specific category (if any).
In code, like this:
$sql_cats_list = $db->query("SELECT category_id, items_counter, hover_title FROM " . DB_PREFIX . "categories WHERE parent_id=0 AND hidden=0 AND user_id=0 AND enable_auctions=1 ORDER BY order_id ASC, name ASC");
$cats = array();
while($row=$db->fetch_array($sql_cats_list)) {
$cats[$row['category_id']] = $row;
}
$cat_ids = implode(', ', array_keys($cats));
$sql_subcats_list = $db->query("SELECT sub.category_id, sub.items_counter, sub.hover_title, parent.order_id FROM " . DB_PREFIX . "categories sub LEFT JOIN " . DB_PREFIX . "categories parent ON (parent.category_id=sub.parent_id) WHERE parent.category_id IN (" . $cat_ids . ") AND sub.hidden=0 AND sub.user_id=0 AND sub.enable_auctions=1 ORDER BY parent.order_id ASC, sub.order_id ASC, name ASC");
while($row=$db->fetch_array($sql_subcats_list)) {
if(!isset($subs[$row['parent_id']])) {
$subs[$row['parent_id']] = array();
}
$subs[$row['parent_id']][] = $row;
}
$template->set('cats_list', $cats);
$template->set('subs_list', $subs);
$category_box_content = $template->process('categories_box.tpl.php');
$template->set('category_box_content', $category_box_content);
I assume this was your categories_box.tpl.php file (?)
<?php
foreach($cats as $parent_id => $cats_header_details) {
$category_link = process_link('categories', array('category' => $category_lang[$cats_header_details['category_id']], 'parent_id' => $cats_header_details['category_id']));
// subcategory links:
if(isset($subs_list[$parent_id])) {
foreach($subs_list[$parent_id] as $sub) {
// do something with your subcategory
}
}
?>
<li><a style="font-size: 11px; font-weight: normal;" href="<?=$category_link;?>"
<?=((!empty($cats_header_details['hover_title'])) ? 'title="' . $cats_header_details['hover_title'] . '"' : '');?> alt=""><img src="themes/<?=$setts['default_theme'];?>/img/arrow.gif" border="0" hspace="4"><?=$category_lang[$cats_header_details['category_id']];?> <?=(($setts['enable_cat_counters']) ? (($cats_header_details['items_counter']) ? '(<strong>' . $cats_header_details['items_counter'] . '</strong>)' : '') : '');?></a> </li> <?
}
?>
And print Main categories on the page with the code below.
<?=$category_box_content;?>
Is there a way to get the category parent_id of a product, in the product page of Opencart v1.5.4.1. What am trying to change the button href based on the category parent_id.. If parent_id = 20 then the button should have href1 else href2.
So far, i've done this but its not working.
Added before "$this->load->model('tool/image');"
$product_cat = $this->model_catalog_product->getCategories($product_id);
$product_cat_parent = $this->model_catalog_category->getCategory($product_cat[0]['category_id']);
if ($product_cat_parent['parent_id'] == '20') {
$this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=12');
} else {
$this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=13');
}
in template file:
<a class="button2 sizeguidebox" href="<?php echo $sizeguide; ?>"><?php echo $text_sizeguide; ?></a>
This looks more complicated but it actually simpler and more efficient as a fool proof method of checking. It will pick all of the categories related to the current product, find all their category info, and filter the categories with the parent_id of 20. If the query has any results (i.e. one of the categories is a subcategory of the category with ID 20) then it will set the faq info accordingly
$product_cat = $this->model_catalog_product->getCategories($product_id);
$result = $this->db->query("
SELECT
`c`.`parent_id`
FROM
`" . DB_PREFIX . "category` `c`
LEFT JOIN
`" . DB_PREFIX . "product_to_category` `p2c`
ON
`c`.`category_id` = `p2c`.`category_id`
WHERE
`c`.`parent_id` = '20'
AND
`p2c`.`product_id` = '" . (int) $product_id . "'
");
if($result->num_rows > 0) {
$this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=12');
} else {
$this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=13');
}
Note that this hasn't been tested but should work in theory