PHP If Else with Navigation Menu [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to create an if else statement where if the "if" part is true, a menu icon is displayed that says "Registration" and takes the user to a registration page. If the "else" part is true, the menu will display the user's name that is registered. I have attached basically how I would like it to look, I just do not know how to incorporate my div into a PHP if else statement.
<?PHP
session_start();
if ($_SESSION['email'] == '')
{
"<div id='cssmenu'>
<ul>
<li>
Registration
</li>
</ul>
</div>"
}
else
{
"<div id='cssmenu'>
<ul>
<li>
<a> "Greetings " . $_SESSION['fname'] </a>
</li>
</ul>
</div>"
}
?>

The following should do the trick. As mentioned by #Ghost, you need echo and you needed to change a few quotes to single quotes.
<?PHP
session_start();
if ($_SESSION['email'] == '')
{
echo "<div id='cssmenu'>
<ul>
<li>
<a href='registration.php'> Registration</a>
</li>
</ul>
</div>";
}
else
{
echo "<div id='cssmenu'>
<ul>
<li>
Greetings " . $_SESSION['fname'] . " </a>
</li>
</ul>
</div>";
}
?>

you can use php whitin html
the php code is only considered between php tags so anything else is put directly to browser
as i can see only one part of your html changes so a better design would be
<?php session_start(); ?>
<div id="cssmenu">
<ul>
<li>
<?php if ($_Session['email']=='') {
?>
Registration
<?php }else{ ?>
Greetings -> <?php echo $_SESSION['fname']; ?>
<?php }?>
</li>
</ul>
</div>

Related

How to display php variable values in html <li> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have got multiple php variables that I want to display in html <ul list. The variables being displayed but not how I want it to be. It seems like one <li is inside of <ul correctly but, the other one is outside of <ul in it's own block and it's own <li even when I am displaying all of the variables inside of single <ul. Also the styling is only being applied to only the first <li element and not to the others.
Here is my html code
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<p> <?php echo $description; ?> </p>
</ul>
</div>
The source code from page source is
<div class="product-info-details">
<div>
<ul class="product-info-list">
<li> 18" x 10" x 6".</li>
</ul></li>
<li> Burlap,Nylon,Polyester </li>
<p> <p>With the Fusion Backpack strapped on, every trek is an adventure - even a bus ride to work </p>
</ul>
</div>
and the output
The html structure that is getting created after doing what I am doing is,
The page element cant be where you have it. The <p> can not be between the </li> and </ul>. I can not find a suitable authoritative reference right now, but I have made the same mistake myself.
So I have shifted it inside the <li>..</li>
Try this.
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?>
<p> <?php echo $description; ?> </p></li>
</ul>
</div>
Or maybe you intended this. I have changed the <p> to a list element
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<li> <?php echo $description; ?> </li>
</ul>
</div>

How to Include PHP code in between HTML tags? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
currently I'm developing a application using Codeigniter framework and PHP. In there I need to include below mentioned PHP code in between HTML tags.
Can anyone provide a solution or an guidance for this?
PHP code :
if(isset($_SESSION['email']))
{
echo '<a href="http://localhost/ci/myads_view">';
echo 'MY ADS' ;
echo '</a>';
}
HTML code:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"/>
</nav>
You can come in and out of PHP at will:
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } ?>
In the above, if the PHP if statement evaluates to true, the HTML within the conditional will execute, otherwise it will not.
In your particular case, I can only assume this is what you're trying to do:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<li>
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } else { ?>
OTHER LINK
<?php } ?>
</li>
</ul>
</nav>
Or something along the lines of the above?
PHP is pretty cool, you can stop/resume it mid-document.
For example like this, I hope this is the solution to what you were trying to do:
<?php
if(isset($_SESSION['email']))
{ /* this will temporarily "stop" php --> */ ?>
<a href="http://localhost/ci/myads_view">
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
</a>
<?php /* <-- php resumes now */
}
?>
So what's going on?
First, you write your IF statement and the curly brackets, the code inside of which would usually be executed if the IF statement evaluates to TRUE.
<?php
if(something==true)
{
echo "write something to the page";
}
?>
But instead of using PHP commands, you quickly jump out of PHP by closing the PHP tag ?> as you would do at the end of your PHP code anyway.
<?php
if(something==true)
{
?>
Now you can continue in plain HTML without all the usual related issues of PHP commands as escaping characters like ' and " and \ watching your "qoute level" and so on. Instead, you write bog standard HTML.
When you're done entering the HTML that should be output in case the IF statement is true, just open another PHP tag and close the IF statement's curly brackets and, if that's the end of your PHP, close the PHP tag as well.
<?php
}
?>
What you have done is correct, additionally you can write as follows:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str;
Reference - http://php.net/manual/en/language.types.string.php
To put the link inside the navigation, you can do it like this, all inside the same PHP file.
<?php
if(isset($_SESSION['email'])) {
$navLink= 'MY ADS';
}
?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"><?php echo($navLink); ?>
This should work:
<?php if(isset($_SESSION['email'])): ?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<?php endif; ?>

