Php Include in .css [closed] - php

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.

Related

CSS property not showing up for active navigation element using PHP

I know the title is kinda vague, sorry. I could'nt express it easily in one statement. So, anyways.
I'm currently working on my Personal Website. I wanted to have the nav element for the current page to be highlighted a certain color. To do so I wanted to use this:
<a href="index.php" class="nav-item" <?php if($current =='home'){echo 'class = "active"';}?>>Home</a>
and the same thing for the other pages. However, "class = "active" is not even being applied to the a tag. My Index pages contains this:
$current = "home";
and the css for the active class looks like this:
.active{
background-color: #fdfdfd !important;
color: #3D1c4B;
}
I seriously don't know what I'm doing wrong. Is there something I'm missing or is this just something I can't do with an a element?
Here is what the nav looks like
You can't have multiple class tags, you already have one you can't add a second. Try this:
Home
Your HTML is invalid, and would render: <a href="index.php" class="nav-item" class="active"> - which has two class attributes (not legal). Modify your PHP like below, to put both classes in the same attribute:
Home
You are writing class attribute two times in <a> tag
<a href="index.php" class="nav-item" <?php if($current =='home'){echo 'class = "active"';}?>>Home</a>
it should be like
<a href="index.php" class="nav-item <?php if($current =='home'){echo
' active'; }?>" >Home</a>
Get the class in a variable and use it like this:
<?php
$className = '';
if($current =='home'){
$className = 'active';
echo "<a href='index.php' class='nav-item " . $className . "'>Home</a>";
?>
First, I see your using:
<a href="index.php" class="nav-item" <?php if ( $current =='home' ) echo 'class = "active"'; ?> >Home</a>
is wrong, since you will get class="nav-item" class="active", - instead you need to write:
<a class="nav-item <?php if ( $current =='home') echo 'active'; ?>" href="index.php">Home</a>
your code will add 2 classes so that only one class will be applied to your tag.
try like this.
Home

PHP If Else with Navigation Menu [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 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>

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

Set active state on navigation dynamically

I seem to run into this problem frequently and can never find a solution. I have a Wordpress site with a top navigation. Since this is in my header.php, and used on all pages, I cannot hardcode my active menu state for each page.
How can I dynamically set the activate state to work for each page?
Here is my current nav code:
<nav id="main-menu" class="padbot">
<ul id="ce">
<li class="cemenu">About</li>
<li class="cemenu">Consulting</li>
<li class="cemenu">Intelligence</li>
<li class="cemenu">Academy</li>
<li class="cemenu">Blog</li>
<li class="cemenu">Contact</li>
</ul>
I've already setup a CSS class called "active" that has my active state properties. Ideally, what I'm looking for is when your on the "About" page (or any of the other pages), the class I created for the active state will be appended to the current li classes's.
Example:
<li class="cemenu active">About</li>
Thanks!
you could try something along the lines of
<li class="cemenu<?php echo ($_SERVER['PHP_SELF'] == '/about' ? ' active' : '');?>">About</li>
You can do this way:
This will add the active class to the <a> which contains the page from the url.
$(function(){
var url = window.location.href;
var page = url.substr(url.lastIndexOf('/')+1);
$('.cemenu a[href*="'+page+'"]').addClass('active');
});
and if you want to add class to its parent li the replace the last line to this and css class should be like this:
.active a{
css
properties
for active li's a
}
// using closest
$('.cemenu a[href*="'+page+'"]').closest('li').addClass('active');
or
// using parent
$('.cemenu a[href*="'+page+'"]').parent('li').addClass('active');
just tryout the fiddle here
First, there is a css pseudo class prepared for styling 'active' links :
a:active {css}
For your situation, you would have to add this class to your styling :
.active a, a:active {css}
But your needs seems more on the PHP side than the CSS, perhaps someone else will help you with that part. There would be a javascript solution with jQuery, finding the actual location then inject a css selector to the proper element.
Check this article and this other one about wordpress. It will help you.
Stack Overflow references :
How do I target each menu different items on active state in Wordpress
How to add Active states and icons in wordpress wp_nav_menu()
Loosing Nav Active State in Wordpress Dynamic Menu
google search
try something like this:
<?php $pages = array('about' => 'About Us', 'blog' => 'blog') ?>
<ul>
<?php foreach($pages as $url => $page): ?>
<?php $isActive = $_SERVER["REQUEST_URI"] == $url ?>
<li<?php echo $isActive ? ' class="active"' : '' ?>>
<?php echo $page ?>
</li>
<?php endforeach ?>
</ul>
It may be worth looking into using wordpres functions such as get_page_link which would be nicer than using the Server super global as that's not nice. This would also fail if you have wordpress in a folder and not the document root, it's just a simple example to get you started :)
You can try like this
<li class="<?php
if($this_page=='Home'){
echo 'active';
}
?>">
Home
</li>
<li class="<?php
if($this_page=='Contact'){
echo 'active';
}
?>">
Contact
</li>
And then in your home page
$this_page='Home';
And in your contact page
$this_page='Contact';
You could use preg_replace() to add class="active" like this:
ob_start();
echo '<ul>
<li>Page 1</li>
<li>Page 2</li>
</ul>';
$output = ob_get_clean();
$pattern = '~<li><a href="'.$url.'">~';
$replacement = '<li class="active"><a href="'.$url.'">';
echo preg_replace($pattern, $replacement, $output);

How to make selected link in Navigation highlighted when clicked [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I highlight a link based on the current page?
Context
I have been trying for ages to get a selected link in my Navigation menu to stay highlighted when clicked, but I cant find any decent understandable tutorials on this.
Questions
How can I resolve this?
Would jQuery or PHP be best for this?
Code
<ul>
<li>ABOUT US
<ul>
<li>Company</li>
<li>Team</li>
<li>Careers</li>
</ul>
</li>
<li>OUR WORK
<ul>
<li>Portfolio</li>
<li>Upcoming</li>
</ul>
</li>
<li>WHATS NEW
<ul>
<li>News</li>
<li>Blog</li>
</ul>
</li>
</ul>
Solution
The cleaner is to add a current class to selected link when you generate the page on the server side.
Example
I found this example which is close to your current architecture, according to the code that you gave us.
Source: http://www.finalwebsites.com/tutorials/css-navigation-bar.php
PHP
$items = array(
array('link'=>'scripts.html', 'label'=>'PHP scripts'),
array('link'=>'tutorials.html', 'label'=>'Tutorial'),
array('link'=>'template.html', 'label'=>'CSS template'),
array('link'=>'examples.html', 'label'=>'Code examples')
);
$menu = '<ul>';
foreach ($items as $val) {
$class = ($_SERVER['SCRIPT_NAME'] == $val['link']) ? ' class="current"' : '';
$menu .= sprintf('<li>%s</li>', $val['link'], $val['label']);
}
$menu .= '</ul>';
echo $menu;
CSS
ul li a.current { background-color: #40611F; color: #FFFFFF; }

Categories