CodeIgniter Forums
Grabbing and printing from database into a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Grabbing and printing from database into a view (/thread-28609.html)



Grabbing and printing from database into a view - El Forum - 03-16-2010

[eluser]mingquel[/eluser]
So I don't know if the title really describes the problem, but I had to put something. So the problem we're having is, we have to two databases that we've inner joined but need to print out a certain amount to the view. As it stands now, when the view is loaded, the user sees just the category links. When you view the source, you see the category links as well as an id that corresponds to the category links, but you also see the one hundred and three Terms and Definitions. The issue is, for every category id, it's printing out all one hundred and three Terms/Definitions. How would we go about getting it to print out only the Terms/Definitions that relate to a specific category id? Here is the code for the view:
Code:
<div id="main-content-wrap" class="content">
    <h1>Glossary of Real Estate Terms</h1>
    <div id="glossary">
        <div class="nav-glossary">
            <ul id="internalnav">
                &lt;?php foreach ($blubs as $blub): ?&gt;
                <li><a href="#">&lt;?php echo $blub->gl_categories ?&gt;</a> &middot; </li>
                &lt;?php endforeach; ?&gt;
            </ul>
        </div>
        &lt;?php foreach ($blubs as $blub): ?&gt;
        <div class="section" id="&lt;?php echo $blub->gl_categories ?&gt;">
        <dl>
            &lt;?php foreach ($blobs as $blob): ?&gt;
            
                <dt>&lt;?php echo $blob->Term ?&gt;</dt>
                <dd>&lt;?php echo $blob->Definition ?&gt;</dd>

            &lt;?php endforeach; ?&gt;
            </dl>
            </div> &lt;!-- end section --&gt;&lt;?php endforeach; ?&gt;

    </div>
</div>
<div id="menu-generic">
    &lt;?php $this->load->view('sidebar-generic'); ?&gt;
</div>

Any help would be appreciated. Feel free to ask for more information. Thanks.


Grabbing and printing from database into a view - El Forum - 03-17-2010

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
I would think you'd want to use a conditional inside of the second foreach that tests whether the given term is related to the category.

Something like:

Code:
&lt;?php foreach ($blobs as $blob): ?&gt;
         &lt;?php if($blob->category ==  $blub->gl_categories):?&gt;  
                <dt>&lt;?php echo $blob->Term ?&gt;</dt>
                <dd>&lt;?php echo $blob->Definition ?&gt;</dd>
        &lt;?php endif;?&gt;
&lt;?php endforeach; ?&gt;


It might be better to address this issue from the model side, and actually insert the array of associated terms and definitions into each category array or object.


Grabbing and printing from database into a view - El Forum - 03-17-2010

[eluser]mingquel[/eluser]
The funny thing is, I tried the if statement before I posted to the forum. My mistake was not using the colon and endif closing tag in mine. Thanks for teaching me something new. My original programming experience was with Java, so that's my excuse. And thanks for the suggestion. It actually works. You just saved us much hair pulling. Now I just have to figure out my other problem. Thanks again. If I need more help, I may ask later.


Grabbing and printing from database into a view - El Forum - 03-18-2010

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
Glad it worked for you. This is, of course, not the usual syntax for PHP, but an alternative syntax that proves very useful for adding logic in views. It makes a bit more sense to designers as they're working on markup. I wouldn't recommend using it in places where it's not intermingled with markup.