Error code in php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So on my website im making it where if a member is not sign in they can't see username My account or settings link
this is what i have so far
<?php
if(empty($_SESSION['user'])){
echo "<li>
Register Account
</li>
<li>
Login Account
</li>";
} else {
echo "<li class='dropdown'>";
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'><?php echo $_SESSION['user']['username'];?></span>
<b class='caret'></b>
</a>";
echo "<ul class='dropdown-menu'>
<li><i class='icon-user'></i> My Profile</li>
<li><i class='icon-tasks'></i> My Tasks</li>
<li><i class='icon-calendar'></i> Calendar</li>
<li class='divider'></li>
<li><i class='icon-key'></i> Log Out</li>
</ul>
</li>";
}
?>
This is the error code i receive
Error i get is Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/index.php on line 92
You have a echo statement in another echo statement!
So change this:
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'><?php echo $_SESSION['user']['username'];?></span>
<b class='caret'></b>
</a>";
To this:
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'>" . $_SESSION['user']['username'] . "</span>
<b class='caret'></b>
</a>";

Code not working in PHP if else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to create a nav bar where if you are a guest it displays a lock icon and 'Log in' and when you are logged in it displays a lock icon and 'Logout'.
Here is the code for my header:
<section class="top-bar-section">
<ul class="left">
<li><img src="images/icons/home_icon.png"> Home</li>
<li><img src="images/icons/about_icon.png"> About</li>
<li><img src="images/icons/pages_icon.png"> Showcase</li>
<li><img src="images/icons/videos_icon.png"> Videos</li>
</ul>
<ul class="right">
<?php
if (!empty($UserName))
{
echo <li><img src="../images/icons/login_icon.png"> Log in</li>;
}
else
{
echo <li><img src="../images/icons/login_icon.png"> Logout</li>;
}
?>
</ul>
</section>
the section I am working on is under <ul class="right">
and the error I am getting when testing the webpage is
`Parse error: syntax error, unexpected '<' in
C:\wamp\www...\header.php on line 16
PS: Line 16 is where the first 'echo' is
HELP! Please :) Thanks
You need to put the string in the echo statement in quotes (I'd suggest single-quotes, since you've got doubles in the text).
try like this you are missing quote on echo
if (!empty($UserName)) {
echo '<li><img src="../images/icons/login_icon.png"> Log in</li>';
}
else {
echo '<li><img src="../images/icons/login_icon.png"> Logout</li>';
}
The echo requires string like this
echo "Hello World";
So your echo statements must be like this:
echo '<li><img src="../images/icons/login_icon.png"> Log in</li>';
echo '<li><img src="../images/icons/login_icon.png"> Logout</li>';
If you want to use double quotes you should escape quotes that are in the string
echo "\""; /* would echo " */

Php Include in .css [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a menu file "menu_include.php" thats going to be included in 6 pages. I want each element to change per page. Lets say we are in "about.php" i want the "about" to change colors with no links. How can i do this
A simple way to do this would be to set a variable before including your menu, something like this.
$currentPage = "about"; // change this on each page.
include 'menu_include.php';
then in your menu_include, you could do something like this with each link
<nav role='navigation'>
<ul>
<li><a href="#" <?php if(isset($currentPage) && $currentPage == 'home'){ echo ' class="active" '; } ?> >Home</a></li>
<li><a href="#" <?php if(isset($currentPage) && $currentPage == 'about'){ echo ' class="active" '; } ?> >About</a></li>
<li><a href="#" <?php if(isset($currentPage) && $currentPage == 'clients'){ echo ' class="active" '; } ?> >Clients</a></li>
<li><a href="#" <?php if(isset($currentPage) && $currentPage == 'contact'){ echo ' class="active" '; } ?> >Contact Us</a></li>
</ul>
</nav>
Then you could style the .active class to change colors as you wish..
.active {
background:lime;
}
Edit: To answer your question, yes you will want to wrap the variable and include in php tags like this.. I didn't put them before, because I assumed you would just put them in your existing php tags.
<?php
$currentPage = "about"; // change this on each page.
include 'menu_include.php';
?>
It sounds like you might want to learn a bit more about php, or even look into using a popular CMS like Wordpress -- http://wordpress.org there are plenty of tutorials/videos that can help you, but unfortunately I can't write ALL of the code for you ;) Trying to help as much as possible.
Update: In response to your last question you can add a hover state to your .onthepage class.
.menu > li > a.onthepage:hover {
color:#f00;
font-weight:bold;
}
This way it won't change when you hover it, I expect that is what you're going for.

Categories