#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Move code from view to controller


    Hi;

    How can I simply move this from my view to controller?

    Thanks
    PHP Code:
    $message_div_open FALSE;
            foreach(
    $messages_array as $val=>$row)
            {
                if(
    $row['your_reply']==1)
                {
                    
    $bg "E1F0F0";
                    
    $border "439393";
                    
    $color "#439393";
                    
    $class "your_reply_box";
                }
                else
                {
                    
    $bg "";
                    
    $border "";
                    
    $color "";
                    
    $class "others_reply_box";
                }
                echo 
    '<!-- Message box -->
                                <div class="alert alert-warning alert-dismissable '
    .$class.'" style = "background-color:#'.$bg.'; border:1px solid #'.$border.'">
                                      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  '
    ;
                
    //echo $row['message_id'];
                
    if($row['your_reply']==1)
                {
                    echo 
    '<em style = "color:'.$color.'"><small>You replied to <strong>'.html_escape(ucfirst($row['sender'])).'</strong></small></em>&nbsp;';
                    echo 
    '<br /><small style = "color:#9FA9A9;">'.$row['date_added'].', '.$row['time_added'].'</small>&nbsp;&nbsp;<br />';
                    echo 
    '<span style = \'font-style:italic; color:#444\'>"'.html_escape($row['content']).'"</span><br /><br />';
                    echo 
    '<div style = "clear:both"></div>';
                }
                else
                {
                    echo 
    "<em><small><strong>".html_escape(ucfirst($row['sender']))."</strong> ".$row['sex'].", ".$row['age']."</small></em>&nbsp;";
                    
    $unread ='';
                    if(
    $row['clicked']!='y')
                    {
                        
    $unread ' ;font-weight:bold; ';
                        echo 
    "</strong>&nbsp;<small style = \"color:#B94A48;\">(New)</small>";
                    }
                    echo 
    "<br /><small style = \"color:#cccccc;\">".$row['date_added'].", ".$row['time_added']."</small>&nbsp;&nbsp;
                        
                                  <br />"
    ;
                    if(
    $need_membership==FALSE)
                    {
                        echo 
    '<span style = \'font-style:italic; color:#444'.$unread.'\'>"'.html_escape($row['content'])."</span><br /><br />";
                        echo 
    "<h6>Write a reply:</h6>
                        <div id = \"reply_message_div_"
    .$row['message_id']."\">
                            <form id = \""
    .$row['message_id']."\" name=\"".$row['message_id']."\">
                                <textarea class=\"form-control\" rows=\"3\" name = \"reply\"></textarea>
                                <br />
                                <button type=\"button\" class=\"btn btn-info btn-xs \" style = \"border-radius: 3px;
                                font-size: 12px; line-height: 1.5; padding: 1px 5px;\" onclick=\"reply_message_function('"
    .$row['message_id']."','".$row['sender_ID']."','".$row['receiver_ID']."')\">Send Repy</strong></button>&nbsp;&nbsp;
                            </form>
                       </div>
                        <div style = \"clear:both\"></div>"
    ;
                        if(
    $row['your_reply']!=&& $row['clicked']!='y')
                        {
                                
                            echo 
    '<span id = "message_read_<'.$row['message_id'].'"><button type="button" class="btn btn-default btn-xs pull-right" style = "border-radius: 3px;
                                                font-size: 12px;
                                                line-height: 1.5;
                                                padding: 1px 5px;" onclick="message_read(\''
    .$row['message_id'].'\')">Mark as read</button></span><div style="clear:both;"></div>';
                        }
                    }
                    else
                    {
                        echo 
    '<br /><div class="alert alert-danger">
                                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                                    <strong>Oh no! </strong>You need to get membership in order to read your messages and reply to them. Please select a membership type from list below.<br />
                                                    </div>'
    ;
                        echo 
    form_open(base_url('process')).'
                                                        <input type="hidden" name="itemname" value="1 year membership for $15" />
                                                        <input type="hidden" name="itemprice" value="15" />
                                                        <input type="hidden" name="itemnumber" value="1" />
                                                        <input type="hidden" name="itemQty" value="1" />
                                                        <input type = "submit" value="1 year membership for $15" class="btn btn-success">
                                                        </form>
                                                        &nbsp;
                                                        '
    .form_open(base_url('process')).'
                                                        <input type="hidden" name="itemname" value="6 months membership for $10" />
                                                        <input type="hidden" name="itemprice" value="10" />
                                                        <input type="hidden" name="itemnumber" value="2" />
                                                        <input type="hidden" name="itemQty" value="1" />
                                                        <input type = "submit" value="6 months membership for $10" class="btn btn-info">
                                                        </form>
                                                        <br /><br />'
    ;
                    }
                        
                } 
    //end if your reply
                
    echo "</div>";
            } 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    What do you mean by “move to controller”? And why would you do that? This is clearly presentation, not application logic.

    Sounds more like you're not using a proper template engine ...
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    What do you mean by “move to controller”? And why would you do that? This is clearly presentation, not application logic.

    Sounds more like you're not using a proper template engine ...
    Hi;

    We don't use template engines. Don't want to discuss that if you don't mind.

    The reason I want to move that is the "designers" in this company don't understand php. They only work with views. They can just handle small php variables <?php echo $form;?>.

    Thanks
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    So what do you wanna do? Give them the fully rendered list of messages? How will they edit the HTML of this list then?

    This is the problem of the templates. I'm not saying that you need an external template engine, but if you (ab)use PHP for the templates, you need to use it correctly.

    Instead of writing PHP code with HTML in it, you need to write HTML with occasional PHP tags in it:

    php Code:
    <?php
     
    $title = 'some_title';
    $some_list_elements = array('foo', 'bar');
     
    ?>
    <!DOCTYPE HTML>
    <html>
        <head>
            <title><?= html_escape($title) ?></title>
            <meta charset="utf-8">
        </head>
        <body>
             <ul>
                <?php foreach ($some_list_elements as $some_element): ?>
                <li><?= html_escape($some_element) ?></li>
                <?php endforeach; ?>
             </ul>
        </body>
    </html>

    I'm sure the designers will understand that. Even Wordpress designers do.

    For future projects, you should definitely look into a real template engine like Twig:

    • less bloated with code, thus better readable and easier to understand for non-programmers
    • convenient features like template inheritance
    • security! less risk of code injection, less risk of the designers forgetting to escape their stuff (Twig has auto-escaping)
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Thanks Jacques1.

    Would you change my code much if you had to redo it? Is there any major problems with it that may come up?

IMN logo majestic logo threadwatch logo seochat tools logo