How can i define, different HTML content for different "user rights"? - php

function settings ()
{
if (!checkRights(2)) {
showRightsInfo();
}
if (checkRights(4)) {
assign("STAMMDATENSTYLE", "");
assign("ADMINSTYLE", "");
}
if (checkRights(3)) {
assign("STAMMDATENSTYLE", "");
("ADMINSTYLE", "display: none;");
} else {
("STAMMDATENSTYLE", "display: none;");
("ADMINSTYLE", "display: none;");
}
("PAGETITLE", "Einstellungen");
("MAINPAGE", "a_settings");
("settings");
}
So i have this code function settings, which is executed why i want to see the settings in my program. My available rights are 1,2,3,4: where 4 should show everything and 3 only "stammdatenstyle".
My function checkRights() returns me if the rights of the current logged in user are higher than the requested rights. I am desperately trying to change my code because I only added a 4th. rights group which can see everything and before "group" 3 was able to see everything. Where is the problem? Do i have an error in my settings()?
<div class="panel panel-default" style="{STAMMDATENSTYLE}">
(....)
<div class="panel panel-default" style="{ADMINSTYLE}">
(....)

The problem is that your falling through the various if conditions and the last if says if = 3 then do this else do this. So condition 4 will get overwritten by this last set of settings.
The way I would do it is to set the default values to start with and then adjust the ones you want to...
function settings ()
{
if (!$this->ctb->checkRights(2)) {
$this->ctb->showRightsInfo();
}
$this->ctb->tpl->assign("STAMMDATENSTYLE", "display: none;");
$this->ctb->tpl->assign("ADMINSTYLE", "display: none;");
if ($this->ctb->checkRights(4)) {
$this->ctb->tpl->assign("STAMMDATENSTYLE", "");
$this->ctb->tpl->assign("ADMINSTYLE", "");
}
if ($this->ctb->checkRights(3)) {
$this->ctb->tpl->assign("STAMMDATENSTYLE", "");
}
$this->ctb->tpl->assign("PAGETITLE", "Einstellungen");
$this->ctb->tpl->parse("AUSSCHUSSMAINPAGE", "ausschuss_settings");
$this->buildPage("settings");
}
You could also change to use if() .. elseif... else... but make sure you get the right combinations.

you have the following if/else construct:
if(condition){
// something
}
if(condition){
// something
}else{
// something
}
this will cause problems surely. as you can see, if the first if-condition is met and the second is not met it will execute two parts of the script, instead of one.
Example:
$color = 'blue';
if($color == 'blue'){
echo 1;
}
if($Color == 'green'){
echo 2;
}else{
echo 3;
}
This will output 1 and 3 and this is how your current script works.
Therefore you should use at least something like elseif
if(condition){
// something
}elseif(condition){
// something
}else{
// something
}
but I recommend you to use switch
switch(condition){
case 1:
// something;
break;
case 2:
// something;
break;
case 3:
// something;
break;
case 4:
// something;
break;
default:
// something
}

Related

if statement and isset($_GET if statement error with the ifelse

<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '1'): ?>
<?php include('pages/home.php');?>
<?php endif;?>
<?php };?>
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '2'):
include('pages/about.php');
else: include('pages/home.php'); endif; };?>
The above is what I have used to try and fix it, but if I don't put it, errors show up. For example if I used "ifelse", and it tells me to change it to else, or endif. But when I use endif, it tells me to use ifelse. I'm trying to make it so http://localhost/?p=PAGE_ID just simply shows the page using an include(FILE_PATH) statement.
Does anyone know where I went wrong? Or how I can fix it :)
You seem to have thoroughly tied yourself in knots. To my mind, this is a much cleanrer and more flexible and maintainable way to approach the whole thing:
$pages = [
"1" => "pages/home.php",
"2" => "pages/about.php"
];
if (isset($_GET['p'])) {
if (isset($pages[$_GET['p']]))
{
$page = $pages[$_GET['p']];
}
else
{
$page = $pages["1"];
}
}
else
{
$page = $pages["1"];
}
include($page);
This way, you have a list of pages, and the system simply looks up the required page in the array by its index number (as passed in via the GET parameter). If it can't find that index, it just uses the default page.
You can now easily add pages by just adding items to the array, without needing to write more if/else logic. You could even store the array data in a database instead of it being hard-coded.
You dont need or want an elseif in this flow, simple do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
}
if ($_GET['p'] == '2') {
include('pages/about.php');
}
}
If the value of $_GET['p'] can only be 1 or 2 you could do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
} else {
include('pages/about.php');
}
}
Or if $_GET['p'] could be multiple values a switch may be more useful
<?php
if (isset($_GET['p'])) {
switch($_GET['p']) {
case 1:
include('pages/home.php');
break;
case 2:
include('pages/about.php');
break;
default:
include('pages/somethingelse.php');
}
}
Note I also left out the intermediate variable, as its not needed. The $_GET and $_POST arrays are, once filled by PHP all yours to use as you want. There is no need to waste CPU cycles and memory creating unnecessary variables

