I got some html code
<?php
if($loggedIn){
echo "<div id='navigation'>
<ul>
<li>
<a href='javascript:toggleLayer('video');'>Global</a>
</li>
<li>
<a href='javascript:toggleLayer('info');'>About</a>
</li>
<li>
<a href='javascript:toggleLayer('register_main');'>Join!</a>
</li>
</ul>
</div>"};
But when I click on the url's on the website, they happen not to execute the javascript code.
after inspectiong one of the elements I found out that
<a video');'="" href="javascript:toggleLayer(">Global</a>
is the code generated, which is wrong ofcourse.
My only clue would be that it should be
Global
Though i have no clue how to fix that inside the echo, because it has to be in between the if-statement
Thanks in advance!
Replace:
<?php if($loggedIn){
echo "<div id='navigation'>
<ul>
<li>
<a href='javascript:toggleLayer('video');'>Global</a>
</li>
<li>
<a href='javascript:toggleLayer('info');'>About</a>
</li>
<li>
<a href='javascript:toggleLayer('register_main');'>Join!</a>
</li>
</ul>
</div>"}; ?>
with:
<?php if($loggedIn){ ?>
<div id="navigation">
<ul>
<li>
Global
</li>
<li>
About
</li>
<li>
Join!
</li>
</ul>
</div>
<?php } ?>
When using quotes inside of strings " I said "Hello" ". You need to escape the matching surrounding quotes like: " I said \"Hello\" ".
Try this
...
<a href='javascript:toggleLayer(\"video\");'>Global</a>
...
The problem are your quotation marks.
You have to escape the ones inside the <a> tags where you set the params for your javascript function e.g. 'video'
Related
I am working on a PHP website. And for some reasons, I need to add multiple level dropdown in my navigation bar. I tried the bootstrap class="dropdown-submenu" but I don't know why it has not worked.
<ul class="dropdown-menu">
<?php
$alllinks = mysql_query("SELECT `cid`, `cname` FROM `services` WHERE `parentid`=0");
while($reslink = mysql_fetch_assoc($alllinks)){ ?>
<li class="dropdown-submenu">
<a tabindex="-1" href="<?php echo MYWEBSITE;?>services/<?php echo to_prety_url($reslink['cname']).'-'.$reslink['cid'];?>.html">
<?php echo $reslink['cname'];?>
</a>
<ul class="dropdown-menu">
<li>
<a href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<?php echo $rowsb['cname'];?>
</a>
</li><br>
</ul>
</li><br>
<?php } ?>
</li>
</ul>
Throughout your code You haven't enclosed MYWEBSITE in inverted comma's
you have written.
<?php echo MYWEBSITE;?>
whereas you should write
<?php echo "MYWEBSITE";?>
and You are using the old and Deprecated mysql connector it is higly recommended that you switch to PDO or mysqli.
I'm trying to make an active navigation bar using PHP where the current page will be highlighted or colored in the navigation bar. The code I used:
<ul class="topmenu">
<li <?php if($_GET['page']="home") { ?> class="active" <?php } ?>>
<b>Bienvenue</b>
</li>
<li <?php if($_GET['page']="livres") { ?> class="active" <?php } ?>>
<b>Livres</b>
</li>
<li <?php if($_GET['page']="bibliotheque") { ?> class="active" <?php } ?>>
<b>Bibliothèque</b>
</li>
<li <?php if($_GET['page']="universite") { ?> class="active" <?php } ?>>
<b>L'université</b>
</li>
<li <?php if($_GET['page']="contact") { ?> class="active" <?php } ?>>
<b>Contact</b>
</li>
</ul>
The code will put the attribut in the class active if the page is the current page in the navigation bar. However, all the attributs will be given the class active in this case. How do I fix this?
P.S: I am not looking for any JS or jQuery alternatives, I'm trying to make this work with PHP only.
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><b>Bienvenue</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><b>Livres</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><b>Bibliothèque</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><b>L'université</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><b>Contact</b></li>
</ul>
I don't use PHP, but give the current page the active tag, and change it per file. Then a single class definition in the CSS handles changing the color for each page.
HTML:
<nav>
<a class="active" href="/home/">Home</a>
Calendar
Community
</nav>
CSS:
.active {
background-color: #4CAF50;
}
Declare page variables at the very top of each individual page.
Example: <? $page="home";?>
For each list item in your nav bar add if statment with corresponding variable and active class.
Example: <li class="<?if($page=="home"){?>active<?}?>"><b>Bienvenue</b></li>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I'm using bootstrap 4 class. I've achieved Active Class selection something like this.
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile1.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile1.php">Home
</a>
</li>
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile2.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile2.php">Home
</a>
</li>
Repeat this logic for further li tags. Hope this helps someone else.
The code work correctly if you use "==" instead of "=" in The if construct
Еxample:
if($_GET['page'] **==** "home")...
Instead:
if($_GET['page'] **=** "home")...
Hope this helps someone else...
I want to change the output of the Drupal 7 menu structure which is like:
<?php print render($primary_nav); ?>
outputs:
<ul class="menu nav navbar-nav">
<li class="first expanded">
<a title="" href="lorem">Lorem</a>
<ul class="menu nav">
<li class="first leaf"><a title="" href="/lorem">Lorem</a></li>
<li class="leaf"><a title="" href="/ipsum">Ipsum</a></li>
</ul>
</li>
<li class="leaf"><a title="" href="/dolor">Dolor</a></li>
<li class="expanded"><a title="" href="/sit">Sit</a>
<ul class="menu nav">
<li class="first leaf"><a title="" href="/sit">Amet</a></li>
<li class="leaf"><a title="" href="/consectetur">Consectetur</a></li>
</ul>
</li>
</ul>
how could I have instead of the ul and li the menu rendered with divs.
Thanks
I feel curious... why do you want to do so?
I guess you should try to make a modified copy of 'render' function and call it instead.
EDIT
The print "function" in PHP just outputs the argument received. I guess you should define your own php function, namely print_menu_as_div:
function print_menu_as_div($primary_nav) {
$htmlCode = '<div class="myOwnMenuClass">';
...
/*Generate your HTML code here to display your
menu items and append to $htmlCode*/
...
$htmlCode .= '</div>';
return $htmlCode;
}
Then instad of:
<?php print render($primary_nav); ?>
write:
<?php print print_menu_as_div($primary_nav); ?>
Hope it helps ;)
I have PHP code which will check if the user is logged in and will return the menu if they are, however I was wondering if there was a way to make each of the current selected highlighted or would I have to go through and add them as a manual list to each page?
The code is:
<?php
if (!securePage($_SERVER['PHP_SELF'])){die();}
//Links for logged in user
if(isUserLoggedIn()) {
echo "<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>
<div id='button1'>
<a href='/Demos.php'>Demos</a></div>
<div id='button2'>
<a href='/Helpfiles.php'>Helpfiles</a></div>
<div id='greeting'>
Hello, $loggedInUser->displayname.</br>";
}
//Links for users not logged in
else{
echo "<div id='Default'>
<ul>
<li><a href='/login.php'>Login</a></li>
<li><a href='/register.php'>Register</a></li>
<li><a href='/forgot-password.php'>Forgot Password</a></li>";
echo "</ul></div>";
}
?>
Now I know that on a normal CSS one it would just be .current and you can do it that way, however I cannot make that work with this echo because they are all on the screen at the same time. What would be the best way? Manually add would see like the longer way.
p.s. this is used in conjunction with usercake
You don't need to put HTML into an echo in PHP. I would recommend something like this.
So you will end up with something like:
<?php
if(isUserLoggedIn()) {
?>
<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul>
</div>
<div id='greeting'>
Hello, <?php echo $loggedInUser->displayname; ?>
</br>
<?php } ?>
Then I would not recommend you to add the class with PHP because you will suffer from lisibility with a lots of if and else cases.
The best way to do this would be to use ID/Classes for your LIs and add the selected class to a specitic item with a simple JavaScript function.
Btw, if you really feel the needs to have this in PHP I recommend you to read this:
http://www.catswhocode.com/blog/snippets/highlight-current-menu-item-in-php
http://webdeveloperswall.com/php/how-to-highlight-the-current-page-in-menu-in-php
So you will have something like:
<?php if(isUserLoggedIn()) { ?>
<ul>
<?php
$url = $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$page_name = basename($parts['path']);
?>
<li><a class="<?php echo ($page_name=='acount.php')?'selected':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='user_settings.php')?'selected':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='logout.php')?'selected':'';?>" href="contact.php">CONTACT US</a></li>
</ul>
<?php } ?>
EDIT
Finally, you should end up with something like this: http://pastebin.com/V8jxwi7T
You could grab the page you are currently browsing and see if it matches. Something like this perhaps? (sorry for the crude example)
<?php
// will return 'home' is the filename is home.php
$filename = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
?>
<li <?php if ($filename == "home") { echo "class='active'"; } ?>>
Home
</li>
Some info on pathinfo and how it works here
Just add class to selected <li> element, this way:
echo "<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li class="selected"><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";
In your CSS, put appropriate style for .selected class.
You should have a variable telling in which page you are, let's say it's $currentpage.
echo "<div id='Default'>
<ul>
<li".($currentpage=='account' ? ' class="selected"' : '')."><a href='/account.php' >Account Home</a></li>
<li".($currentpage=='usersettings' ? ' class="selected"' : '')."><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";
I know this question comes across a lot, but I just can't figure out how to do this using the, already answered posts.
I have a header with navigation links. I would like to add class="active" to the link that's active at the moment.
How could I do this if I have the following navigation?
<nav>
<ul id="main_nav">
<li class="home">
<a href="search">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
</li>
<li class="categorie">
<a href="categorieen/all">
<i class="icon-list-ul"></i>
<span>CATEGORIE</span>
</a>
</li>
<li class="aanbieding">
<a href="aanbiedingen">
<i class="icon-shopping-cart"></i>
<span>AANBIEDING</span>
</a>
</li>
<li class="vacature">
<a href="vacatures">
<i class="icon-copy"></i>
<span>VACATURE</span>
</a>
</li>
<li class="agenda">
<a href="agenda">
<i class="icon-calendar"></i>
<span>AGENDA</span>
</a>
</li>
<li class="contact">
<a href="contact">
<i class="icon-envelope"></i>
<span>CONTACT</span>
</a>
</li>
</ul>
</nav>
I tried this, but it did not work:
<script>
$(function() {
var href = $(this).find('a').attr('href');
alert(window.location.pathname)
if (href === window.location.pathname) {
$(this).addClass('active');
}
});
</script>
Maybe there's a better Codeigniter-ish way?
try this one.i think no need of javascript or jquery.
If you are using codeigniter then you can use URI Class.
<li class="home">
<a class="<?php if($this->uri->segment(1)=="search"){echo "active";}?>" href="<?=base_url('search')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
</li>
please let me know if you face any problem
I created a helper and saved it into the helper directory named as "menu_helper.php":
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('active_link')) {
function activate_menu($controller) {
// Getting the class instance.
$ci = get_instance();
// Getting the router class to actived it.
$class = $ci->router->fetch_class();
return ($class == $controller) ? 'active' : '';
}
}
Then in config/autoload.php, I added "menu" as a helper on line 91.
The last step is to put the code for print the "active" class when accessing the page (i.e. Login Page):
<li class="<?php echo activate_menu('login'); ?>">
<?php echo anchor('login', 'Login'); ?>
</li>
if (href === window.location.pathname) {
$('a[href='+ href +']').addClass('active');
}
Try this one:
<a class="<?=(current_url()==base_url('search')) ? 'active':''?>" href="<?=base_url('search')?>">