Show button if two conditions are met - php

So I have a button that I want to show and hide on certain parts.
So if $unlockcourse is false AND the $courseid is 1, I would like the button to be hidden, BUT I would like it to be shown at all other times no matter what the courseidis
$courseid = ($userinfo['course']) * 1;
$mycourse = ($userinfo['id_course']) * 1;
$unlockcourse = true;
if($haspaid == 1){
$unlockcourse = false;
} else if ($haspaid == 0) {
$unlockcourse = true;
}
<?php if ($unlockcourse == false && $mycourse >= 1) { ?>
Resume Course
<?php } ?>

I am surprised how did this not throw an error. The error lies here:
if ($unlockcourse == false) && if ($mycourse == 1) && {
It should be:
if ($unlockcourse == false && $mycourse == 1) {

So what you actually want to do is show it if unlockcourse is false, and mycourse is 1. This means that to find out when to show it, you need to negate both conditions like this:
<?php if (!($unlockcourse == false && $mycourse == 1)) { ?>
Resume Course
<?php } ?>

Try Editing the following code
<?php if ($unlockcourse == false && $mycourse >= 1) { ?>
Resume Course
<?php } ?>
into
<?php if !($unlockcourse == false && $mycourse >= 1){
echo "<a href='course-work-proc.php' class='btn btn-primary'>Resume Course</a>";
}?>
This will make the button appear when your statements are true, and nothing will appear when the statement is false

Related

PHP Elseif always returning false, multiple conditions

I'm trying to make an helper php-function that's going to return true/false if the user got right access level. The access level is set when the user logs in. The problem is that the function always return false. The function is located in a "class" php-file that's included(with include_once) on the page I want to use it.
I'm kinda new to php, but the if conditions seems to be right.
I have tested to log in as admin and "economy", but it doesn't return true.
I also have tried to echoing the value that is sent as an parameter and even checking that the access level is right(by echoing the value) before the elseif statement.
const AccessLevelUser = 0;
const AccessLevelAdmin = 1;
const AccessLevelManager = 2;
const AccessLevelEconomy = 3;
public static function hasAccessLevel($requiredAccess) {
//file_put_contents('logg.txt', $logg);
if( !isset($_SESSION['accesslevel']) ) {
header("location:index.php?location=login");
exit;
}elseif ( $requiredAccess == AccessLevelAdmin && $_SESSION['accesslevel'] == AccessLevelAdmin ) {
echo "Admin True";
return true;
}elseif( $requiredAccess == AccessLevelEconomy && ($_SESSION['accesslevel'] == 3 || $_SESSION['accesslevel'] == 1) ) {
echo "Economy True";
return true;
}elseif( $requiredAccess == AccessLevelManager && ($_SESSION['accesslevel'] == 2 || $_SESSION['accesslevel'] == 1) ) {
echo "Manager True";
return true;
}elseif( $requiredAccess == AccessLevelUser && ($_SESSION['accesslevel'] == 0 || $_SESSION['accesslevel'] == 1) ) {
echo "User True";
return true;
}else{
echo "FALSE!";
return false;
}
}

PHP - How to check users level into a login session

I am using this code to protect my pages using login session:
<?php
//PUT THIS HEADER ON TOP OF EACH UNIQUE PAGE
session_start();
if (!isset($_SESSION['username'])) {
return header("location:login/main_login.php");
}
?>
but I'd like to check the users level, because I'd like to show e.g. page-one.php for users level 1 and 2; page-two.php for users level 3 and so on...
I tried in this way but it's not working:
<?php
session_start();
$level = 2;
$_SESSION['level'] = $level;
if (!isset($_SESSION['username'])) {
if($_SESSION['level'] == '0' && $_SESSION['level'] == '1') {
return header("location: page-one.php");
} else if ($_SESSION['level'] == '2') {
return header("location: page-two.php");
}
}
Any advice, please?
EDIT I edited my code, I found another solution: I stored level in a variable and I checked it together the username
This will always be false:
if($_SESSION['level'] == '0' && $_SESSION['level'] == '1')
The same variable can't equal two different values at the same time. Did you mean to use the comparison "or" operator (||) instead?:
if($_SESSION['level'] == '0' || $_SESSION['level'] == '1')
Or perhaps more else-if chaining?:
if($_SESSION['level'] == '0') {
//...
} else if ($_SESSION['level'] == '1') {
//...
} else if ($_SESSION['level'] == '2') {
//...
}

Conditionals operation helps in php

Hello everybody i put this code but and when the value referal is < 0 running }else{ but i dont do.
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}
}
I want to put }else{ but not how, }else{ $referal="$referal2"; ????
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}}else{ $referal="$referal2";
}
Regards and thanks
First of all this the if elseif statement syntax.
if($condition1){
// code if the first condition is true
}elseif($condition2){
// code if the first condition is false and the second one is true
}else{
// code if both the first and second condition are false
}
it is described in the php official website
If I have understood your question you want, when the $referal1 is equal to 0, to set the value of $referal2 to $referal1.
In this case your code should be something like this
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}elseif($referal==0){
$referal=$referal2;
}
Hope this will solve your problem.

