CodeIgniter Forums

Full Version: Views and PHP Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]bagwaa[/eluser]
Greetings All,

I am quite new to CI but have been using PHP for many years, I decided to learn CI because I wanted a decent framework and all the others I just didn't quite like enough. I have also been using Ruby on Rails which I do like, but my main forte is PHP so CI is perfect.

As I am new to MVC frameworks my question is regarding views, I have created an application recently for a client who wants an online store to deliver PDFs, simple enough and with CI its actually fun to-do! but one thing that I wasn't comfortable with was my views. Now this may well be either me doing it wrong, or trying to stick to the MVC concept to much, but I had a set of data which I wanted to draw out on a page which sits in a table. The code I wrote basically creates the table and then has a foreach loop embedded into the html so that the data can be placed within <td></td> tags, I then use the % operator so that every 4 items, it starts a new row <tr></tr>.

Using foreach loops in a view seems like a really crude way of doing it in CI, is there a way to build these tables up or am I just thinking about the MVC structure too much?

Thanks,

Richard

El Forum

[eluser]felyx[/eluser]
Well here is how I create table with php in a view:

Code:
<table border="0" cellspacing="0" cellpadding="0">
<tr>
  <td>t1</td>
  <td>t2</td>
</tr>

&lt;?php foreach($itemarray as $item): ?&gt;

<tr>
  <td>&lt;?php echo $item['t1']; ?&gt;</td>
  <td>&lt;?php echo $item['t2']; ?&gt;</td>
</tr>

&lt;?php endforeach; ?&gt;

</table>

It seems fine to me but maybe someone can show you a better approach. However I think you should not complicate it more if you can settle with this solution, its just a simple easy to understand code in a view even a designer can handle it I think.

El Forum

[eluser]attos[/eluser]
That's the beauty of CI. You have the freedom to choose what fits you best Smile

El Forum

[eluser]bagwaa[/eluser]
Your code looks way neater than mine because I am doing a few other things in the code, basically its a page of book covers, and when there are 4 books on one row I want it to-do </tr><tr> to start on the next row.

<table cellpadding=20 border=0>
<tr>
&lt;? $i = 1; ?&gt;
&lt;? foreach ($documents as $document): ?&gt;
<td>
&lt;?= img('www/img/'.$document->image) ?&gt;
<center><font size=1>
&lt;?=anchor('store/viewdocument/'.$document->id, '[view]'); ?&gt;
&lt;?=anchor('store/addtocart/'.$document->id, '[add to cart]'); ?&gt;
</font></center>
</td>
&lt;? if (!($i % 4)) { ?&gt;
</tr><tr>
&lt;? } ?&gt;
&lt;? $i++; ?&gt;
&lt;? endforeach; ?&gt;
</tr>
</table>

El Forum

[eluser]felyx[/eluser]
I know what you mean and I do not have a better solution for you at the moment. I would be interested in a better solution to this matter however if anyone has one.

El Forum

[eluser]plainas[/eluser]
From what I red out there in the forums, most of the people simple jam loops into the views. I personally find myself thinking how 'unMVC' those loops are.

There are neater solutions in my opinion.

Write a helper function to render the table in a CSS friendly way, then push it to the view through a variable. IMHO, the layout of a table is not part of the view, its looks are, but that you can control by simply using CSS.

Other solution is writing a view only to render the table, then fetch that view output and pass it to another view. I guess this is the best solution.

El Forum

[eluser]felyx[/eluser]
[quote author="plainas" date="1233560160"]From what I red out there in the forums, most of the people simple jam loops into the views. I personally find myself thinking how 'unMVC' those loops are.

There are neater solutions in my opinion.

Write a helper function to render the table in a CSS friendly way, then push it to the view through a variable. IMHO, the layout of a table is not part of the view, its looks are, but that you can control by simply using CSS.

Other solution is writing a view only to render the table, then fetch that view output and pass it to another view. I guess this is the best solution.[/quote]

Why make it more complicated when you do not need to? That is my only question Smile

El Forum

[eluser]plainas[/eluser]
That is _not_ more complicated than I need. Most of the times I need strict separation between business logic and presentation logic. I guess the answer to your question is:

"For code tidyness' sake."

If you think about it, that is the main porpuse of MVC design.

If you want to redesign the looks of a site, I don't think having loops and whatnot in the views is very friendly. Suposely they should be possible to edit by a person with zero PHP knowledge.

El Forum

[eluser]felyx[/eluser]
[quote author="plainas" date="1233564581"]That is _not_ more complicated than I need. Most of the times I need strict separation between business logic and presentation logic. I guess the answer to your question is:

"For code tidyness' sake."

If you think about it, that is the main porpuse of MVC design.

If you want to redesign the looks of a site, I don't think having loops and whatnot in the views is very friendly. Suposely they should be possible to edit by a person with zero PHP knowledge.[/quote]

Agreed but since I am my own webdesigner I can handle it. However with a separate designer it might be a better solution working the way you are suggesting to, that is true.

El Forum

[eluser]Colin Williams[/eluser]
I think there are distinctions to be made between "application logic" and "display logic." Keeping roles clearly separated, the controller should do little "display logic", because this takes power away from the views.

So what's the solution, do both! Let's say I have a blog app and there are areas where one expects to see a list of blog posts. Where do you loop. You can send an array to the view and have it loop, or you can loop from the controller, calling a view on each iteration, and send the result to the view. Or, you can do both. Pass the array and the rendered iterative result to the view. The "designer" can either template each iteration in the view that is called multiple times, or get "fancy" and iterate from the "parent" view. Best of both worlds.
Pages: 1 2