Jump to content

Displaying different interfaces for different users on the same page


Recommended Posts

I have a question. Say I want to display on my php page an interface for users. I want that interface to change depending on the user type. For example, a super-user gets extra controls that the normal users won't have.

 

I've used the following approach in my code:

 

function display_interface() {

if ($array['user_type'] == "0") {
$interface = '<html code in here>';
}
else if ($array['user_type'] == "1") {
$interface = '<different html code in here>';
}

return $interface;
}

 

 

I do find this approach impractical however. As I have to paste the html code somewhere else everytime I need to edit it (I use dreamweaver). Also, I can't pass arrays/etc. to $interface since it's a variable.

 

Any ideas/hints/tips for a better approach? Thanks in advance.

there is no doubt that there are many ways to achieve this feature. however, what you want to do specifically would dictate how you took your approach. with the information you've provided, i'd do something similar to cmgmyr's suggestion, except i'd use a switch() statement. design a template for each type of user and call upon whichever one is needed using the switch:

<?php
        switch($array['user_type']){
                case 1;
                        include "template1.php";
                        break;
                case 2;
                        include "template2.php";
                        break;
                case 3;
                        include "template3.php";
                        break;
                case 4;
                        include "template4.php";
                        break;
                default;
                        include "template0.php";
                        break;
        }
?>

It does seem that this approach is a whole lot better than what I've been doing. Thanks alot for the suggestions.

 

I have another small question. Say I designed my page the way you both suggested, by including external files. Is it recommended to write all the functions in the main page, or is it better to write each set of functions inside it's related external page? I assume it's more secure/organized that way?

It does seem that this approach is a whole lot better than what I've been doing. Thanks alot for the suggestions.

 

I have another small question. Say I designed my page the way you both suggested, by including external files. Is it recommended to write all the functions in the main page, or is it better to write each set of functions inside it's related external page? I assume it's more secure/organized that way?

 

to be honest it's all personal preference. if you feel like your code is better organized with local functions for each page, then do that. if you feel like there should be a page that holds all your functions, do that. really this is a question of design implimentation. there's actually a forum of this topic in here. this can also spawn the interest of studying object oriented programming. you can find useful documents in my signature under the category oop.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.