Tuesday, July 8, 2008

Using a helper to count items in an iterator

today the example is for a site that organizes web links and associates them with tags. In order to count the tags while listing data, we do something like this:

views/Link/Link-List.tmpl

<table>
<tr>
<th>URL</th>
<th>Tags</th>
</tr>

<{ITERATOR links}>
<tr>
<td><{url}></td>
<td>
<{IF (LinkHelper::Count_tags_by_link_id(id) > 0)}>
<{ITERATOR get_tags()}>
<{name}><br />
<{/ITERATOR}>
<{ELSE}>
(none)
<{/IF}>
</td>
</tr>
<{/ITERATOR}>
</table>


include/LinkHelper.class.php

public static function Count_tags_by_link_id( $link_id ) {

FUSE::Require_model( 'Link' );

$link = new Link();
$link->id = $link_id;

return $link->tags->count_all();
}


models/Link.class.php

public function get_tags() {
return $this->tags->fetch_all();
}

No comments: