Admin view only navigate bar PHP - php

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>';}
?>

Related

make button disabled based on if row column

I have some working PHP code and I have recently added a button that allows the user to download the image form the root directory, in the database we put the file name e.g. example.png / example.jpeg and when the user clicks download it opens the image in a new tab
what we need is: if the [proof] is= NULL , the download button disables, otherwise it will be enabled and they can click the button
<?php
// output data of each row
while($row=mysqli_fetch_assoc($designresult)) {
?>
<div class="card mb-4 box-shadow"><div class="card-header">
<h4 class="my-0 font-weight-normal">Job Reference: <?php echo $row["jobRef"]; ?></h4>
</div>
<div class="card-body">
<p><b>Company Name:</b><br> <?php echo $row["companyName"]; ?> </p>
<p><b>Requested:</b><br> <?php echo $row["dateReq"]; ?> </p>
<p><b>Request By:</b><br> <?php echo $row["yourName"]; ?> </p>
<p><b>Graphic Type:</b><br> <?php echo $row["graphicType"]; ?> </p>
<p><b>Double Sided:</b><br> <?php echo $row["doubleS"]; ?> </p>
<p><b>Design Info:</b><br> <?php echo $row["info"]; ?> </p>
<p><b>Purpose:</b><br> <?php echo $row["purpose"]; ?> </p>
<p><b>Proof:</b><br> <?php echo $row["approved"]; ?> </p>
<p><b>Proof Date:</b><br> <?php echo $row["appDate"]; ?> </p>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">Download</a>
</div>
</div>
<?php
}
?>
To my knowledge the link tag does not have a "disabled" attribute. But you can "disable" the link by removing the href attribute.
Something like this: It checks if $row['proof'] has some value (by negating the empty), then it prints out the href if result is true.
<a class="btn btn-success" target="_blank" <?php if(!empty($row['proof'])): ?> href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php endif; ?> >Download</a>
Or maybe better: Check if variable is empty and give the user a hint that it's not available. I think this is the better solution, because then your users will know what's going on.
<?php if(empty($row['proof'])): ?>
<span>No proof available</span>
<?php else: ?>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
<?php endif; ?>
<a
class="btn btn-success"
target="_blank"
<?php if(empty($row['proof'])) echo "disabled"; ?>
href="<?php if(!empty($row['proof'])) echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
try above code. and add disabled class using this condition
<?php
$state = (empty($row['proof'])) ? "disabled='disabled'" : "";
$class = (empty($row['proof'])) ? "disabled" : "";
?>
<a class="btn btn-success <?php echo $class; ?>" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php echo $state; ?>>Download</a>
To disabled the button, you need to use disabled HTML attribute. The code above checks $row['proof'] == NULL. If this statement is true it prints disabled = "disabled" in the button element and it isn't true, it prints nothing.
Assuming that you are using bootstrap, .disabled will grayed out the button.

How do I add a button after a if(isset)?

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; ?>

Clicking view button and nothing happens

After clicking the VIEW button and nothing happening, I started to wonder what's wrong?
GALLERIES NAME POST EDIT DELETE
Slideshow VIEW
SERVICES POST VIEW
Code:
<?php foreach ($gallery as $gallery_item): ?>
<tr>
<td><?php echo $gallery_item['galleries_name']; ?></td>
<td>
<?php if( $gallery_item['galleries_post_type'] == 'post') { echo "#"; }?>
</td>
<td>
<button type="button" class="edit" onclick="location.href = '
<?php
if( $gallery_item['galleries_post_type'] == 'post') {
echo site_url('cpages/galleries/'.$gallery_item['galleries_id']);
} else {
echo site_url('cpages/viewpictures/'.$gallery_item['galleries_id']);
}
?>
';">VIEW</button>
</td>
I think you should use links and describe them as button (role, type, whatsoever) and decore them with CSS. That is:
<?php
$id = $gallery_item['galleries_id']);
?>
<td>
<?php if ($gallery_item['galleries_post_type'] === 'post'): ?>
<a href="<?= site_url('cpages/galleries/'.$id); ?>" class="edit">
<?php elseif (): ?>
<a href="<?= site_url('cpages/viewpictures/'.$id); ?>" class="edit">
<?php endif; ?>
</td>
By searching for a better solution, I found an answer from BalusC that, I think, should solve your problem and help you understand how/what/when questions about buttons, links and so one.

Conditional Login Link - Wordpress - PHP

Thanks in advanced for any help provided and taking the time to review my question. I basically just have a Wordpress site that once you login, you see your name and avatar in the top right corner and when you hover over your name an "Edit Profile" and "Log out" link appear. All I want is for a "Login" link to be in that same spot when you're not logged in. (This saves me from having to put a login widget in the sidebar).
I was able to track down the PHP file that holds that specific piece of code and find the spot where the user menu is setup. I'm just not sure what piece of code I could add so that there's a login link there that leads to mysite.com/login/. Here is the current code:
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
Here's an image to show you what I mean as well. Top is logged out, bottom logged in.
http://6chill.com/loli.jpg
Thanks again for any help that you're able to provide and if you require further details just let me know and I'll do my best to provide them.
Thanks for the prompt reply everyone :) As requested, here's more of the code to ensure nothing was left out.
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php }?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>
I hope that's what you're looking for. Where you see the comment "//submit menu", that's where the "Submit Video" button comes into play. Thanks so much again guys, you're freaking awesome!
Please try this code, I think this will help you
<?php
if ( is_user_logged_in() )
{ ?>
LOGOUT
<!-- add another action as per your requirement-->
<?php } else
{ ?>
<a class="login" href="<?php echo site_url(); ?>/login/">LOGIN</a>
<! -- give link to your login page -->
<?php }
put this code in header file.
You need to add an else to the if statement that checks if the user is logged in. If they aren't logged then you output a link to the login page. You may need to tweak the CSS or the classes on the menu item for the login link.
<?php }
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php } else { ?>
<li class="menu-item current_us">
Login
</li>
<?php } ?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>

Compare a variable against more than one (an array of) variables, with the same outcome, in PHP

I have a simple navigation which uses php to know which "current page" you are on allowing my css to indicate this.
my php code:
<?php echo "\n"; if ($currentPage == 'about.php') { ?>
What I'm trying to do is have this button active as the current page within its child pages?
is there any way of having multiple pages within the above code?
I tried this but it doesn't work
<?php echo "\n"; if ($currentPage == 'about.php about2.php about3.php') { ?>
You can use in_array:
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
Which will basically go through an array and compare the value ($currentPage) with each of those in the array.
You could use the in_array() function : http://php.net/manual/en/function.in-array.php
if (in_array($currentPage, array('about.php', 'about2.php'))) {
// Do Something
}
(NOT AN ANSWER JUST AN UPDATE)
This is what I have at the moment: (only the about.php activates the switch)
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="about.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</a>
</li>
<?php } ?>
If I change the pages to home.php, contact.php (which were in the navigation originally) they all work turning them all on at the same time?
So im not sure what I need to do in order to include my new pages?
I this is one of my other buttons:
<?php echo "\n"; if ($currentPage == 'contact.php') { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="contact.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</a>
</li>
<?php } ?>
Is this why it is picking up the original pages but not my new ones?

Categories