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

[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>
#2

[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;
#3

[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

#4

[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
#5

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

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

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

[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;
#9

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

Thank you guys,

Bill




Theme © iAndrew 2016 - Forum software by © MyBB