I tried to show a login/logout link in my header based on the value in session. i tried some thing like this
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{?>
<li>Logout</li>
<?php}
else
{?>
<li>Login</li>
<?php } ?>
</ul>
then both links didn't appeare in the header(login/logout). then after a lot of trying i came up with this code
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{
?>
<li>Logout</li>
<?php
}
else
{
?>
<li>Login</li>
<?php
}
?>
</ul>
the code is actually same but i have added some spaces between the curly brackets'{'. And it works as i intended. Is space an issue when we use html and yii2 code combined?
This has nothing to do with Yii, it is simply a php syntax problem (you should always have a space after <?php)...
If you want to mix condition and html output, and have a better readability, you should use this :
<?php if ($user_id != null) : ?>
Output 1
<?php else : ?>
Output 2
<?php endif; ?>
Read more : http://php.net/manual/en/control-structures.alternative-syntax.php
There HAS to be a space after the opening <?php tag, which means <?php} is not valid:
without space:
$ cat z.php
<?php if(true) {?>
true
<?php} else {?> // note, no space after <?php
false
<?php }?>
$ php z.php
true
<?php} else {?>
false
with space:
$ cat y.php
<?php if(true) {?>
true
<?php } else {?>
false
<?php }?>
$ php y.php
true
Note the difference in output. This has nothign to do with Yii, and everything to do with your core PHP coding.
Related
I have two users one is 'admin' and 'user'. I need to when I login as a 'user' hide the some url.
my add_sales.php file include some code
<div class="side-menu fl">
<h3>Sales Management</h3>
<ul>
<?php if($_SESSION['usertype'] == 'admin' ) { ?>
<li>Add Sales</li>
<li>View Sales</li>
<?php } ?>
<?php else if($_SESSION['usertype'] == 'user' ) { ?>
<li>Add Sales</li>
<?php } ?>
</ul>
</div> <!-- end side-menu -->
when I access as a 'user' I use this code for use user to if($_SESSION[usertype']=='user') I don't know this is right o wrong please can some one help
me..
my cheacklog.php is
if($count==1){
$row = mysql_fetch_row($result);
$_SESSION['id']=$row[0];
$_SESSION['username']=$row[1];
$_SESSION['usertype']=$row[3];
if($row[3]=="admin"){
header("location:dashboard.php");
}
else if($row[3]=="user")
{
header("location:dashboard.php");
}
}
?>
You can write it in line. This link will be inserted into the page only if the user has admin status:
<ul>
<li>All users will see this link</li>
<?php
if ($row[3]=="admin") {
echo "<li><a href='adminStuff.php'>hidden link</a></a>";
}
?>
</ul>
I am pretty sure that is what you meant, or if you want to do conditional redirects, I think you already have that done.
Try this :
<?php if ($_SESSION['userType'] == 'admin'): ?>
<a>Only admin can see this. </a>
<?php else if($_SESSION['userType'] == 'user' ): ?>
<a>Only user can see this</a>
<?php endif ?>
My code doesnt work properly and it does now shows any error or any clue, what am I missing here ? any hints ? TIA
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php } ?>
<?php
elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
One unnecessary <?php and else if is in wrong way, do like below:-
<?php if(isset($_SESSION['user_session_organizer'])){ ?>
<li>VIEW TRIPS</li>
<?php } elseif(isset($_SESSION['user_session'])){?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
Note:-
Always add
error_reporting(E_ALL);ini_set('display_errors',1);
on top of your each php page to prevent yourself from a situation like:-"White Screen Of Death"
(It will on the error reporting setting for all type of errors and every error will displayed on the page if any occur).
Thanks
Try this, your else if is wrong ?> is a close tag and then the compiler dont understand the elseif, better this:
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php }elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
This is easier to read and to debug:
<?php if (isset($_SESSION['user_session_organizer'])) : ?>
<li>
VIEW TRIPS
</li>
<?php elseif (isset($_SESSION['user_session'])) : ?>
<li>
VIEW TRIPS
</li>
<?php else : ?>
<li>
<a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a>
</li>
<?php endif; ?>
Check about control structures.
try this, its neat and simple code.
<?php if (isset($_SESSION['user_session_organizer'])): ?>
<li>VIEW TRIPS</li>
<?php elseif (isset($_SESSION['user_session'])): ?>
<li>VIEW TRIPS</li>
<?php else: ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php endif; ?>
So i have this in html
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
</ul>
And i want to add this php code in the list:
<?php
...
elseif($_SESSION['logged']==false)
echo 'Login</li>';
echo 'Register</li>';
?>
But instead of displaying 2 separate list items, one with Login and one with Register i only get one with those two links.
What can I do?
You missed starting li tags and brackets.
do this:
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
If $_SESSION['logged'] is going to be not set or false then use empty(), also your missed you opening li tags:
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
<?php if(empty($_SESSION['logged'])): ?>
<li>Login</li>
<li>Register</li>
<?php endif; ?>
</ul>
You have forgotteh curly bracket { and li
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
I am trying to learn PHP for a website that I am building. In CSS, I have a class, nav a.thispage, setup to make a 'button' on the navigation be the same color as the highlight. It works beautifully. But, as I added pages, I find that I needed to constantly update all of the HTML files of the site, over, and over again. I found out that PHP could help me to automate this. I use the following PHP, in my HTML to do this:
<?php include 'content/header.php';?>
The header.php file has the following content:
<header>
<h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
<nav>
<ul>
<li>Home</li>
<li>Events</li>
<li>Social</li>
<li>About</li>
<li>Contact</li>
<li>Area Game Stores</li>
<li>PFS</li>
</ul>
</nav>
</header>
Now, because I am using this method, I can't just set the class="thispage" on the a tag. Is there a way to set the class, dynamically, with PHP? If so, how to I tell if the page loading the html is actually the page that needs it? Is using PHP even the correct way to handle this, or should I be using JavaScript?
I know this is a lot, and I didn't really provide a lot of what I have done, but I can't actually seem to see what I need to do for this. All I really need is a point in the right direction, rather than a full code sample.
Thank you for any help you can provide.
Use basename($_SERVER['REQUEST_URI'])
<header>
<h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
<nav>
<ul>
<?php $basename = basename($_SERVER['REQUEST_URI']);?>
$class = $basename === 'index.php' || empty($basename) ? ' class="thispage"' : '';
<li <?php if($basename=="index.php" || $basename==""){?> class="thispage" <?php } ?>>Home</li>
<li <?php if($basename=="events.php"){?> class="thispage" <?php } ?>>Events</li>
<li <?php if($basename=="social.php"){?> class="thispage" <?php } ?>>Social</li>
<li <?php if($basename=="about.php"){?> class="thispage" <?php } ?>>About</li>
<li <?php if($basename=="contact.php"){?> class="thispage" <?php } ?>>Contact</li>
<li <?php if($basename=="gamestores.php"){?> class="thispage" <?php } ?>>Area Game Stores</li>
<li>PFS</li>
</ul>
</nav>
</header>
Updated another Simple way
<header>
<h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
<nav>
<ul>
<?php $basename = basename($_SERVER['REQUEST_URI']);?>
$class = $basename === 'index.php' || empty($basename) ? ' class="thispage"' : '';
<li <?= $class ?>>Home</li>
<li <?= $class ?>>Events</li>
<li <?= $class ?>>Social</li>
<li <?= $class ?>>About</li>
<li <?= $class ?>>Contact</li>
<li <<?= $class ?>>Area Game Stores</li>
<li>PFS</li>
</ul>
</nav>
</header
You can use a bit oF PHP and JavaScript to handle this
<header>
<h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
<nav>
<ul>
<li><a id="aHome" href="index.php">Home</a></li>
<li><a id="aEvents" href="events.php">Events</a></li>
<li><a id="aSocial" href="social.php">Social</a></li>
<li><a id="aAbout" href="about.php">About</a></li>
<li><a id="aContact" href="contact.php">Contact</a></li>
<li><a id="aGame" href="gamestores.php">Area Game Stores</a></li>
<li><a id="aPFS" href="http://someaddress" target="_blank">PFS</a></li>
</ul>
</nav>
</header>
Now suppose the visitor has landed on the About page. The code to check that and set the class name on the a tag would be
<?php
if ($_SERVER['SCRIPT_NAME'] == '/about.php') {
?>
<script type="text/javascript">
document.getElementById("aAbout").className = 'thispage';
</script>
<?php
}
?>
Try it and let me know.
In order for PHP to help you, you'd have to generate your navigation within a loop. Something along the lines of:
<?php
$menu = array(
'Home' => 'index.php',
'Events' => 'events.php',
'Social' => 'social.php',
'About' => 'about.php'
);
?>
<?php foreach ($menu as $name => $href) : ?>
<?php $class_name = basename(__FILE__) === $href ? 'thispage' : ''; ?>
<li><?php echo $name; ?></li>
<?php endforeach; ?>
So I have a menu in a php file that looks like this (This is the whole file. I'm totally new to PHP.)
menu.php:
<li id="current"><span>Home</span></li>
<li><span>Blog</span></li>
<li><span>Results</span></li>
<li><span>Pictures</span></li>
<li><span>Our Location</span></li>
Now in my pages I do this (index.php):
<div id="tabs1" >
<ul>
<!-- CSS Tabs -->
<?php include("menu.php"); ?>
</ul>
</div>
So what I want to be able to do is change the line above to this:
<?php include("menu.php?current=pictures"); ?>
Which would make the active tab the Pictures tab. How can I do this?
You could also try this:
Your php script
<?php
$selected = "pictures";
$current_id = ' id="current"';
include "menu.php";
?>
this is your menu:
<ul>
<li <?php if ($selected == "pictures") print $current_id; ?>><span>Home</span></li>
<li <?php if ($selected == "blog") print $current_id; ?>><span>Blog</span></li>
<li <?php if ($selected == "home") print $current_id; ?>><span>Results</span></li>
<li <?php if ($selected == "me") print $current_id; ?>><span>Pictures</span></li>
<li <?php if ($selected == "contacts") print $current_id; ?>><span>Our Location</span></li>
</ul>
Try this:
<li <?php if($_GET['current'] == 'home') {echo 'id="current"'}?>><span>Home</span></li>
<li <?php if($_GET['current'] == 'blog') {echo 'id="current"'}?>><span>Blog</span></li>
<li <?php if($_GET['current'] == 'results') {echo 'id="current"'}?>><span>Results</span></li></li>
and so on....
worth looking at
intelligent navigation
<nav>
<style>
#active{
color:#FFC801;
}
</style>
<?php
$activePage = basename($_SERVER['PHP_SELF'], ".php");
?>
<ul>
<li>About Us</li>
<li>Mentors</li>
<li>Tours</li>
<li>Animation</li>
<li>Blog</li>
<li>Testimonials</li>
<li>Press/Media</li>
<li>Facts</li>
</ul>
</nav>
I don't think its necessary for it to be done at the server side (using up CPU cycles).
Use javascript/CSS to achieve this.