PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
As It's quite obvious what you mean by "Whats wrong with this?", mainly you are using multiple if/ifelse commands, and not resuing your code correctly..
You could actually use a switch statement and reduce the amount of data you are defining by just defining a static variable to hold a alternating value such as your URL.
$path = NULL;
switch(strtolower($_SESSION['level'])) {
case 'user':
$path = 'user/control_panel.php';
break;
case 'manager':
$path = 'manager/control_panel.php';
break;
case 'creator':
$path = 'creator/control_panel.php';
break;
default:
// Set $path to be whatever it should be for any other condition not met above. In this case, I assume you are setting anything that doesn't meet the first requirements to be the owner.
$path = 'owner/control_panel.php';
break;
}
$meta_information = '<meta http-equiv="refresh" content="0;URL=' . $path . '">';
you could, but if the directory URL ever changes, or becomes different per instance, the above will result in error.
edit: The key to any coding practice is to train yourself to code expecting changes to be made later down the road, which would result in fewer lines to edit and more control of what/how data s used.
That could be said about the OP's code as well, seeing as all my code does is compact his list of if/elseif's into one simple string. I was just responding to that (somewhat vague) question.
Looking at what it seems to be doing, I would guess that, dependent on a given users level, the user would be redirected to a unique control panel inside a folder that is named for their level. Would I do this? Not on your life. I would setup one control panel and figure out what to offer the user depending on their level inside that one control panel.
But given the OP's original question, 'What is wrong with this?', well, not a whole lot other than it seems to be really inefficient. I would just make it a little more simple, which is what I did in my code example.
Everah wrote:That could be said about the OP's code as well, seeing as all my code does is compact his list of if/elseif's into one simple string. I was just responding to that (somewhat vague) question.
Looking at what it seems to be doing, I would guess that, dependent on a given users level, the user would be redirected to a unique control panel inside a folder that is named for their level. Would I do this? Not on your life. I would setup one control panel and figure out what to offer the user depending on their level inside that one control panel.
But given the OP's original question, 'What is wrong with this?', well, not a whole lot other than it seems to be really inefficient. I would just make it a little more simple, which is what I did in my code example.
100% agree. However, a lot of beginner level coders begin writing code such as this as an indirect means of achieving a solution for something later.
To reitterate what you said, a simple solution is often the best in almost every instance of development. Write it, make it work, and then rewrite it.
But to reitterate what I said, it's also good to write towards a more dynamic approach for obsticals later on down the road.
Agreed. Foresight in planning is a rather keen piece of advice. Of course, most new developers want to know "how to make this do what I want it to", not "how to make this do what I want it to in the best way possible" (yes, I was one of those types of newbies).