Displaying tags in modules
Hi! I'd like to learn how to display tags only of the category selected when creating an entry in a module.
I have a module that lets the user add merchandise. I have created a merchandise category. It has subcategories like apparel, electronics, etc. When creating an entry, the module should list these subcategories. Each subcategory has multiple tags. I'd like the module to display tags only of that subcategory. If the user selects the apparel subcategory, the tags that would be listed would be only of those from the apparel subcategory.
Is there a way to do this?
Comments
http://docs.getfuelcms.com/general/forms#dependent
$fields['tags'] = array('type' => 'dependent', 'depends_on' => 'category_id', 'url' => fuel_url('modules/fuel/tags/ajax/options'));
fuel_url('tags/ajax/options')
In my merchandise module, I have two dummy data. Data A is categorized under Apparel and under the tag Men. Data B is under category Apparel and tag Women. Data A was created before I implemented the code above. Data B is created after I implemented the code above. I have a view which retrieves items under a certain category or tag which searches based on the uri. If I go to the url "www.site.com/merchandise/apparel/men" it will show Data A. But if I go to the women's section (url would then be "www.site.com/merchandise/apparel/women" Data B does not show at all. Even if I just go to the Apparel category, Data A will show but Data B will not. If I edit Data A, the changes I made won't take effect at all, so if I change Data A to have the Women tag, it will still come out having the Men tag.
Here is my code:
$slug = uri_segment(4); if($slug) { $product = fuel_model('merchandises', array('where' => array('slug' => $slug))); if(!empty($product)) { foreach($product as $p) { echo '<div id="product-info">'; echo '<div id="product-image">'; echo '<img src="' . img_path('merchandise/' . $p->image) . '"></img>'; echo '</div>'; // right side echo '<div id="product-details">'; // product name echo '<div id="product-name">' . $p->name . '</div>'; // price echo '<div id="product-price">' . $p->price . '</div>'; // size echo '<div id="product-name">' . $p->size . '</div>'; // color echo '<div id="product-name">' . $p->color . '</div>'; echo '</div>'; echo '</div>'; echo '<div id="product-description">'; echo $p->description; echo '</div>'; } } else { redirect_404(); // echo "no product"; } } else { // subcategory page $slug = uri_segment(3); if($slug) { $tags = fuel_model('tags', array('where' => array('slug' => $slug))); if(!empty($tags)) { foreach($tags as $tag) { foreach($tag->merchandises as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; // info echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } } else { redirect_404(); // echo "no subcategory"; } } else { // category page $slug = uri_segment(2); if($slug) { $category = fuel_model('categories', array('where' => array('slug' => $slug))); if(!empty($category)) { foreach($category as $cat) { $tags = fuel_model('tags', array('where' => array('category_id' => $cat->id))); foreach($tags as $tag) { foreach($tag->merchandises as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } } } else { redirect_404(); // echo "no category"; } } else { // main merchandise page $all = fuel_model('merchandises'); if(!empty($all)) { foreach($all as $i) { echo '<div class="item-box">'; // image echo '<div class="item-image">'; echo '<img src="' . img_path('merchandise/') . $i->image . '"></img>'; echo '</div>'; // info echo '<div class="item-text">'; echo $i->name; echo '</br>'; echo 'Php ' . $i->price; echo '</br>'; echo $i->color; echo '</div>'; echo '</div>'; } } else { } } } }
$tags = fuel_model('tags', array('where' => array('slug' => $slug)));
Try running and see what it returns:
CI()->merchandises_model->debug_query();
$tags = fuel_model('tags', array('where' => array('slug' => $slug))); $tags = fuel_model('tags', array('where' => array('slug' => $slug))); CI()->merchandises_model->debug_query();
foreach($tag->merchandises as $i) { CI()->merchandises_model->debug_query(); ....