bachx Posted April 24, 2007 Share Posted April 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/ Share on other sites More sharing options...
cmgmyr Posted April 24, 2007 Share Posted April 24, 2007 You can also include/forward them to a different page: if ($array['user_type'] == "0") { include "user.php"; } else if ($array['user_type'] == "1") { include "admin.php"; } Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/#findComment-236961 Share on other sites More sharing options...
boo_lolly Posted April 24, 2007 Share Posted April 24, 2007 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/#findComment-236970 Share on other sites More sharing options...
bachx Posted April 24, 2007 Author Share Posted April 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/#findComment-236985 Share on other sites More sharing options...
bachx Posted April 24, 2007 Author Share Posted April 24, 2007 Anyone can answer that last question? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/#findComment-237148 Share on other sites More sharing options...
boo_lolly Posted April 24, 2007 Share Posted April 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48458-displaying-different-interfaces-for-different-users-on-the-same-page/#findComment-237161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.