Last active
October 2, 2015 17:47
-
-
Save litzinger/2287143 to your computer and use it in GitHub Desktop.
Example of how to create a plugin tag with nested loops and prefixed variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Usage | |
{exp:kaplan:categories | |
products="55,56,52,53,54,65" | |
state="{url:segment_2_category_id}" | |
variable_prefix="category:"} | |
{if no_results}{/if} | |
{category_list} | |
<a href="{site_url}{segment_1}/{url_title}/{category:cat_url_title}">{category:cat_name}</a> | |
{/category_list} | |
{/exp:kaplan:categories} | |
*/ | |
function categories() | |
{ | |
$product_category_ids = $this->EE->TMPL->fetch_param('products', '55,56,52,53,54,65'); | |
$state_category_id = $this->EE->TMPL->fetch_param('state'); | |
$prefix = $this->EE->TMPL->fetch_param('variable_prefix', ''); | |
if ( ! $state_category_id) | |
{ | |
show_error('{exp:kaplan:categories} requires a state id'); | |
} | |
$sql = 'select cp.entry_id, cp.cat_id, c.cat_name, c.cat_url_title | |
from exp_category_posts as cp | |
join exp_categories as c on cp.cat_id = c.cat_id | |
where cp.cat_id in('. $this->EE->db->escape_str($product_category_ids) .') AND exists | |
(select entry_id from exp_category_posts where cat_id = '. $this->EE->db->escape_str($state_category_id) .' and entry_id = cp.entry_id) | |
group by cat_id'; | |
$qry = $this->EE->db->query($sql); | |
$vars = array(); | |
$categories = array(); | |
$i = 1; | |
$total_results = $qry->num_rows(); | |
if ($total_results == 0) | |
{ | |
return $this->EE->TMPL->no_results(); | |
} | |
foreach ($qry->result_array() as $row) | |
{ | |
$prefixed_row = array(); | |
foreach ($row as $key => $value) | |
{ | |
$prefixed_row[$prefix.$key] = $value; | |
} | |
$prefixed_row[$prefix.'count'] = $i; | |
$prefixed_row[$prefix.'total_results'] = $total_results; | |
$categories[] = $prefixed_row; | |
$i++; | |
} | |
$vars[] = array( | |
'category_list' => $categories, | |
$prefix.'total_results' => $total_results | |
); | |
return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment