Welcome Guest, Not a member yet? Register   Sign In
How to Call private method from view?
#1

[eluser]saidbakr[/eluser]
Hello,

I may think that calling controllers functions - methods - from view is not recommended behavior, Really I don't know why! However, I need it as a matter of application performance in restricted or limited situation.

My story is saving some users privileges in the database as letters, for example:
Admin will be ABC, Editor will be BC, User will be C and so on.

I'd like to list users data including there privileges and I want to replace the letters with words regarding each one.

The following private function is created:
Code:
function _echo_privileges($str)
        {
            switch ($str)
            {
                case 'ABC':
                    return 'Admin';
                    break;
                case 'BC':
                    return 'Editor';
                    break;
                case 'C':
                    return 'User';
                    break;
                default :
                    return 'Error';
            }
            
        }
It is possible to loop inside the query result from the controller, but this will add additional loop to the loop in view to display the list. The loop in the view seems like the following
Code:
<?php
    foreach ($query as $row){
    ?>
    <tr>
        <td>&lt;?php echo $row->id;?&gt;</td>
        <td>&lt;?php echo $row->full_name;?&gt;</td>
        <td>&lt;?php echo $row->email;?&gt;</td>
        <td>&lt;?php echo ($row->gender == 'M')? 'Male' : 'Female';?&gt;</td>
//LOOK At The Following Line ***************
        <td>&lt;?php echo _echo_privileges($row->privileges);?&gt;</td>
//***************
        <td>&lt;?php echo $row->join_date;?&gt;</td>
    </tr>
    &lt;?php
    }
    ?&gt;

Please consider the part between **** in the above code. It ofcourse generates call to undefined function - _echo_privileges() - now I need to make this view able to define this function from the controller!


Messages In This Thread
How to Call private method from view? - by El Forum - 01-06-2010, 04:51 PM
How to Call private method from view? - by El Forum - 01-06-2010, 05:13 PM
How to Call private method from view? - by El Forum - 01-06-2010, 05:43 PM
How to Call private method from view? - by El Forum - 01-06-2010, 08:08 PM
How to Call private method from view? - by El Forum - 01-06-2010, 08:55 PM
How to Call private method from view? - by El Forum - 01-06-2010, 08:56 PM
How to Call private method from view? - by El Forum - 01-07-2010, 07:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB