April 17, 201214 yr In my site at the moment for things like menus and all other things, I was thinking it might be easier to read code wise and also quicker if I change my if else statements to switches, for instance as an example in my menu code I have a block of html then some php to say if the user is moderator or admin add this, then back to normal html code, then more php to do the same check to add something else and it goes on for about 2 or 3 more things, would it be simpler and easier to just use a switch and write the whole block of code out for each user level? I did it the other way so that when it comes to editing I don't necessarily need to change 3 lots of the same code it just changes the 1 piece, but it defiantly looks a bit messy!
April 18, 201214 yr Whichever is neater and easier to read. I Generally use switch statements when there are 3+ options, and if/else for 2 options. Whatever works for you though.
April 18, 201214 yr It's up to you, either works. Which looks neater to you? <? switch ($user_level){ case 3: include('admin.inc.php'); break; case 2: include('moderator.inc.php'); break; case 1: include('member.inc.php'); break; default: include('guest.inc.php'); break; } <? if (3 == $user_level){ include('admin.inc.php'); } elseif (2 == $user_level){ include('moderator.inc.php'); } elseif (1 == $user_level){ include('member.inc.php'); } else { include('guest.inc.php'); } Not 100% sure why I naturally went decremental with this. Tiredness?
April 21, 201214 yr Author Let me try and explain it a bit better. I have coded my navigation bar for a user that is logged out, it has lots of little bits of code within it such as anchors, forms, <p>'s etc etc you get the idea. Then in the middle of the code at various places I have added 'if' statements to change a bit of the code depending on whether the viewer is a logged in user, a user that is a mod or a user that is admin, and as neat as I try to keep my code it is beginning to look messy because of all the different places where an 'if' statement is needed. So I was wondering whether to have a switch statement and write the navigation bar out multiple times with the differences in user classifications; switch ($user) { case 1: // Not a logged in user, display nav bar for non user break; case 2: // Standard user, display nav bar for normal user break; case 3: // Mod, display nav bar for moderator break; case 4: // Admin, display nav bar for admin break; } or keep doing how I have done and write the nav bar code once and just change the parts that need changing as they need changing using if else
April 21, 201214 yr mmmm id say use a switch and add each link as needed <?php //Nav bar goes here //Use a switch to add extra links that each cat of user may need switch ($user) { case 1: //Login/register link maybe break; case 2: //Logout link break; case 3: // Logout link/Mod CP link break; case 4: // Logout link/Admin CP link break; } ?> Edited April 21, 201214 yr by webdesigner93
April 21, 201214 yr Author mmmm id say use a switch and add each link as needed <?php //Nav bar goes here //Use a switch to add extra links that each cat of user may need switch ($user) { case 1: //Login/register link maybe break; case 2: //Logout link break; case 3: // Logout link/Mod CP link break; case 4: // Logout link/Admin CP link break; } ?> but the links don't just go at the end of the rest of the code, it is embedded amongst it which is where it is starting to look messy, there would need to be 4 switch statements if i did it that way. I think I'll leave it how it is as it is working fine. thanks for your help though
April 21, 201214 yr but the links don't just go at the end of the rest of the code, it is embedded amongst it which is where it is starting to look messy, there would need to be 4 switch statements if i did it that way. I think I'll leave it how it is as it is working fine. thanks for your help though well u can also put the switch in the middle of the nav or where ever you need it , but whatever suits your needs will be fine
April 22, 201214 yr Author So you have switch statement directly in your frontend view logic? If so, ouch. Why ouch? And no I'm using if else at the minute, I don't quite understand what you mean saying I have it directly in my front end view logic...
Create an account or sign in to comment