Is there a way to add a button for a if/else statement? This is my code
<?php if(isset($_SESSION["steamname"]))
//If steamname not equals 0
{
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
}
else
{
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
}
?>
But my server keeps saying that it's a invalid. My understanding of php isn't that great but what I'm trying to do is to make it so that if a user is logged in a logout button will appear and if not it will be login. My current method doesn't work so is it even possible? Thanks.
P.S. I've tried echoing it out, no luck either.
P.S.S I don't think it has anything to do with my isset command. I did a plain echo and it worked out fine.
You need to echo the HTML you want:
<?php if(isset($_SESSION["steamname"]))
//If steamname not equals 0
{
echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>';
}
else
{
echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>';
}
?>
Without the echo, PHP will try to parse your HTML as PHP, which won't work.
change your code to. You have to put html tags out side PHP
<?php if(isset($_SESSION["steamname"]))
{ ?>
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
<?php }
else
{ ?>
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
<?php } ?>
OR
You can echo html tags
<?php if(isset($_SESSION["steamname"]))
{
echo '<a class="button-logout" href="steamauth/logout.php">Log Out</a>';
}
else
{
echo '<a class="button-login" href="steamauth/login_steam.php">Log In</a>';
}
?>
If you don't want to echo html as a string, you can do it like this with alternative syntax:
<?php if(isset($_SESSION['steamname'])): ?>
<a class="button-logout" href="steamauth/logout.php">Log Out</a>
<?php else: ?>
<a class="button-login" href="steamauth/login_steam.php">Log In</a>
<?php endif; ?>
Related
I'm planning to create a header navigate bar for both user & admin with different content. If it is the user, so they will only see the Log out button. If it is the Admin, they will see button "Usermanagement" and "Logout" using php. For now I didn't to manage to ensure that the php read the status of current user. 'roles' = 'Y' is consider as admin, i store it inside mssql. Below is my code.
<?php if( isset($_SESSION['username']) && !empty($_SESSION['username']))
{ ?>
<a class="right" href="logout.php">Log Out</a>
<?php if( isset($_SESSION['roles']) && $_SESSION['roles']===['Y']) ?>
<a class="right" href="userManagement.php">User Profile</a>
<?php }else{ ?>
<a class="right" href="login.php">Login</a>
<a class="right" href="register.php">Register</a>
<?php } ?>
any help would highly appreciated, i've been searching and applying many code since last 4 days, none of the code working.
I'm not sure if you want this or not.
<?php if (isset($_SESSION['username']) && !empty($_SESSION['username'])) { ?>
<a class="right" href="logout.php">Log Out</a>
<?php if ($_SESSION['roles'] === "Y") { ?>
<a class="right" href="userManagement.php">User Profile</a>
<?php } ?>
<?php } else { ?>
<a class="right" href="login.php">Login</a>
<a class="right" href="register.php">Register</a>
<?php } ?>
To conditionally output HTML code you need to echo it from PHP. Here's what I think you want. I have kept your formatting
<?php
if( isset($_SESSION['username']) && !empty($_SESSION['username']))
{echo '<a class="right" href="logout.php">Log Out</a>';}
if( isset($_SESSION['roles']) && $_SESSION['roles']===['Y'])
{echo '<a class="right" href="userManagement.php">User Profile</a>';}
else
{echo '<a class="right" href="login.php">Login</a>';
echo '<a class="right" href="register.php">Register</a>';}
?>
How can i show some default code only if BOTH if statements are false ?
I've got this code -
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
i'm not using the else bit at the end at the moment because i only want that to show if both 'if's are false ?
hope that it makes sense
You can use 1 php tag only to make your syntax much more readable and
avoiding
multiple <?php ?> tags.
you can also use echo to print your html markup using php scripting like this.
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
Ok thanks , the 'else' works if neither of the if's are true , but if i have one that is true or both are true , it only displays the first entry but shows it twice ?
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>
I am surprised to see my top login bar cannot show any change after user login.
Other element of in this bar like logo, decorative div show as it but after 'login to change username' & 'registration to change logout' & 'all notification' display like I am not logged in.
I cannot understand where is my wrong. It cannot show any error also.
Please see my code:
Included my every pages:
<?php include_once("top_login.php"); ?>
my top_login.php
<?php include_once("login/session.php"); ?>
<div id="toplogin">
<div class="logintext1">
<?
if($session->logged_in){
echo '<a class="logintext" href=../login/myprocess.php>Logout</a>';
} else {
echo '<a class="logintext" href="../login/regis.php">Register</a>';
}
?>
</div>
<div class="logintext2">
<?
if($session->logged_in){
echo '<a class="logintext" href="../login/mn.php">'.$_SESSION["username"].'</a>';
} else {
echo '<a class="logintext" href="../login/mn.php">Log In</a>';
}
?>
</div>
<div class="logintext3">
<?
if($session->logged_in){
echo '<a class="logintext" href="#"><img style="padding:2px 2px" src="http://www.mywebsite.com/images/nof2.png" /></a>';
} else {
echo '<a class="logintext" href="../login/mn.php"><img style="padding:2px 2px" src="http://www.mywebsite.com/images/nof.png" /> </a>';
}
?>
</div>
</div>
I'm trying to make an if-else-statement which works by going if there is a link print it around the name.
I have the below code which is almost there. However, the link is being printed above the text ranther than being an actual link.
<?php if( the_sub_field('corporate_link') ){ ?>
<a href="<?php the_sub_field('corporate_link'); ?>"
target="_blank"
title="<?php the_field('corporate_name'); ?>"><?php the_field('corporate_name'); ?></a>
<?php } else { ?>
<?php the_sub_field('corporate_name'); ?>
<?php } ?>
Any thoughts on how to make it link instead of printing the link if it`s there?
So what im looking to acheive is if there is a link print this
Coprate Name
If there isn't a link it just shows the corporate name.
use get_sub_field('corporate_link') instead of the_sub_field('corporate_link')
<?php
$corporate_link = get_sub_field('corporate_link');
$corporate_name = get_sub_field('corporate_name');
if( $corporate_link != '' ){ ?>
<a href="<?php echo $corporate_link; ?>"
target="_blank"
title="<?php echo $corporate_name; ?>"><?php echo $corporate_name; ?></a>
<?php } else { ?>
<?php echo $corporate_name; ?>
<?php } ?>
use get_sub_field() and get_field() instead of the_sub_field() and the_field()
I'm filtering some content on my website via country specific code, I'm trying to add else statements so it doesn't have to run each piece as individual code except whatever I try gives an error:
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
The above gives the following error: Parse error: syntax error, unexpected T_ELSEIF in and refers to the first elseif
As you're combining PHP with direct HTML / OUTPUT. During your code you're printing whitespace in between the ending bracket and the elseif keyword.
PHP's Interpreter looks directly after the } for the elseif keyword but what it finds is a block of outputted data, so it raises an error.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
What you need to do is to remove the whitespace like so.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>
You will notice the lack of space OUTSIDE the PHP blocks.
This should resolve your problem.
Else cannot evaluate a condition, it will be executed if all other conditions are false, think of it in terms of the default statment of a switch.
This:
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Needs to be this:
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Or even better to use switch-case for many conditions.
For example:
switch($x) {
case 1:
?> some html <?php
break;
case 2:
?> more html <?php
break;
}
Instead of closing the tags you can echo/print the html.
This will work:
<?php if (function_exists('isCountryInFilter')) {
if(isCountryInFilter(array("us", "ca"))) {
?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }
}
?>
instead of that format use
<?php if($foo == 1): ?>
html code
<?php else if($foo == 2): ?>
other html
<?php else: ?>
more html code
<?php end; ?>
FrEaKmAn's answer will probably work, but I prefer the curly braces myself. Try changing
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
to
<?php } elseif(isCountryInFilter(array("au"))) { ?>
Also, in general, it's good to minimize the number of times you break in and out of php blocks...
(Once this piece is fixed, you'll need to deal with the problem pointed out by DamienL.)