CodeIgniter Forums
Best practices:building html with CI - 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: Best practices:building html with CI (/thread-53245.html)



Best practices:building html with CI - El Forum - 07-16-2012

[eluser]bill19[/eluser]
Hello everyone,

I am trying to build a dynamic portfolio page with a portion of the page made up a small html template fragment/"subview", each of which I would like to populate from a database. Therefore each subview corresponds to 1 database row . What is the best practice way in CI of looping through, populating and appending these subviews to an html page in codeigniter .

An example of a 'subview' is:

Code:
<h2>$db field 1</h2>
           <p>$db field 2. </p>
          <p><a class="btn" href="#">View details ยป</a></p>



Best practices:building html with CI - El Forum - 07-16-2012

[eluser]Aken[/eluser]
Load the items from your DB into the view, and then loop them.

Code:
&lt;?php foreach ($results as $row): ?&gt;

<h2>&lt;?php echo $row->title; ?&gt;</h2>
<p>&lt;?php echo $row->field; ?&gt;</p>

&lt;?php endforeach; ?&gt;



Best practices:building html with CI - El Forum - 07-17-2012

[eluser]bill19[/eluser]
Thanks Aken , for showing me the code.

Only 1 slight syntax error keeps popping up though. When I use:

Code:
&lt;?php foreach ($results as $row): ?&gt; //// LINE 68

<h2>&lt;?php echo $row->title; ?&gt;</h2>
<p>&lt;?php echo $row->field; ?&gt;</p>

&lt;?php endforeach; ?&gt;

I get the following error:
Quote:Parse error: syntax error, unexpected '<' ... AT LINE 68

I've been searching google, but its not obvious whats wrong here.

Thank you,

Bill




Best practices:building html with CI - El Forum - 07-17-2012

[eluser]PhilTem[/eluser]
Which PHP version are you using? I think older versions (below 5.0) don't support this kind of coding. Otherwise you just need to alter your code to include HTML-tags in your PHP code and not like you do right now include PHP within HTML tags


Best practices:building html with CI - El Forum - 07-17-2012

[eluser]CroNiX[/eluser]
PHP Docs say the alternative control structure syntax should work in PHP 4/5


Best practices:building html with CI - El Forum - 07-17-2012

[eluser]bill19[/eluser]
I'm using php 5.35


Best practices:building html with CI - El Forum - 07-17-2012

[eluser]Aken[/eluser]
You probably have an opening &lt;?php tag already.


Best practices:building html with CI - El Forum - 07-17-2012

[eluser]cideveloper[/eluser]
Check above line 68. did you close the php above?

Code:
&lt;?php
        $data = array ('alpha', 'beta', 'gamma', 'delta', 'epsilon');
//missing closing tag
&lt;?php foreach($data as $alpha):?&gt;
&lt;?php echo $alpha . chr(10);?&gt;
&lt;?php endforeach;?&gt;



Best practices:building html with CI - El Forum - 07-18-2012

[eluser]bill19[/eluser]
That's it!

Thank you guys,

Bill