I include two files in every view, which are nav.php and footer.php.
All session varaiables and base_url() are working in nav.php file, but when I want do the same in footer.php it is not working.
url helper file is autoload via config/autoload.php as
$autoload['helper'] = array('url', 'file');
I spent whole day to figure it out my self but it's not working.
nav.php
<ul class="nav navbar-nav">
<li>Post New Project</li>
<li class="dropdown">
Projects <b class="caret"></b>
<ul class="dropdown-menu">
<li>All Projects</li>
<li>Featured Projects</li>
</ul>
</li>
<li class="dropdown">
Funds <b class="caret"></b>
<ul class="dropdown-menu">
<li>Upload Funds</li>
<li>Withdraw Funds</li>
</ul>
</li>
</ul>
footer.php
<ul class="footer-list">
<li>Login</li>
<li>Register</li>
</ul>
In my viewname.php file I'm calling them as
<?php include('includes/nav.php'); ?>
Other code here
<?php include('includes/footer.php'); ?>
nav.php working fine but footer.php not working.
autoload.php
(Line :67) $autoload['helper'] = array('url', 'file');
config.php
(Line :17) $config['base_url'] = 'http://localhost/cl';
Probably an echo is missing?
And make sure that autoload is not declared more than once.
In order to use base_url(), you must first have the URL Helper loaded (which you have done). This can be done either in application/config/autoload.php (on or around line 67):
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the
protocol, domain and path to your installation.
application/config/config.php, line 13
EDIT:
Change the following
include('includes/nav.php'); // Other code here
include('includes/footer.php');
to
$this->load->view('includes/nav.php');
// Other code here
$this->load->view('includes/footer.php');
I wouldn’t use this method in an MVC framework since there is no need with $this->load->view. Another thing is the paths have to be absolute/relative which mean if you did change the folder structure you have to go update all include statements. Its really against MVC structure. If we there is ability to do this using parser or defined methods then why we call view in another view:)
Related
I've created a Header.php file in the root directory.
Below is the tree structure.
-SiteName
-Header.php
-Books
-Samples
-Math
-MathBook.php
-index.php
-About.php
So MathBook.php includes Header.php like below
include '../../../Header.php';
Header.php is included in About.php like this
include 'Header.php';
Now Header.php contains the code for the navigation menu.
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
So now when I'm in Math.php and I press home it takes me to:
/Books/Samples/Math/index.php
Obviously the URL doesn't exist.
How i can make sure when the user is in Math.php it should go to index.php without including current path.
Header.php is included in all files.
You could define includes and links relative to your site's base url.
The hrefs can be written like so:
<ul>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/index.php';?>"
class="<?php active('index.php');?>">Home</a>
</li>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/About.php';?>"
class="<?php active('AboutUs.php');?>">About Us</a>
</li>
</ul>
You are using a relative path.
by typing "index.php" in the href attribute, you are pointing to a file named "index.php" in the current folder.
If you want to point to a file in the root folder, you need to use a slash /
href="/<sub folder if any>/index.php"
href="/<sub folder if any>/About.php"
This will take the user to the file you would like, also if you are unsure about what to use, just use absolute paths like this:
href="https://www.example.com/<sub folder if any>/index.php"
href="https://www.example.com/<sub folder if any>/About.php"
EDIT
Adding a slash works because by adding / you tell the browser to go to the root folder/website and start the path from there.
Let's say you are in yourwebsite.com/current/path/file.html.
A link to /some/path/to/a-file.php resolves to yourwebsite.com/some/path/to/a-file.php
But a link to some/path/to/a-file.php resolves to yourwebsite.com/current/path/some/path/to/a-file.php
This is my header.php stored in include folder
<ul class="dropdown-menu">
<li>My Account</li>
<li class="somefile.php">Admin Panel</li>
<li>Users</li>
</ul>
NOTE: i have to include header.php in both folder
Main Root Folder
RootFolder/MasterFolder
Now when i am calling file from Root directory it works fines
but when i am include this file in Other folder and call index(My account) so it call like this
mysite/master/index.php
but i wants when i am calling include file from master named folder
mysite/index.php
My File Structure is like this
in master also have files. and my header.php is in includes Folder
URLs and paths are different.
When you're including your html header in php you should require it using the document root of the server. This way, no matter which system you're on, you can still access it.
If telecaller is your web root directory, then you should have the code:
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
Paths can then be relative URLs in the HTML inside of that header.
<a href='/index.php'>Telecaller Index File</a>
<a href='/master/index.php'>Master Index File</a>
<script src='/ajax/something.js'></script>
<a href='/login.php'>login page</a>
define('ROOTPATH', __DIR__);
//define root path
// and make link of file from the root path
<ul class="dropdown-menu">
<li>My Account</li>
<li class="<?php echo ROOTPATH.'somefile.php'; ?>">Admin Panel</li>
<li>Users</li>
</ul>
<ul class="dropdown-menu">
<li>My Account</li>
<li class="../somefile.php">Admin Panel</li>
<li>Users</li>
</ul>
You can give a relative path like this, or give absolute path
https://www.w3schools.com/html/html_filepaths.asp
<li>SERVICES</li>
<li>ABOUT</li>
<li>CONTACT</li>
I'm using CodeIgniter and this code was in a different controller. suppose http://localhost/defctrl/function1
I want to return to my homepage "maine/home" in a specific div when you click on the list but the above code doesn't work. neither ../maine/home#services.
How can I solve this problem?
if it's on the same page you can use
<li><a href="#services"
if another page
<li><a href="<?php echo base_url() ?>{route or controller name}#services"
Make sure URL helper is loaded and site load with index.php
I had some big problems calling my header at all from project1.php. It got solved with the header included like this:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/header.php'; ?>
The header is now called, but all my CSS is not called. Howcome can that be? On all my pages there is lying in the root folder the CSS is working fine.
My folder structure is looking like this:
This code below is a snip of my header. As you can see Home should go to index.php. But when I am in my project1.php and I click on Home I get the URL: `
http://localhost:8888/vouzalis/projects/index.php
The correct root should be:
http://localhost:8888/vouzalis/index.php
It does not make sense to be. Does anybody have a clue why? Is it something with how the header is included?
<div class="navbar-collapse collapse">
<ul class="nav nav-pills nav-main pull-right">
<!-- begin navigation items -->
<li class="active">Home</li>
<li>
Services
</li>
<li>
Portfolio
</li>
<li>
About Me
</li>
<li>Contact</li>
<!-- end navigation items -->
</ul>
</div>
I believe the problem is here:
<li class="active">Home</li>
Replace href="index.php" with href="/vouzalis/index.php". The / specifies the domain root, without it, the root is the your current location (/vouzalis/projects/).
Hello i recently just got my web server to run all .html files as .php files which now allows me to use the php include function, which is very useful.
However i have multiple sub directories with multiple files in each.
Homepage
--banners
--banner2.html
--banner2.html
--signs
--sign1.html
and so on. The problem here is im trying to use include for my top nav bar. when i include the bar in the sub files, for example banner2.html it wont correctly link.
nav.php
<ul class="nav navbar-nav">
<li>
About
</li>
<li>
Services
</li>
<li>
Contact
</li>
<li>
Login
</li>
<li>
Logout
</li>
<li>
<img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart
</li>
</ul>
in the nav bar is a link to about.html normally i would do the
about
however i cant do that when every file is going to share the same navigation bar.
How can I fix this?
I have answered my own quest and feel silly for even asking the question. I just used absoulte paths
nav.php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
About
</li>
<li>
Services
</li>
<li>
Contact
</li>
<li>
Login
</li>
<li>
Logout
</li>
<li>
<img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart
</li>
</ul>
There are some ways to fix this, one of them is:
Use $_SERVER["DOCUMENT_ROOT"] – We can use this variable to make all our includes relative to the server root directory, instead of the current working directory(script’s directory). Then we would use something like this for all our includes:
`about`
Also you should check this post.
You could use the relative path to the root directory which is /file.ext
Example:
About