CodeIgniter Forums
Count fetch results in view file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Count fetch results in view file (/showthread.php?tid=60463)



Count fetch results in view file - El Forum - 04-01-2014

[eluser]comyou[/eluser]
Hello everyone,

I currently have a page which displays all Teams and Team Members from MySQL results.

Im trying to make it so that it doesn't show a particular team if there are no members in that team.

The code I have at the moment is:
Code:
<div id="content-wrapper">
<div id="content">
            <h1>Dynasty Roster</h1>
            
&lt;?php foreach(fetch('team_mdl') as $team) : ?&gt;
            <div class='widget team'>
            <h ><span >&lt;?= str_replace('_', ' ', $team->platform) ?&gt;</span>&lt;?= $team->name; ?&gt;</h2>
            <ul>
&lt;?php foreach(fetch('team_member_mdl', 'team='.$team-&gt;id.'') as $member) : ?&gt;
                
                <li>
    &lt;?= $member->firstname ?&gt;"
    &lt;?= $member->gamertag ?&gt;"
    &lt;?= $member->lastname ?&gt;-
    &lt;?= $team->name; ?&gt;
                </li>
&lt;?php endforeach; ?&gt;
            </ul>
            </div>

&lt;?php endforeach; ?&gt;
</div>
</div>


I imagine I'd need to do something like
Code:
<div id="content-wrapper">
<div id="content">
            <h1>Dynasty Roster</h1>
            
&lt;?php foreach(fetch('team_mdl') as $team) : ?&gt;
            <div class='widget team'>
            <h ><span >&lt;?= str_replace('_', ' ', $team->platform) ?&gt;</span>&lt;?= $team->name; ?&gt;</h2>
            <ul>
&lt;?php foreach(fetch('team_member_mdl', 'team='.$team-&gt;id.'') as $member) : ?&gt;
                
                &lt;?php if(///////FETCH_RESULTS /////////!= 0): ?&gt;
                <li>
    &lt;?= $member->firstname ?&gt;"
    &lt;?= $member->gamertag ?&gt;"
    &lt;?= $member->lastname ?&gt;-
    &lt;?= $team->name; ?&gt;
                </li>
                &lt;?php endif; ?&gt;
&lt;?php endforeach; ?&gt;
            </ul>
            </div>

&lt;?php endforeach; ?&gt;
</div>
</div>

but Im not sure how to go about doing it? :S


Count fetch results in view file - El Forum - 04-01-2014

[eluser]comyou[/eluser]
I've just added a little counter to check, is there a more efficient way?
Code:
<div id="content-wrapper">
<div id="content">
            <h1>Dynasty Roster</h1>
</div>
        <div id="content"  0 !important; margin-top: -20px;">
&lt;?php foreach(fetch('team_mdl') as $team) : $i = 0;?&gt;
&lt;?php foreach(fetch('team_member_mdl', 'team='.$team-&gt;id.'') as $member) :
    $i += 1;
    endforeach;
    if ($i > 0):
        ?&gt;
            <div class='widget team'>
            <h ><span >&lt;?= str_replace('_', ' ', $team->platform) ?&gt;</span>&lt;?= $team->name; ?&gt;</h2>
            <ul class="team-members">
&lt;?php foreach(fetch('team_member_mdl', 'team='.$team-&gt;id.'') as $member) : ?&gt;
                
                <li>
    &lt;?= $member->firstname ?&gt;"
    &lt;?= $member->gamertag ?&gt;"
    &lt;?= $member->lastname ?&gt;
                </li>
&lt;?php endforeach; ?&gt;
            </ul>
            </div>
            &lt;?php
    endif;
?&gt;
            

&lt;?php endforeach; ?&gt;
        </div>
</div>