Welcome Guest, Not a member yet? Register   Sign In
in the view, php embedded into html or php echo the whole html?
#1

[eluser]searain[/eluser]
In non MVC programming, instead of using echo to create the whole table like the code below. I used to use html to create the table, and embed
Code:
<?php echo ; ?>
into the html code which is easier to use WYSIWYG editor like dreamweaver etc. and it is easier for non programmer designer if I have to work with one of them.


Code:
if (count($article_categories)){
    echo "<table border='1' cellspacing='0' cellpadding='3' width='400'>\n";
    echo "<tr valign='top'>\n";
    echo "<th>ID</th>\n<th>Name</th><th>Status</th><th>Actions</th>\n";
    echo "</tr>\n";
    foreach ($article_categories as $key => $list){
        echo "<tr valign='top'>\n";
        echo "<td>".$list['id']."</td>\n";
        echo "<td>".$list['name']."</td>\n";
        echo "<td align='center'>".$list['status']."</td>\n";
        echo "<td align='center'>";
        echo anchor('admin/article_categories/edit/'.$list['id'],'edit');
        echo " | ";
        echo anchor('admin/article_categories/delete/'.$list['id'],'delete');
        echo "</td>\n";
        echo "</tr>\n";
    }
    echo "</table>";
}

What approach do you use? html and embed php into it, or use php to echo whole html? I still think if I can use "html and embed php into it", I would prefer to use it, even in CI coding.
#2

[eluser]xwero[/eluser]
what gives you the idea you need to echo all strings in the views?
#3

[eluser]searain[/eluser]
Just wondering why people do that. CI or not, I never saw the advantages of echo all strings to create html page, from day 1 when I get to web programming (asp then). But I saw many experts do that. Such as in many book examples, many authors do that.

I always prefer like this
Code:
&lt;?php
if (count($article_categories)){
?&gt;
    <table border='1' cellspacing='0' cellpadding='3' width='400'>
        <tr valign='top'>
            <th>ID</th><th>Name</th><th>Status</th><th>Actions</th>
        </tr>
&lt;?php
    foreach ($article_categories as $key => $list)
    {
?&gt;
        <tr valign='top'>
            <td>&lt;?php echo $list['id']; ?&gt;</td>
            <td>&lt;?php echo $list['name']; ?&gt;</td>
            <td align='center'>&lt;?php echo $list['status']; ?&gt;</td>
            <td align='center'>&lt;?php echo  anchor('admin/articles/article_categories/edit/'.$list['id'],'edit'); ?&gt; | &lt;?php echo  anchor('admin/articles/article_categories/delete/'.$list['id'],'delete'); ?&gt;</td>
        </tr>
&lt;?php
    }
?&gt;
    </table>
&lt;?php
}
?&gt;
#4

[eluser]xwero[/eluser]
I like to use the alternative syntax which makes it a bit more readable than curly brackets.

I guess those people just love to type.
#5

[eluser]tonanbarbarian[/eluser]
given that CI is a MVC framework then I feel that there should be minimal PHP code in the views. This means that I would never echo the html but rather embed the php in the html

The reason for this is simple. MVC helps to promote code seperation. Controller seperate from Models seperate from Views. This means that you can have 1 (or more) developer who codes the controllers, another (or more) who codes the models and yet another (or more) who works with the views. If you have the views written as embedded php it is much easier for someone with limited PHP skills but advanced HTML/CSS etc skills to modify the views. If the html is all echoed there is too much of a chance of something going wrong and the resulting view not displaying correctly.

Even if I am building an app that I "think" I will be the only maintainer on I always plan that some day someone else may need to maintain it, so like good documentation, nicely formatted code, including html in views, is essential.
#6

[eluser]John_Betong[/eluser]
&nbsp;
The magic of hindsight Smile
&nbsp;
Vague memories and a quick search gave the following Derek's guidelines:
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB