hope you fine and well,
i have a navigation bar like this :
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
how i can show this bar in all my html and php pages !
regards.
Put the render code in a my_navbar.php
echo '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>';
and then in all php file you need the navbar put
<?php
include_once "my_navbar.php"; // this will include a.php
?>
Use include_once for avoiding multiple include
To expand on scaisEdge's comment, here's how you can do this in an include.
In nav.php (name it however you want):
<?php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
?>
Then where you want the navigation:
<?php include('nav.php'); ?>
You can use include(), i.e.:
navigation.php
<?php
echo <<< EOF
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
EOF;
anotherfile.php
<?php
include("navigation.php");
//the rest of the code...
include vs include_once:
The include_once and require_once functions are slower than include
and require, simply because they keep track of the files that have
already been included, to avoid including them more than once.
Related
I'm new at web programming was was wondering if you guys could help me. What I want I guess must be easy, but can't really find /how/.
I have my cute little sidebar that isn't even finished and is constantly needing to be changed. I already gave up on updating it until everything is ready:
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Search</li>
<li>Dashboard</li>
<li>Lookup</li>
</ul>
</li>
<li><a><i class="fa fa-cubes"></i> Inventory <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Asset List
<li><a>Asset Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create Assets
</li>
<li>Update Assets
</li>
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-sitemap"></i> Tickets <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ticket Overview
<li><a>Ticket Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">Create a Ticket
</li>
<li>Update a Ticket
</li>
</ul>
</li>
<li><a>User Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">User List
</li>
<li>Create a User
</li>
<li>Update a User
</li>
</ul>
</li>
</ul>
<li><a><i class="fa fa-laptop"></i> Loans <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a>Loan Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create a Loan
</li>
<li>View Loans
</li>
</ul>
</li>
</ul>
</li>
</li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
But the problem is that I'm doing it like an uncultured swine and keeping that in every single page where it is needed. And if I want to make a change, I need to edit every single page.
I was wondering if there was a way to have a class, or something, with my header, sidebar, and footer, and load them across all pages that needed them?
Thank you for your help!
You can extract the sidebar code into its own PHP file (sidebar.php) then include it where you need it with include or include_once
<html>
...
<body>
<?php include './sidebar.php' ?>
...
</body>
</html>
Using include is essentially the same as copy pasting the code into your file. I.e. It will be added where you include the code and it will be evaluated there as well
Its very simple, Save you sidebar in a php file lets call it sidebar.php, and add below code in your other PHP file where you want to show the sidebar.
<?php include_once('sidebar.php');?>
That's it.
How to make some menu tab option is invisible if user is not Admin, eg: if ['user_level']>=5
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">DTC</li>
<li>View Proposal</li>
<li>Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
I want to make only home.php menu tap visible to user only. Admin can see all the menu tab.
eg screen shot
Server side in php you can conditionally echo the related element.
Assuming you user auth is assigned to $userAuth var you can eg:
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php if ( $userAuth == 'Admin' ) {
echo '<li class="active">DTC</li>';
}
?>
<li>View Proposal</li>
<li>Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
You can do with 2 methods:
1) You have to give class hidden like this:
<style type="text/css">
.hidden {
display: none;
}
</style>
<?php
if ($user_level>=5) {
$hide = "hidden";
} else {
$hide = "";
}
?>
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active <?=$hide?>">DTC</li>
<li class="<?=$hide?>">View Proposal</li>
<li class="<?=$hide?>">Users details</li>
</ul>
<ul class="nav navbar-nav navbar-right">
2) if else condition:
<a class="navbar-brand" href="#">CBS</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php if ($user_level>=5) { ?>
<li class="active">DTC</li>
<?php } else { ?>
<li class="active">DTC</li>
<li>View Proposal</li>
<li>Users details</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
Don't duplicate code - it is first rule. So better is to wrote:
(i persume that ['user_level'] is some value in array(); - here I call this array $user:
<a class="navbar-brand" href="#">CBS</a>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<!--first of all check if you get $user values-->
<?php if (isset($user)): ?>
<!-- display common elements for ALL USERS -->
<li class="active">DTC</li>
<!-- elements only for admins - users with level greater then 5-->
<?php if ($user['user_level'] >= 5): ?>
<li>View Proposal</li>
<li>Users details</li>
<?php endif; ?>
<?php endif; ?>
</ul>
I have two PHP scripts:
header.php
contact_us.php
In Index.php when the user clicks the menu, it smoothly slides down.
But for the "Contact Us" page I don't want that behavior; because it is so simple, containing only a link, I just want it to come up quickly and remain until the home link is clicked in the menu.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php
if(basename($_SERVER['SCRIPT_FILENAME']) == 'index.php'){
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}else{
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}
?>
</div>
For else condition when you are on contact-us page.
Change URL as below:
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
I'm attempting to hide the "Register" link in my navbar after the page checks if someone is logged in. I'm using CodeIgniter as my framework.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<li >Register <span class="sr-only">(current)</span></li>
</ul>
I used this to check if someone is logged in before it shows the logout button and it works fine and dandy.
<?php if($this->session->userdata('logged_in')): ?>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
<?php endif;?>
However, I've seen to run into a snag when trying to do it to hide the Register link. Any help would be much appreciated!
Its should be like this
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<?php if(!$this->session->userdata('logged_in')){ ?>
<li >Register <span class="sr-only">(current)</span></li>
<?php } ?>
</ul>
I just need to include an html file (that has my navigation bars for all my pages).
I've tried: <?php htmlentities(file_get_contents("include/navigation.html")); ?>
But nothing shows up at all.
I've checked other questions like this and the code above is always the answer. So why is it not working for me?
Here's my navigation file if needed:
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">[ ] Advanced Web Development</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active">
Home
</li>
<li>
About
</li>
<li>
RĂ©sume
</li>
<li>
Blog
</li>
<li>
Projects
</li>
<li>
Contact
</li>
<!--
<li class="dropdown">
Portfolio <b class="caret"></b>
<ul class="dropdown-menu">
<li>
1 Column Portfolio
</li>
<li>
2 Column Portfolio
</li>
<li>
3 Column Portfolio
</li>
<li>
4 Column Portfolio
</li>
<li>
Single Portfolio Item
</li>
</ul>
</li>
<li class="dropdown">
Blog <b class="caret"></b>
<ul class="dropdown-menu">
<li>
Blog Home 1
</li>
<li>
Blog Home 2
</li>
<li>
Blog Post
</li>
</ul>
</li>
<li class="dropdown">
Other Pages <b class="caret"></b>
<ul class="dropdown-menu">
<li>
Full Width Page
</li>
<li>
Sidebar Page
</li>
<li>
FAQ
</li>
<li>
404
</li>
<li>
Pricing Table
</li>
</ul>
</li>
-->
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
In your above example, php is creating a string from the file, but doing nothing with it. In order to make your code work, you would need to add an echo:
<?php echo htmlentities(file_get_contents("include/navigation.html"));
This is because there are generally three ways that functions can affect things:
Returning a value (what htmlentities does)
Modifying (a) value(s) passed into them (take a look at passing by reference
Echoing something directly or to the output buffer
Since htmlentities returns a value, nothing is sent to output. You would have to either save it to a variable for output later, or echo/print it to the output now.
Alternatively, you could include it as suggested by #David.
While using the echo on your normal code would be sufficent, if you want people to be able to access the HTML, you can include it in a php file itself, anywhere except inside the <?php ?> tag.
htmlentities() does not output to the browser. You still need to echo() or otherwise output.
<?php echo htmlentities(file_get_contents("include/navigation.html")); ?>
Or as suggested in the comments use include() to embed the entire file. PHP will attempt to process the file, so make sure it is error free if it contains any PHP code.
<?php include( "include/navigation.html" ); ?>
try:
<?php
$file = htmlentities(file_get_contents("include/navigation.html"));
echo $file;
?>