Page 1 of 1
[solved] ListIt 2 subcategory listing
Posted: Fri Mar 23, 2012 9:01 am
by HarmO
i'm working with the latest verion of CMSMS (1.10.3 "Hyacinthe") and i'm using listIt2 (version 1.1) because it can work with subcategories.
I made a listing of values, but currently, a sub category shows at the same level as a parent company...
What i want to achieve:
- category 1
-- subcategory a
--- list of values
-- subcategory b
--- list of values
-category 2
-- list of values
any ideas?
my current template code:
Code: Select all
{assign var='current_category' value=''}
{assign var='open' value=false}
{foreach from=$items item=item}
{if $current_category != $item->category_alias}
{if $open}</div><!-- items -->{/if}
<a name="{$item->category_alias|cms_escape}"></a><h2 class="category-title">{$item->category_name|cms_escape}</h2>
<div class="items">
{assign var='current_category' value=$item->category_alias}
{assign var='open' value=true}
{/if}
<div class="item">
{if !empty($item->fielddefs)}
<div class="details item-title">{$item->title|cms_escape}</div>
{foreach from=$item->fielddefs item=fielddef}
{if $fielddef.type == 'upload_file' || $fielddef.type == 'select_file'}
{if $fielddef.value|cms_escape}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"><a target="_blank" href="/uploads/{$fielddef.value|cms_escape}">{$fielddef.name|cms_escape}</a></div>{else}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"> </div>{/if}
{elseif $fielddef.type == 'textbox'}
{if $fielddef.value|cms_escape}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"><a target="_blank" href="{$fielddef.value|cms_escape}">{$fielddef.name|cms_escape}</a></div>{else}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"> </div>{/if}
{else}
<div class="details {$fielddef.name|cms_escape}">{$fielddef.value}</div>
{/if}
{/foreach}
{/if}
</div><!-- item -->
{/foreach}
{if $open}</div><!-- items -->{/if}
i suppose it has got something to do with not checking on parent category id but i have no idea on how i can check it.
Re: ListIt 2 subcategory listing
Posted: Fri Mar 23, 2012 11:19 am
by HarmO
Ok, i continue searching for a solution.
I tried to check on parent_ID (this is a field in the database)
but in debug mode this ID is not known...
Code: Select all
{assign var='current_category' value=''}
{assign var='parent_category' value='-1'}
{assign var='open' value=false}
{foreach from=$items item=item}
{if $current_category != $item->category_alias}
{if $open}</div><!-- items -->{/if}
{if $parent_category != $item->parent_id}
<a name="{$item->category_alias|cms_escape}"></a><h2 class="subcategory-title">{$item->category_name|cms_escape}</h2>
{else}
<a name="{$item->category_alias|cms_escape}"></a><h2 class="category-title">{$item->category_name|cms_escape}</h2>
{/if}
<div class="items">
{assign var='current_category' value=$item->category_alias}
{assign var='parent_category' value=$item->parent_id}
{assign var='open' value=true}
{/if}
<div class="item">
{if !empty($item->fielddefs)}
<div class="details item-title">{$item->title|cms_escape}</div>
{foreach from=$item->fielddefs item=fielddef}
{if $fielddef.type == 'upload_file' || $fielddef.type == 'select_file'}
{if $fielddef.value|cms_escape}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"><a target="_blank" href="/uploads/{$fielddef.value|cms_escape}">{$fielddef.name|cms_escape}</a></div>{else}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"> </div>{/if}
{elseif $fielddef.type == 'textbox'}
{if $fielddef.value|cms_escape}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"><a target="_blank" href="{$fielddef.value|cms_escape}">{$fielddef.name|cms_escape}</a></div>{else}<div class="details {$fielddef.name|munge_string_to_url} {$fielddef.type|cms_escape}"> </div>{/if}
{else}
<div class="details {$fielddef.name|cms_escape}">{$fielddef.value}</div>
{/if}
{/foreach}
{/if}
</div><!-- item -->
{/foreach}
{if $open}</div><!-- items -->{/if}
But also if the parent_id would be known, and i start with products in a subcategory he will not echo the parent company.
anyone an idea? could real use some help.
Re: ListIt 2 subcategory listing
Posted: Fri Mar 23, 2012 8:13 pm
by uniqu3
{$item->category_parent_id} then check against {$item->category_id}
User <pre>{$items|print_r}</pre> and you will see what is available.
Re: ListIt 2 subcategory listing
Posted: Mon Mar 26, 2012 9:58 am
by HarmO
Thx unique3
but it seems that, even if i have the parent ID, i can't display the parent category name.
Is it better to work with a udt you think?
Re: ListIt 2 subcategory listing
Posted: Fri Mar 30, 2012 2:56 pm
by HarmO
really nobody that can help me?
Re: ListIt 2 subcategory listing
Posted: Sun Apr 01, 2012 6:42 pm
by Peciura
Create UDT 'listit_cat_hash'. it transforms category array to category hash:
Code: Select all
/** listit_cat_hash UDT
* Transform ListIt category array to a hash
*
* $params['categories'] category array. Mandatory.
* $params['key'] category property to use as key. Default - 'id'. Optional.
* $params['assign'] save result to a variable. Default - 'categories'. Optional.
**/
if(!empty($params['categories']) ){
$smarty = cms_utils::get_smarty();
$categories = $params['categories'];
if (!empty($params['assign'])){
$assign = $params['assign'];
}
else{
$assign = 'categories';
}
if (!empty($params['key'])){
$key = $params['key'];
}
else{
$key = 'id';
}
$return = array();
foreach($categories as $category){
$return[$category->{$key}] = $category;
}
$smarty->assign($assign, $return);
}
By default hash key is category ID. In your case you need to supply only $categories to UDT. Call it at the top of the template
Code: Select all
{listit_cat_hash categories=$categories}
Now you can access name in {foreach} loop like this:
Code: Select all
{assign var='category_parent_id' value=$item->category_parent_id}
{$categories.$category_parent_id->name}
You can access the rest of properties (description, alias , id, parent_id, hierarchy) too:
Code: Select all
{$categories.$category_parent_id->description}
Also any of them can be used as a hash key:
Code: Select all
{listit_cat_hash categories=$categories key='alias'}
Re: ListIt 2 subcategory listing
Posted: Tue Apr 03, 2012 3:01 pm
by HarmO
ok used a udt! created my own query and display script (had some help to write php)
Re: [solved] ListIt 2 subcategory listing
Posted: Tue Aug 21, 2012 6:52 am
by allan1412
I also need to be able to show the subcategory listing. Would you mind sharing how managed to get it working?
Re: [solved] ListIt 2 subcategory listing
Posted: Wed Feb 06, 2013 12:56 pm
by HarmO
ok,
I know it is a late raction, but here is the result:
http://www.derbigum.fr/produits
I had created some additional fields like
- Fiche-technique (alias TF)
- CE (alias CE)
- FDS (alias FDS)
obtained the result with an UDT written by
buildbyrobot
Note: if you renamed the module (like i did) you will need to change the table names in the queries!
Code: Select all
/**
* Generate a parentcat - subcat - product array
**/
$sql = 'SELECT category_id, category_name FROM cms_module_listit2_category WHERE parent_id = -1 ORDER BY position';
$qry = mysql_query($sql);
while ($p_cat = mysql_fetch_object($qry)){
$sql2 = 'SELECT category_id, category_name FROM cms_module_listit2_category WHERE parent_id = ' . $p_cat->category_id . ' ORDER BY position';
$qry2 = mysql_query($sql2);
if (mysql_num_rows($qry2)){
echo '<a name="#cat' . $p_cat->category_id . '"></a>';
echo '<h2 class="category-title">' . $p_cat->category_name . '</h2>';
while ($cat = mysql_fetch_object($qry2)){
$psql = 'SELECT item_id, title, alias FROM `cms_module_listit2_item` WHERE category_id = ' . $cat->category_id . ' AND active = 1 ORDER BY position';
$pqry = mysql_query($psql);
if (mysql_num_rows($pqry)){
echo '<a name="#cat' . $cat->category_id . '"></a>';
echo '<h3 class="category-title">' . $cat->category_name . '</h3>';
echo '<div class="items">';
while ($item = mysql_fetch_object($pqry)){
// get the details
$dsql = 'SELECT cms_module_listit2_fielddef.name, cms_module_listit2_fielddef.alias, cms_module_listit2_fielddef.type, cms_module_listit2_fieldval.value FROM cms_module_listit2_fielddef INNER JOIN cms_module_listit2_fieldval ON cms_module_listit2_fieldval.fielddef_id = cms_module_listit2_fielddef.fielddef_id WHERE cms_module_listit2_fieldval.item_id = ' . $item->item_id . ' ORDER BY cms_module_listit2_fielddef.position';
$dqry = mysql_query($dsql);
$dt = array(
'Description' => ' ',
'Fiche-technique' => ' ',
'CE' => ' ',
'FDS' => ' '
);
while ($do = mysql_fetch_object($dqry)){
switch ($do->alias){
case 'desc':
$key = 'Description';
break;
case 'TF':
$key = 'Fiche-technique';
break;
case 'CE':
$key = 'CE';
break;
case 'fds':
$key = 'FDS';
break;
}
$dt[$key] = $do->value;
}
echo '<div class="item">';
echo '<div class="details item-title">' . $item->title . '</div>';
foreach ($dt as $cla => $opt){
if ($cla == 'Description'){
$cla .= '';
}else{
if ($opt != ' '){
$opt = '<a href="' . $opt . '" target="_blank">' . str_replace('-', ' ', $cla) . '</a>';
}
$cla .= ' textbox';
}
echo '<div class="details ' . $cla . '">' . $opt . '</div>';
}
echo '</div>';
}
echo '</div>';
}
}
}else{
// only if we find products
$psql = 'SELECT item_id, title, alias FROM `cms_module_listit2_item` WHERE category_id = ' . $p_cat->category_id . ' AND active = 1 ORDER BY position';
$pqry = mysql_query($psql);
if (mysql_num_rows($pqry)){
echo '<a name="#cat' . $p_cat->category_id . '"></a>';
echo '<h2 class="category-title">' . $p_cat->category_name . '</h2>';
echo '<div class="items">';
while ($item = mysql_fetch_object($pqry)){
// get the details
$dsql = 'SELECT cms_module_listit2_fielddef.name, cms_module_listit2_fielddef.alias, cms_module_listit2_fielddef.type, cms_module_listit2_fieldval.value FROM cms_module_listit2_fielddef INNER JOIN cms_module_listit2_fieldval ON cms_module_listit2_fieldval.fielddef_id = cms_module_listit2_fielddef.fielddef_id WHERE cms_module_listit2_fieldval.item_id = ' . $item->item_id . ' ORDER BY cms_module_listit2_fielddef.position';
$dqry = mysql_query($dsql);
$dt = array(
'Description' => ' ',
'Fiche-technique' => ' ',
'CE' => ' ',
'FDS' => ' '
);
while ($do = mysql_fetch_object($dqry)){
switch ($do->alias){
case 'desc':
$key = 'Description';
break;
case 'TF':
$key = 'Fiche-technique';
break;
case 'CE':
$key = 'CE';
break;
case 'fds':
$key = 'FDS';
break;
}
$dt[$key] = $do->value;
}
echo '<div class="item">';
echo '<div class="details item-title">' . $item->title . '</div>';
foreach ($dt as $cla => $opt){
if ($cla == 'Description'){
$cla .= '';
}else{
if ($opt != ' '){
$opt = '<a href="' . $opt . '" target="_blank">' . str_replace('-', ' ', $cla) . '</a>';
}
$cla .= ' textbox';
}
echo '<div class="details ' . $cla . '">' . $opt . '</div>';
}
echo '</div>';
}
echo '</div>';
}
}
}