PHP: Is there an alternative to using multiple if-statements?

I am in the making of some code that needs to check if a users login details are correct, and I therefore need a lot of if-statements inside each other. If any of the conditions in the if-statements are not true, they should alle return the same value. Is there an easy way of doing this, instead of writing the same multiple times? I have made an example below to visualize my problem. As you can see here I write " else { return false; }" multiple time, and this is what I am wondering if you are able to do more efficiently. Maybe so I only have to write "or else return false" once.
//some code
if (/*some condition*/) {
//some code
if (/*some new condition*/) {
//some code
if (/*some new condition*/) {
//some code
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
I am having a hard time finding a good way to explain my problem, so if you have a more elegant way of explaining it, do not hesitate to edit my post. I am also not quite sure that the title is as good as it could be, so if you have any ideas to an alternativ please say so :)
Lets say you have something like that (I added No):
if ( condition1 ) {
//some code 1
if ( condition2 ) {
//some code 2
if ( condition3 ) {
//some code 3
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
Since each time a condition is false, you exit the function returning false, you can directly test if the condition is false using a negation (if the negated condition is true):
if ( !condition1 ) {
return false;
}
//some code 1
if ( !condition2 ) {
return false;
}
//some code 2
if ( !condition3 ) {
return false;
}
//some code 3
This doesn't reduce the number of if statements, but you avoid many nesting levels and the else statements.
You can also try the switch statement. For many situations it will produce cleaner code.
<?php
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
The switch statement is also compatible with using strings:
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
Good luck! :)

If Dynamic URL is specific, do this?

I have this code being used on my website
if( isset($_GET['gw'] )){
//this is some content
}
So whenever my url is something like this
http://mywebsite.com/?gw=hello
the content inside that div will appear. It works very well, but now what I want to do is have different content for a specific string. Something like this
if (gw=hello) {
// content here
} else if (gw=goodbye) {
// content here
} else {
//content here
}
I know that is not the code, but does anyone know a way I could do this? Thanks!
Your pretty much there!
if(isset($_GET['gw'])) {
if($_GET['gw'] == "hello") {
}
else if ($_GET['gw'] == "goodbye") {
}
else {
}
}
It's probably best to use a switch statement, so you can easily expand on the conditions in the future.
switch (isset($_GET['gw']) ? $_GET['gw'] : '')
{
case "hello":
// do something here
break;
case "goodbye":
// do something here
break;
default:
// default action if nothing above matches
break;
}

Restart an if else construct

Let's take a look at the following code:
if ($a == 1) {
echo "this is stage 1";
}
else if ($a == 2) {
echo "this is stage 2";
}
else if ($a == 3) {
$a = 1;
// at this point I want something that restarts the if-else construct so
// that the output will be "this is stage 1"
}
I'm working on an if else construct at the moment and let's say that I have three stages and the if-else construct checks which stage I'm in.
Now it happens that some activities in stage 3 lead to a jump back to stage 1. Now I've already passed the code for stage one, which is why I want to somehow restart the if-else construct. Is there a way to do that? And even more important: Is there a better way to do what I want? Because my idea doesn't seem to be good practice.
You're right, it's bad practice.
You're asking for goto.
Example:
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
The above would never output 'Foo'
It's difficult to suggest the better method without seeing exactly what you're trying to do, but consider a switch.
switch ($a) {
case 3:
// Execute 3 stuff
// No break so it'll continue to 1
case 1:
// Execute 1 stuff
break // Don't go any further
case 2:
// 2 stuff
break;
}
That's probably not what you want either.
You may just want to abstract the code into functions and call them multiple times if necessary.
You can put an endless loop around your if and break out if you're done
while (1) {
if ($a == 1) {
echo "this is stage 1";
break;
}
else if ($a == 2) {
echo "this is stage 2";
break;
}
else if ($a == 3) {
$a = 1;
}
else {
break;
}
}
Maybe you want to look at Wikipedia - Finite-state machine and this question PHP state machine framework
The short answer is yes, there is a way, but the better answer is yes to your second question as well.
Put, at very least, the code that can get called from multiple locations in a function. For example,
function stageOneCode() {
//do stuff;
}
etc.. I would recommend a function for each stage, but it's hard to make recommendations without actually seeing what's being executed in the stages.
In any event, at the end of your stage three function, simply call your stage one function.
A recursive function is helpful for this (but maybe overkill if it will always revert back to 1)
function echo_stage($stage) {
if ($a == 1) {
return "this is stage 1";
}
else if ($a == 2) {
return "this is stage 2";
}
return echo_stage(1);
}
echo echo_stage(5);
Or:
switch ($number)
{
case 2 :
echo "this is stage 2";
break;
case 1:
default:
echo "this is stage 1"
}
use switch(). you can have a "default" case as well as specific cases.
A loop is what you are searching for:
// initialize $a
$a = 1;
// the while loop will return endless
while (true);
// if you want to break for any reason use the
// break statement:
// if ($whatever) {
// break;
// }
if ($a == 1) {
echo "this is stage 1";
}
else if ($a == 2) {
echo "this is stage 2";
}
else if ($a == 3) {
$a = 1;
// continue will go back to the head
// of the loop (step 1) early:
continue;
}
// don't forget to increment $a in every loop
$a++;
}

PHP flow logic problem

I have a bit of a problem with my PHP code, I am assigning values to variables in different states of the flow depending on what I receive, but for some reason it keeps getting stuck at one point, here is the code.
if (isset($session)) {
//if the user is in the database
if ($row == 1) {
$from = $_GET['from'];
if (isset($from)) {
$page = $_GET['page'];
switch ($page) {
case "game":
$page = "game";
sendVars($page);//send the variable
break;
case "gallery":
$page = "gallery";
sendVars($page);//send the variable
break;
case "leaderboard":
$page = "leaderboard";
sendVars($page);//send the Variable
break;
}
}else {
$page = "game";
sendVars($page);//send the variable
}
//if the user is not in the database
}else {
//do this
}
} else {
//register
}
Now for some odd reason, it keeps setting the value of $page to game, even though I set the page variable to gallery like so http://www.mydomai.com/?from=set&page=gallery . the only reason for this that I can think of is that my switch is not working as it should? or it is bypassing the switch somehow?
Thanx in advance!
I just ran your code after removing a few of the unessersary variable assignments:
<?php
// I added this function just for testing
function sendVars($page) {
echo $page;
}
if (isset($_GET['from'])) {
$page = $_GET['page'];
switch ($page) {
case "game":
sendVars($page); //send the variable
break;
case "gallery":
sendVars($page); //send the variable
break;
case "leaderboard":
sendVars($page); //send the Variable
break;
}
} else {
$page = "game";
sendVars($page); //send the variable
}
And it all seems fine, xxx.php?from=1&page=gallery echos out "gallery", try doing a print_r($_GET) at the top of your script and see what it prints out and let us know.
On a side note I think the below may be shorter for you and still do the same thing:
if (isset($_GET['from'])) {
// Check if $_GET['page'] exsists and is either game, gallery or leaderboard
if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
sendVars($_GET['page']);
}
else
sendVars('game');
I hope this helps
Cheers
Luke
Try doing a var_dump($page);exit; before the switch and see what it spits out.
Also you can do a var_dump($from) and see what that is spitting out - it may be that it goes to the else, so it may not even be getting to the switch.
If this is inside a function, I personally prefer guard-style clauses than constantly increasing the levels of indentation. The idea is you pick out the bad conditions (ie if something is going wrong) to "protect" the larger block of logic.
In your case that's the switch statement.
if (!isset($session))
return ...; // register
if ($row != 1)
return ...; // do this
$from = $_GET['from'];
$page = $_GET['page'];
if (isset($from)) switch ($page) {
case "game":
$page = "game";
break;
case "gallery":
$page = "gallery";
break;
case "leaderboard":
$page = "leaderboard";
break;
}
else $page = "game";
return sendVars($page);// don't need to repeat this if all cases do it!
It's just a style of code and it's not going to fix all (if any) of your problems. You actually don't need the switch block in there for this code. I can't see that it's doing anything.
You don't necessarily need the switch statement. I can't see an obvious problem with your logic but I think this will do the same thing:
if (isset($session))
{
//if the user is in the database
if ($row == 1)
{
$page = (in_array($_GET['page'],array('game','gallery','leaderboard'))) ? $_GET['page'] : "game";
sendVars($page); //send the variable
}
else //if the user is not in the database
{
//do this
}
}
else
{
//register
}
Just saw Luke used the same in_array method but 25 mins before me. Serves me right for being slow!

Categories