Welcome Guest, Not a member yet? Register   Sign In
My views are too complex, there is a way to avoid this?
#1

[eluser]Sinclair[/eluser]
Hi,

I'am new to codeigniter, and I I'am with some trouble in organizing code and I think my Views are too complex.

How can I do better than this?

Example View:
Code:
<?php

$prevCidade = '';
$primVezTab = '0';
$primVezHrl = '0';

foreach ($result as $row){

    if ($prevCidade != $row->n_cidade){
        
        if ($primVezTab != '0') {
            echo "</tr></table><br />";
        }
        
        if ($primVezHrl == '0') {
            echo "<br />";
            echo "&nbsp;&nbsp;&nbsp;<span class=\"style4\">$row->n_cidade</span><br /><br />";
            echo "<table><tr>";
            }
        else {
            echo "<hr size=\"1\" noshade />&nbsp;&nbsp;&nbsp;<span class=\"style4\">$row->n_cidade</span><br /><br />";
            echo "<table><tr>";
            }
            
        # Assignar novos valores as variaveis(dizer que já passaram por aqui uma vez)
        $primVezTab = '1';
        $primVezHrl = '1';
        # Assignar a zero a variável que vai fazer as quebras na tabelas
        $i = 0;
        
        } # End ($prevCidade != $row->n_cidade)    
        
    # Para fazer a quebra na tabela    
    if ($i >= 4)
    {
        echo "    </tr>
                    <tr>
                      <td height=\"15\">&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>";
        $i = 0;
    }
        
        
    echo "<td>&nbsp;</td>";
    echo "<td><A >id_anuncio \"><img >id_anuncio/$row->n_ficheiro\" title=\"$row->n_anuncio\" alt=\"$row->n_anuncio\" width=\"221\" height=\"306\" /></a></td>";
            
        # Assignar novo valor a' variavel
        $prevCidade = $row->n_cidade;
        # Incrementar 1 a' variavel
        $i = $i + 1;
        
            
    } # End foreach
echo "</tr></table><br />";
?&gt;

Can I see examples of views on the web, what will help me to do better code using MVC?

Sorry my bad english.

Best Regards,
#2

[eluser]JHackamack[/eluser]
In relation to projects that I've worked on this view is relatively simple. It looks like you're using of table rows that are blank, sometimes I split those with divs, but as I said in the beginning, this looks like a pretty simple view.
#3

[eluser]OES[/eluser]
I agree with JHackmack BUT !.

Where I can I try not to use echo with a long block of code so what you could do is to use the other type of php if/foreach like this.

Code:
&lt;?php if (condition): ?&gt;
  // HTML  
&lt;?php endif; ?&gt;


&lt;?php foreach ($variable as $key => $value): ?&gt;
  // HTML    
&lt;?php endforeach ?&gt;


So with foreach for example with a table this looks alot better in the template.

Code:
<table width"100%" id="mytable">
<tr>
  <th>Title</th><th>Name</th>
</tr>
&lt;?php foreach ($variable as $key => $value): ?&gt;
  <tr>
    <td>&lt;?php echo $title; ?&gt;</td><td>&lt;?php echo $name; ?&gt;</td>
  </tr>    
&lt;?php endforeach ?&gt;
</table>

Another way would be to use Smarty as your template parser which looks even better again.

Hope this helps a little
#4

[eluser]jedd[/eluser]
In contrast, I really don't like jumping in and out of PHP within my HTML - I think it is harder to read.

There's some simple stuff you could do. For example, rather than this:

[quote author="Sinclair" date="1263779653"]
Code:
// original code
        if ($primVezHrl == '0') {
            echo "<br />";
            echo "&nbsp;&nbsp;&nbsp;<span class=\"style4\">$row->n_cidade</span><br /><br />";
            echo "<table><tr>";
            }
        else {
            echo "<hr size=\"1\" noshade />&nbsp;&nbsp;&nbsp;<span class=\"style4\">$row->n_cidade</span><br /><br />";
            echo "<table><tr>";
            }
[/quote]

... I'd pull the table/tr out (it's common) and combine the first two lines in the first block - which means you get the construct down to 4 lines for the if/else (you can forego the { } ).

For neatness, I also like to scatter \n's throughout my compound echo statements, so that the final HTML reads more easily - it fits my overly-neat preferences, but actually has some genuine benefit when you try to match up missing tags later by looking through your page-source.

Similarly your code like this:
Code:
echo "    </tr>
                    <tr>
                      <td height=\"15\">&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>";
        $i = 0;

I'd definitely not do one big echo - instead (if it's outside an if block) I'd drop to HTML, but if it's part of a PHP conditional then I'd at least do a simple loop in there to dump out 8 lots of "<td>&nbsp;</td>\n" lines.

I think there's quite a few examples of code - check the Tutorials page in the wiki - and there's a few pages that list demonstration sites (Bamboo Invoice is the main one, but more recently the music one that I can never remember the name of. Check the [url="/wiki/Applications_Using_Code_Igniter/"]Applications Using Code Igniter[/url] page in the wik for a few more hints.

EDIT: Unravel the music - that's the other big one. A huge site, though not sure how instructive it should be given the authors' resistance to writing comments in their code.
#5

[eluser]elambiguo[/eluser]
I ask my self.... 'What is the best?... HTML+PHP or PHP+HTML.........?'


I see various sources and i can't decide ...... but the line winner is HTML+PHP......
#6

[eluser]nerdburn[/eluser]
Personally, I think OES's example is the cleanest, sticking with MVC doctrine, as the View should generally be the domain of the designer. It's easier for designers to mess around if the HTML is all in clearly identifiable sections.
#7

[eluser]jamziee[/eluser]
I think HTML + PHP, also have you thought about using a libary to move the functions out of the view, this would allow the view to be used for html and only printing out php values insted of processing from with in the view files




Theme © iAndrew 2016 - Forum software by © MyBB