Welcome Guest, Not a member yet? Register   Sign In
Problem with "good practics"... controller and view =/
#1

[eluser]punchi[/eluser]
Hi!

In the controller class, I have a function who call a model to get a list. There's no problem with that, I pass the list via

Code:
$data['clients'] = $this->client_model->list_clients();
$this->load->view('templates/default', $data);

and in the view...

Code:
foreach ($clients->result() as $row) {...}

Now the problem: I want the table for every new row, that row changes the color. The pars are blank and impar are yellow. I can make a "is_par()" function in the view, and the code is "if (is_par()) then $color = yellow" but that not make sense, because the logic must be in the controller.

How can I keep the good practices? =)

Thanks!
#2

[eluser]skunkbad[/eluser]
If you want to alternate row colors, check this out:

http://www.geekpedia.com/tutorial10_Alte...olors.html
#3

[eluser]John_Betong[/eluser]
 
Take a look in the User Guide for alternator():
Quote:alternator()

Allows two or more items to be alternated between, when cycling through a loop. Example:

for ($i = 0; $i < 10; $i++)
{
echo alternator('string one', 'string two');
}
You can add as many parameters as you want, and with each iteration of your loop the next item will be returned.

for ($i = 0; $i < 10; $i++)
{
echo alternator('one', 'two', 'three', 'four', 'five');
}
Note: To use multiple separate calls to this function simply call the function with no arguments to re-initialize.
&nbsp;
&nbsp;
&nbsp;
#4

[eluser]Colin Williams[/eluser]
Well, you are limiting yourself by only using the database result. Instead, your model could add data to the result, like an is_par parameter, so when it gets to the view, that data is there. And there's nothing wrong with a little bit of logic in the view, especially if it relates to display.
#5

[eluser]punchi[/eluser]
Thank you guys =)

I should keep a strict separation (or most), im doing an application for testing, then take codeigniter to code my final exam in the university (actually i want a system with a framework), and if a teacher ask me for a code and see logic in a view... waaaaaa im death XD

but yeah, i think this kind of logic its acceptable, its just for view purposes
#6

[eluser]überfuzz[/eluser]
I find myself aiming for no logic in view files. The reason being I feel that designers shouldn't be bothered with it. However, if you're going to send stuff that will effect the layout/look of a page you might as well do it in the view file. On good example of this is different colours of rows.

In your case I'd say it's no question about it. Do exactly as the teacher said.
#7

[eluser]skunkbad[/eluser]
[quote author="John_Betong" date="1259418538"]&nbsp;
Take a look in the User Guide for alternator():
Quote:alternator()

Allows two or more items to be alternated between, when cycling through a loop. Example:

for ($i = 0; $i < 10; $i++)
{
echo alternator('string one', 'string two');
}
You can add as many parameters as you want, and with each iteration of your loop the next item will be returned.

for ($i = 0; $i < 10; $i++)
{
echo alternator('one', 'two', 'three', 'four', 'five');
}
Note: To use multiple separate calls to this function simply call the function with no arguments to re-initialize.
&nbsp;
&nbsp;
&nbsp;[/quote]

Wow, learn something new every day. I've been doing it the hard way!




Theme © iAndrew 2016 - Forum software by © MyBB