Looking into row for data match MySQLi

<?php
include_once("php_includes/check_login_status.php");
if($user_ok != true){
header("location: login.php");
exit();
}
?>
<?php
$result = mysqli_query($db_conx, "SELECT active FROM profilepage WHERE username='$u'") or die(mysqli_error($db_conx));
$row = mysqli_fetch_assoc($result) or die ("$u not found");
$a_check = $row['active'];
if ($u == $log_username && $user_ok == true && $a_check = 1){
echo include_once("videofeedform.php"); }
else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
{ echo "viewers should only see this"; }
exit();
if($u == $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok != true && $a_check = 0)
($a_check = 0); {
echo ' Video is going to be added here'; }
exit();
?>
ok so basically I am trying to make a module work only if it has been activated to work by the user. Only the owner of the pages sees what they is mean to and the viewer only sees what they are meant to so that part works perfect however i need to tell the script to do these IF's only if the data inside the row match 1 and if they do not match 1 (e.g 0) then they do something else. I believe the current search is only looking for a number of rows called active what is owned by the user and if it is then do...... I was wondering how I would make the query look inside the row to find the data it needs to execute as it just seems like it is just executing when it finds 1 row that matches
okay there is a decent amount wrong with this script.
you never show us what $log_username or $user_ok so I cannot help to evaluate whether those contain the correct values in your script. We'll just assume they do.
You must brush up on your usage of Comparison Operators in php.:
http://www.php.net/manual/en/language.operators.comparison.php
throughout the conditionals(if, elseif, etc..) in your script you have things like:
if ( $a_check = 1 )
but with a single '=' you are assigning the integer value 1 to the variable $a_check, this is basically the same thing as saying:
if (1)`
which evaluates to true in your if statement (any integer value other than 0 will always evaluate to true).
similarly the line
if ( $a_check = 2 )
is the same as
if (2)
and would also evaluate to true
as aldanux mentioned in his comment above, if you would like to compare if 2 variables are equal you should use ==.
if ( $var1 == $var2 )
if they are equal this will evaluate to true.
More issues with conditionals:
http://www.php.net/manual/en/control-structures.elseif.php
this block:
if ($u == $log_username && $user_ok == true && $a_check = 1){
echo include_once("videofeedform.php"); }
else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
{ echo "viewers should only see this"; }
exit();
should be more like:
if ($u == $log_username && $user_ok == true && $a_check == 1){
echo include_once("videofeedform.php");
}
else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)
{
echo "viewers should only see this";
exit(); //move exit inside the elseif block
}
fixed conditionals
use elseif since you want to evaluate another expression (e.g. $user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1),
remove semicolon from the end of your else statements
move exit() inside the elseif block (as you currently have the script would always exit on that line).
also an additional comment. the expression:
else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)
is saying i don't care is $user_ok is true or false as long as $a_check equals one. This is the same thing as doing:
else if ($a_check == 1)
though... now im thinking you just want to exit if your previous if statment false:
if ($u == $log_username && $user_ok == true && $a_check == 1) //if this fails, exit
so your code there would actually look something more like:
if ($u == $log_username && $user_ok == true && $a_check == 1){
echo include_once("videofeedform.php");
}
else{ //just remove the expression from here
echo "viewers should only see this";
exit(); //move exit inside the elseif block
}
these same issues discussed above exist with your last if statement.

Idea with php logic using operators

I am new to PHP and find it very hard to explain.
I have a PHP navigation script with 5 categories and two variables relevant to my question:
$catname = 'used-cars'; // the same value for all categories
$currentpage; // pages 1 to 5
index.php of my site has $currentpage == '1'
The issue is that I need a logic that will say:
If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway
I am thinking of something like this:
if($catname != 'used-cars' && !($currentpage > '1')):
endif;
Hope you can help!
This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.
if ($catname !== "used-cars" || $currentpage === 1) {
// do your thing
}
This is just:
if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)
(Careful with the string comparison.)
Alternatively, you could declare it as a boolean first:
$proceed = false;
if($catname != 'used-cars')
$proceed = true;
if($currentpage == 1)
$proceed = true;
if($proceed){
// whatever you want
}
$doflag = 0;
if($catname != 'used-cars')
{
$doflag = 1;
} else if($currentpage == 1) {
$doflag = 1;
}
if($doflag == 1) {
//do something
}
Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.

Categories