Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:59:36+00:00 2026-05-21T17:59:36+00:00

We have two models. Ebooks HABTM Tags, where tags follows the tree behavior. For

  • 0

We have two models. Ebooks HABTM Tags, where tags follows the tree behavior.

For each tag we need two numbers. First, the number of ebooks associated to the tag, and secondly the number of ebooks associated to the tag + the number of associated ebooks for each descendant.

How can we get the tags with these numbers in an array in tree format?

Thank you very much for any help.

Update: There is a datetime parameter Ebook.published which defines when the book is to be counted or not. All the ebooks that have codeEbook.published < NOW() should be counted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:59:37+00:00Added an answer on May 21, 2026 at 5:59 pm

    Actually, I found a simpler solution since I already have build the function that returns the tags tree. I used a query for each tag to get the actual count of that moment. Here is what I ‘ve build. Feel free to use it according your needs.

    In Tag model

    // Returns the number of published ebooks of selected tag
    function count_direct_published($tag_id = 0){
        $temp = $this->query('SELECT count(*) as count FROM ebooks_tags LEFT JOIN ebooks ON ebooks_tags.ebook_id = ebooks.id WHERE ebooks.published < NOW() AND ebooks_tags.tag_id = '.$tag_id);
        return $temp[0][0]['count'];
    }
    
    
    // Returns an array in tree format with $id tag and all his children
    // $id = 0 start from the top (parent_id = null), or, from $id = the top's tag id
    // $limit = boolean (default false)
    // $level = Is the limit of depth applied only if $limit = true
    // $ext = true Means this is the first time the function is called
    // You can run tree_builder(), returns all the tree,
    function tree_builder($id = 0, $limit = false, $level = 1, $ext = 1){
        if($ext == 1){
            $ext = 0;
            $undo = true; 
        }else{
            $undo = false;
        }
    $this->recursive=-1;
    
        $this->contain('EbooksTag');
    
        $var = array();
        $count_all = 0;
        // If limit = too big , exit
        if($limit !== false && $level > $limit){
            return '';
        }
        // Or else, 
        // If $id=0, find all the children
        if($id == 0){
            $tags = $this->find('all',array('conditions'=>array( 'Tag.parent_id IS NULL'), 'order'=>array('Tag.gre')));
        // If $id!=0 && runs internally
        }elseif($id != 0 && !$undo ){
            $tags = $this->find('all',array('conditions'=>array( 'Tag.parent_id'=>$id ), 'order'=>array('Tag.gre')));
        }
        // If $id!=0 && is called from outside 
        elseif($id != 0 && $undo){
            $tags = $this->find('all',array('conditions'=>array( 'Tag.id'=>$id )));
    
        }
    
        foreach($tags as $key => $tag){
            $var[] = $tag;
            $next = $this->tree_builder($tag['Tag']['id'], $limit, $level+1, $ext);
            end($var); // move the internal pointer to the end of the array
            $last_key = key($var); // fetches the key of the element pointed to by the internal pointer
            $var[$last_key]['children'] = $next['var'];
            $counter_direct = $this->count_direct_published($id);
            $var[$last_key]['Tag']['count_all'] = $next['count_all']+$counter_direct;
            $count_all += $var[$last_key]['Tag']['count_all'];
    
        }
    
        if( $undo )
        {
            return $var;
        }else{
            return array('count_all'=> $count_all, 'var' => $var);
        }
    
    }
    

    In tags_controller.php

    $this->set('tags', $this->Tag->tree_builder());
    

    In the view

    <?php foreach($tags as $tag){?>
        <?php // Ο Γονέας σε dropdown box ?>
        <div class="main-categ">
        <?php echo $tag['Tag']['gre']; ?>
        <?php echo $html->image('layout/arrows.png', array('alt'=> "Expand")); ?>
        </div>
    
    
    
        <div class="collapse"> 
            <?php // Τα στοιχεία του γονέα ?>
            <div class="tag-1">
                <span class="tag-1">
                    <?php // Αν ?>
                    <?php if($tag['Tag']['count_direct']>0){
                        // Display link
                         echo $html->link($tag['Tag']['gre'],array('action'=>'view',$tag['Tag']['id']));
                         echo ' ('.$tag['Tag']['count_direct'].')';
                    }else{
                        // Display text
                         echo $tag['Tag']['gre'];
    
                    }  ?>
                </span> 
                &nbsp;&nbsp;&nbsp;&nbsp;
                <?php echo $html->link( 'view all' ,array('action'=>'view_all',$tag['Tag']['id'])); ?>
                 (<?php echo $tag['Tag']['count_all']; ?>)
            </div>  
    
            <?php // Για κάθε πρώτο παιδί ?>
            <?php foreach($tag['children'] as $tag_1){ ?>
            <div>
                <span class="tag-2">
                    <?php if($tag_1['Tag']['count_direct']>0){
                        // Display link
                         echo $html->link($tag_1['Tag']['gre'],array('action'=>'view',$tag_1['Tag']['id']));
                         echo ' ('.$tag_1['Tag']['count_direct'].')';
                    }else{
                        // Display text
                         echo $tag_1['Tag']['gre'];
    
                    }  ?>
                </span>
                &nbsp;&nbsp;&nbsp;&nbsp; 
                <?php echo $html->link( 'view all' ,array('action'=>'view_all',$tag_1['Tag']['id'])); ?>
                 (<?php echo $tag_1['Tag']['count_all']; ?>)
    
                    <?php // Τα δεύτερα παιδιά ?>
                    <?php $i=0; ?>
                    <?php foreach($tag_1['children'] as $tag_2){ ?>
                        <?php if($i==0){ echo '<ul class="split">'; $i++; } ?>
                        <li>
                        <?php if($tag_2['Tag']['count_direct']>0){
                                // Display link
                                 echo $html->link($tag_2['Tag']['gre'],array('action'=>'view',$tag_2['Tag']['id']));
                                 echo ' ('.$tag_2['Tag']['count_direct'].')';
                            }else{
                                // Display text
                                 echo $tag_2['Tag']['gre'];
    
                            }  ?>                       
                        </li>
                    <?php } ?>
                    <?php if($i==1) echo '</ul>'; ?>
    
                <div class="clear"></div>       
            </div>
    
            <?php } ?>      
    
        </div>
    

    Perhaps its not the best solution but it works. Hope that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models as follows: class Tag(models.Model): # ... class Paragraph(models.Model): tags =
I have two Models, Programme and Event, a programme has many events. I need
I have two models User and Category that have a HABTM association. I would
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models, TreeNode and User. Each user has_one TreeNode, which is the
I have two models, associated with a HABTM (actually using has_many :through on both
I have two models, Users and Groups. Each group can have many users and
I have two models that are related to each other. For a contrived example,
I have two models, Tweets and TweetsLocations, associated to each other with: class Tweets
I have two models of concern, Order and Kit; each order has_one :kit Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.