Welcome Guest, Not a member yet? Register   Sign In
Best practices:building html with CI

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:

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

Load the items from your DB into the view, and then loop them.

&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;

Thanks Aken , for showing me the code.

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

&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,



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

PHP Docs say the alternative control structure syntax should work in PHP 4/5

I'm using php 5.35

You probably have an opening &lt;?php tag already.

Check above line 68. did you close the php above?

        $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;

That's it!

Thank you guys,


Theme © iAndrew 2016 - Forum software by © MyBB