I am using codeigniter and wants to shwo the js code only if following condition true
<?php
if(($this->uri->segment(1)!='reports') || ($this->uri->segment(2)!='manageflyers' && $this->uri->segment(3)!='save')){?>
//some js code
<?php }?>
But i does not work please help where i am making the mistake.
A couple of != with an OR || will always equal true because that string variable will only equal one thing and not be the other
so your code will work like this
<?php
if(($this->uri->segment(1)=='reports') || ($this->uri->segment(2)=='manageflyers' && $this->uri->segment(3)=='save')){
//do nothing
}else{
//some js code
}
Related
#if(\Route::getFacadeRoot()->current()->uri() == 'dashboard' || \Route::getFacadeRoot()->current()->uri() == 'welcome' || \Route::getFacadeRoot()->current()->uri() == 'change-password' ||
\Route::getFacadeRoot()->current()->uri() == 'not-found' )#endif
In case of PHP, add <?php at the beggining of the file and ?> at the end of the file.
Other than PHP, search online.
I am trying to create a function that checks part of the URL on a page and then either shows or hides a nav bar based on the results. I can get the function to work if it checks for only one string but adding different strings in an OR statement doesn't do anything.
Below is an example of the function working with a single condition...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
This correctly prevents the navbar from showing up on the "login" page. But if I also want the navbar to not show up on the "register" page and do something like this...
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $SERVER['REQUEST_URI'];
if (strpos($url,'/login') == false || strpos($url,'/register') == false){ ?>
<nav>Navbar</nav>
<?php } ?>
...the functionality stops working entirely and the navbar shows up on all pages.
So how can I refine my OR statement so that it works properly? Thanks!
Logic flaw. Change the or condition to and
if(strpos($url,'/login')==false || strpos($url,'/register')==false)
Should be
if(strpos($url,'/login')===false && strpos($url,'/register')===false)
You want a && ... not a ||. Because you want the $url to not contain "/login" AND "/register". Otherwise it'll always be true.
try with () like this:
if ( (strpos($url,'/login') === false) || (strpos($url,'/register') === false) ){ ?>
THE PROBLEM
The problem I've having is I can't override the current page with include.
if ($LS::getUser('clan') || isset($_GET['clan']) && !isset($_GET['search'])) {
include("res/templates/clan-overview.php");
} else {
include("res/templates/clan-search.php");
}
I don't get why I can't change the page with clan-search.php because I said that if $_GET['search'] is NOT set execute the else statement. But for some weird reason I'm still getting true even though I said that if the user has a CLAN || $_GET['clan'] AND $_GET['search'].
What I've Tried
That code I put at the top is actually the modified version of my original code which I thought would fix the problem. If you are interested in looking at the original code which I don't think will help you here it is:
if ($LS::getUser('clan') || isset($_GET['clan'])) {
include("res/templates/clan-overview.php");
} else if (isset($_GET['search'])) {
include("res/templates/clan-search.php");
}
#Drew pierce gave you first response as you need to wrap the OR parts as a group. try this:
if (($LS::getUser('clan') || isset($_GET['clan'])) && !isset($_GET['search'])) {
I'm trying to implement multiple if(isset()) statements but I can't get it to work.
example:
if (isset($_GET['a']) or isset($_GET['b'])) {
// HTML
} else {
// HTML
<a href="?link=a>LinkA</a>
<a href="?link=b>LinkB</a>
}
When I click a or b I still got the else statement executed.
I also tried:
if (isset($_GET['a'] or $_GET['b']))
but then I get a error
I'm trying to display different pages on different $_GET requests.
Can someone point me in the right direction or is this not the right way to do this?
Change if (isset($_GET['a']) or isset($_GET['b'])) To:
if ( (isset($_GET['link']) && $_GET['link'] == 'a') OR (isset($_GET['link']) && $_GET['link'] == 'b']) )
You are checking wrong variable.
You need to check $_GET ['link']
I am trying to hide a div once a user has logged into there account of my online store (built with cartweaver 4 php) but i just cant seem to get it to work.
the php code for defining wether the user is logged in or not is as followed:
if(!strlen($_SESSION["cwclient"]["cwCustomerID"]) ||
$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
$_SESSION["cwclient"]["cwCustomerID"] === "0" ||
$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
and the div i would like to hide upon login, which contains a table, have been given an id and class of:
<div id="newcustomertable" class='newcustomer'>
I have tried applying this method using css: Show Hide div if, if statement is true
but that ended up giving me an undefined variable error on my testing server.
I admit i am a bit of a php newbie, so if anyone who is able to make more sense of this could help out and possibly find a solution it would be much appreciated.
Thank you.
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
Make sure that the condition check is AFTER the div element getting rendered. Because if you put it before the element the page would not have fully rendered and so the script will not be able to get the div that you want to hide.
REVERSE Condition based on OP's request. I know this is a kluge, folks. Dont flame me!
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
//do nothing
}
else
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
You've got at least 3 variables there that could be throwing your undefined variable warning:
$_SESSION["cwclient"]
$_SESSION["cwclient"]["cwCustomerID"]
$_SESSION["cwclient"]["cwCustomerType"]
you need to isset() each of those before doing any tests.
For starters, the strlen function in PHP returns an INT, not a boolean.
Change from:
(!strlen($_SESSION["cwclient"]["cwCustomerID"])
to:
(strlen($_SESSION["cwclient"]["cwCustomerID"]) > 0)
First of all could that not just be simplified to the following if statement to make it easier
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
#code here
}
Second of all to hide the div if they are logged in you could do it like this
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
$shouldhide="style='visibility:hidden;'";
} else {
$shouldhide="";
}
echo"<div id='hidethis' $shouldhide>";
Although if the content is a security risk it'd be better to not output it at all as it's still visible in the source code.