How to group dulicate table values in UL? - php

So I'm not sure if its my foreach loop or my sql query that needs fixing, but Ive tried grouping by "categoryname" to no avail. I want the category names ($qs['name']) to be displayed only once and all li belonging to that category displayed in it. Any help would be great.
What I have is this:
NEW HAMPSHIRE <--This is $state_name-->
Personal auto <--This is $qs['name']-->
• Your young drivers—help them play it safe
Personal auto <--This is $qs['name']-->
• The dangers of drunken driving
Personal auto <--This is $qs['name']-->
• How to keep your vehicle safe from car thieves
ETC....
And what I'm looinkg for is this:
NEW HAMPSHIRE <--This is $state_name-->
Personal auto <--This is $qs['name']-->
• Your young drivers—help them play it safe
• The dangers of drunken driving
• How to keep your vehicle safe from car thieves
ETC....
CODE:
$sql = mysql_query(
"SELECT qs, state, title, link, category, categoryid, categoryname
FROM `irc_consumer_content`
JOIN `irc_consumer_content_category`
ON irc_consumer_content.category=irc_consumer_content_category.categoryid
ORDER BY categoryname ASC;");
$row = mysql_fetch_assoc($sql);
$states_array = array( "AL"=>"Four-state topics",
"CT"=>"Connecticut topics",
"NH"=>"New Hampshire topics",
"NJ"=>"New Jersey topics",
"NY"=>"New York topics");
do {
$qs_array[$row['state']][] = array( "qs"=>$row['qs'],
"title"=>$row['title'],
"link"=>$row['link'],
"name"=>$row['categoryname']);
} while ($row = mysql_fetch_assoc($sql));
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) { ?>
<h3><? echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<div class="selected-list-item">
<div id="title-text">
<? echo iconv('Windows-1252', 'UTF-8', $qs['title']); ?>
</div>
<div class="ideas-div">
<div class="ideas-top">
</div>
<div class="idea-images">
</div>
<div class="ideas-bottom">
</div>
</div>
</div>
</li>
</ul>
<? } ?>
<? } ?>
</div>
CODE:(UPDATED)
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<? $category = "";?>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) {
if ($category != $qs['name']){
$category = $qs['name'];
?>
<h3><? echo $category; //echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<? }else { ?>
</li>
</ul>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<? }?>
<!--ALL ELSE IS THE SAME-->

You can check if current category is the same with the previous one. So you can open and close the ul accordingly.
e.g.
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<?php $old_name = ''; ?>
<ul>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) { ?>
<?php if ($old_name != $qs['name']) : ?>
<?php $old_name = $qs['name']; ?>
</ul><--Close previous item ul-->
</ul><--Open ul for new item-->
<?php endif; ?>
<h3><? echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<div class="selected-list-item">
<div id="title-text">
<? echo iconv('Windows-1252', 'UTF-8', $qs['title']); ?>
</div>
<div class="ideas-div">
<div class="ideas-top">
</div>
<div class="idea-images">
</div>
<div class="ideas-bottom">
</div>
</div>
</div>
</li>
<? } ?>
</ul>
<? } ?>
</div>
Also keep in mind that mysql* functions are deprecated. Use PDO instead.

Related

load product in particular category in magento

