Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSamandar Mirzayev
11,834 Pointsreally I screwed up, I don't know what to do. I have tried but useless, I need your support guys
<?php //Available roles: admin, editor, author, subscriber if (!isset($role)) { $role = 'subscriber'; }
//change to switch statement if ($role != 'admin') { echo "You do not have access to this page. Please contact your administrator."; }else{ switch($role){ case "admin": echo "As an admin, you can add, edit, or delete any post."; break; case "editor": echo "As an editor, you can add or edit any post, and delete your own posts."; break; case "author": echo "As an author, you can add, edit, or delete your own post."; break; default: echo "You do not have access to this page. Please contact your administrator."; break; } }
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
$role = 'subscriber';
}
//change to switch statement
if ($role != 'admin') {
echo "You do not have access to this page. Please contact your administrator.";
}else{
switch($role){
case "admin":
echo "As an admin, you can add, edit, or delete any post.";
break;
case "editor":
echo "As an editor, you can add or edit any post, and delete your own posts.";
break;
case "author":
echo "As an author, you can add, edit, or delete your own post.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
}
7 Answers
Benjamin Larson
34,055 PointsYou have the right code for the switch statement, you just need to remove the if/else blocks. The intention of the switch statement here is to replace the if/else structure.
Benjamin Larson
34,055 PointsThough you will need to keep the first if (!isset($role))
block.
Samandar Mirzayev
11,834 PointsAdd a check for the role of "editor" and display the following message: As an editor, you can add or edit any post, and delete your own posts. Add a check for the role of "author" and display the following message: As an author, you can add, edit, or delete your own post. this is question erroris : Bummer! I do not see the correct output for editors. Did you leave off a "break"?
Samandar Mirzayev
11,834 Pointsi stucked guys pls i need support
Marco Wolfs
3,153 Pointsjust delete the if-statement like me and Benjamin said. I literally gave you the solution in my post. You only have to delete $role='editor' on line 8
Your code can't get past the if-statement right now.
Samandar Mirzayev
11,834 Pointsuseless guys, I did same without if structure
Anne Donald
9,847 PointsHi Guy's
I'm having the same issues? really not too sure what to do? or what I'm missing - Any help would be greatly appreciated thank you!
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
$role = 'admin';
}
//change to switch statement
switch($role){
case "admin":
echo 'As an admin, you can add, edt, or delete any post.';
break;
case "editor":
echo "You do not have access to this page. Please contact your administrator.";
break;
case "author":
echo "You do not have access to this page. Please contact your administrator.";
break;
case "subscriber":
echo "You do not have access to this page. Please contact your administrator.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
Anne Donald
9,847 PointsHi Guy's I think this might have something to do with a system error? not too sure but I tried the following, hit the preview button - it then comes up with a 404 error message when I go back to the Editor and preview again it's doing what it's supposed to do?
I hate leaving things unfinished. are there any suggestions as I feel that I'm going to have to move on to the next lessons?
Many Thanks in Advance
Anne
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
$role = 'subscriber';
}
//change to switch statement
$role = 'editor';
switch($role){
case 'admin':
echo "As an admin, you can add, edit, or delete any post.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
Anne Donald
9,847 PointsOkay, going slightly crazy.... so apologies in advance! Don't know why but I removed $role = 'editor'; (underneath the comments " Change to Switch statement" and it seemed to have worked?
German Tsatsura
8,978 PointsHey guys, I think the question is a bit confusing as my first attempt was the first block you see below. I just did not understand what my final output would be so I wrote.
<?php
if (!isset($role)) {
$role = 'subscriber';
}
//change to switch statement
switch ($role != 'admin') {
case "subscriber":
case "editor":
case "author":
echo "You do not have access to this page. Please contact your administrator.";
break;
case "admin":
echo "As an admin, you can add, edit, or delete any post.";
break;
default:
echo "Choose a role.";
}
?>
Which would echo "You do not have acces to this page. Pleae contact your administrator.
But finally did some research and basically found the answer to pass this code test. But the funny thing is I get the exact same output on both the scripts.
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
$role = 'subscriber';
}
//change to switch statement
switch ($role) {
case "admin":
echo "As an admin, you can add, edit, or delete any post.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
}
?>
Can Someone explain exactly why we need to take out != 'admin' within the switch?
Marco Wolfs
3,153 PointsMarco Wolfs
3,153 PointsHey man, I'm pretty new to this as well. It's not really clear what you're trying to do here.
Now without knowing your question, I think I can spot some problems.
First of all your code says that if $role is not set, $role is subscriber. In the code below (in your switch statement) there's no referral to subscribers.
Second problem is pretty big. Your code says that if the role IS NOT admin. It has to echo "You do not have access to this page. Please contact your administrator."
Then you follow up with an else statement for the other roles. The problem is the code won't execute unless the role is admin. In all other cases the code will echo "You do not have access to this page. Please contact your administrator."
For example, lets take editor. The code will go to your if-statement and it will be like .. oh yes this role is not admin. I should echo "You do not have access to this page. Please contact your administrator.". Since I have completed the if-statement there's not reason to proceed to the Else statement.
The way to fix that problem is:
Underneath I have changed the code and I have set $role = 'editor' just to see if it executes (and it does). The first part is still pretty weird because subscriber is not defined in the switch statement.