I am creating navigation for my store. I am new to magento. I am having problem to load product under that category. It is possible for me to print all product under all categories but I don't know how to load products under its specific category.
This is my code
<?php $_menu = $this->renderCategoriesMenuHtml($this->getSelectedCategoryId(), 0, 'level-top') ?>
<?php
if ($this->getTitle() != '') {
$title = $this->getTitle();
} else {
$title = 'Categories';
}
?>
<?php
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
?>
<?php if ($_menu): ?>
<div class="block vertical-menu">
<div class="title-block" style="margin-bottom:0px;"><h4><?php echo $this->__($title) ?></h4></div>
<div class="block-content">
<?php $children = Mage::getModel('catalog/category')->getCategories(2);?>
<ul class="content-navigation">
<?php foreach($children as $category){ ?>
<li>
<?php echo $category->getName();?>
<? foreach($category->getId()){ ?> // here I want to load product as per their category
<ul>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</div>
<?php else: ?>
<?php if (Mage::helper('mpanel')->acceptToUsePanel()): ?>
<div class="block vertical-menu">
<div class="block-title"><strong><span><?php echo $this->__($title) ?></span></strong></div>
<div class="block-content">
<p><?php echo $this->__('Have no categories.'); ?></p>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
try to use following code
$category = Mage::getModel('catalog/category')->load(' Mage::registry('current_category')->getId() '); //You will get current category id use passing value.
$productCollection = $category->getProductCollection();
foreach($productCollection as $product)
{
echo $product->getName();
}
It work..

Opencart Display subcategories

on each products page I currently have displayed the products subcategory, but only one level deep. So if in categories I have Brother > MFC > B4564 > TN450.... and I am on the TN450's product page, down below it will only show BN4564 . I need it also show The mfc and the brother part. I am not sure how to do this.. any ideas? here is my current code.
.TPL file
<!-- Display product categories -->
<?php foreach ($catprod as $catp)
{
?><?php echo $catp['name']; ?> <?php
}`
controller .php file
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($this->request->get['product_id']);
foreach ($product_category as $prodcat)
{
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info)
{
$data['catprod'][] = array('name' => $category_info['description'],'href' => $this->url->link('product/category', 'path=' . $category_info['category_id']));
}
}`
which version of opencart?
this should work for version 1:
open catalog/view/theme/default/template/module/category.tpl
remove its contents and insert:
<div class="box" id="categories-menu">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-category" id="box-category">
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active list-group-item" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="submenu">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

Sending multiple values from php to ajax

I am trying to send 2 values from different parts of a page to the same ajax function. Basically, it's a search filter wherein selecting categories is one module and selecting brands is another both of which are attributes of a product and independent of each other.
I need 3 conditions where search is performed either
only if brand is selected or
only if cat is selected or
if both are selected.
My issue is 1 and 3 are working. but 2nd condition isn't as it takes the cat value as the brand. Please help me correct my function.
My AJAX function:
function filter(subid) {
alert(subid);
var brand_filter = new Array();
$("input:checked").each(function() {
brand_filter.push($(this).val());
});
$.ajax({
type: 'POST',
url: 'getSubcategoryProducts.php',
data: {
brand_filter:brand_filter,
subId: subid
},
success: function(data) {
//alert(data);
$('#productDetail').html(data).fadeIn('slow');
$('#sub_cat_menuItem a').addClass('.collection-category-brands li.active, .collection-category-brands li:hover');
}
});
}
Module of selecting brand:
<div class="collection-brands-filter">
<h4>Filter by Brands</h4>
<div class="brand-filter-form">
<form>
<?php $brandlist = $objBrands-> getAll("status=1");
foreach($brandlist as $branditem){
?>
<span><input type="checkbox" name="brand_filter[]" <?php if ('checked="checked"'){ ?> onClick="filter(<?php echo $branditem['id'];?>);" <?php } ?> value="<?php echo $branditem['id'];?>"/><?php echo $branditem['name'];?></span>
<?php } ?>
</form>
</div>
</div>
Module of selecting category:
<div class="collection-category-brands">
<ul>
<?php
//Fetching sub categories of the main_category
while($query2res = mysql_fetch_array($query2)){ ?>
<li id="sub_cat_menuItem"><?php echo $query2res['1'];?></li>
<?php $subId = $query2res['id'];}?>
</ul>
</div>
getSubcategoryProducts.php code:
<?php
include_once("class/site_root.php");
include('includes/connect.php');
include_once(DIR_ROOT."class/functions.php");
include_once(DIR_ROOT."class/products.php");
$objProducts = new products();
$subId = $_POST['subId'];
//IF ONLY BRAND IS SELECTED AND NOT SUB CATEGORY
if(isset($_POST['brand_filter'])&&!isset($_POST['subId']))
{
echo "only brand is selected";
foreach($_POST['brand_filter'] as $item)
{
$res = $objProducts->getAll('status=1 and brand_id='.$item);?>
<ul>
<?php foreach($res as $val){?>
<li>
<img src="<?php echo $val['img_small']; ?>" alt="icon"/>
<div class="collection-item-hover">
<div class="collection-category-detail">
Know More
<div class="collection-item-detail">
<h4><?php echo $val['product_name']; ?></h4>
<p><?php echo $val['descr_small']; ?></p>
</div>
</div>
</div>
</li>
<?php }?>
</ul>
<?php }
}
//IF ONLY SUB CATEGORY IS CHOSEN!
else if(isset($subId)&&!isset($_POST['brand_filter'])){
echo "only sub cat selected!";
$productsquery=mysql_query("SELECT products.product_name,products.id,products.main_id,
products.img_small,products.descr_small,sub_category.name
FROM products
LEFT JOIN sub_category on
products.sub_id=sub_category.id
WHERE sub_category.id=".$subId);
$count = mysql_num_rows($productsquery);
$subNameVal=mysql_fetch_array(mysql_query("select name from sub_category where id=".$subId));
?>
<h3><?php echo $subNameVal['name'];?></h3>
<?php
if($count>0)
{
?>
<ul>
<?php
while($productsRes=mysql_fetch_array($productsquery))
{
?>
<li>
<img src="<?php echo $productsRes['img_small']; ?>" alt="icon"/>
<div class="collection-item-hover">
<div class="collection-category-detail">
Know More
<div class="collection-item-detail">
<h4><?php echo $productsRes['product_name']; ?></h4>
<p><?php echo $productsRes['descr_small']; ?></p>
</div>
</div>
</div>
</li>
<?php }
?>
</ul>
<?php }
}
//IF BOTH ARE SELECTED
else //if(isset($_POST['brand_filter'])&&isset($subId))
{
echo "both selected!";
echo
"SUB-".$subId."<br/>";
foreach ($_POST['brand_filter'] as $item)
{
echo "brand-".$item."<br/>";
}
}
?>

Get Magento Categories with Especific Sort of Admin Panel

I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();

Categories