HTML file path references dilemma - php

/index.php
/navbar.php // <a id="#this"></a><div id="that"></div>
/user/search.php
/user/register.php
I have this folder structure in a project I'm currently doing. and initially thought of placing navbar in a separate file for me to include in all other files I have. but when I include navbar.php in a file other than whatever is in root, relative paths won't work so I now use an absolute path. navbar.php implements Bootstrap accordion btw, my problem is with that, I can't make it work with all the file path issue I currently have.
What am I doing wrong? What should be done? and any comments on how I structure my files? would PHP's include_path sort this thing out (reading up on this atm) ?
EDIT:
navbar.php:
<div id="accordion">
<ul class="nav nav-sidebar">
<li>HOME</li>
<li>SEARCH</li>
<li>REGISTER</li>
</ul>
<div class="collapse">
<div id="search"> . . . </div>
<div id="register"> . . .</div>
</div>
</div>
index.php:
<?php include("navbar.php") ?>
...some <html>
user/search.php:
<?php include("../navbar.php") ?>
...some <html>
if I use relative path for navbar <a href="">, all is fine but not when I'm inside user folder
and if I use an absolute path, idk, how can I implement it properly to target a specific tag (in my case, an anchor pointing to a div) inside itself?

Related

get values from SQL DB with php and use it in HTML5

I have a HTML5 file respectively a simple webpage.
and I have a php file.
this file includes a function getKursname(string)
The function is working properly when called in the php file.
This all happens on XAMPP.
I now want to update some values and strings in my html page.
this should happen only on load.
this ist a code the code in the html:
<div class="w3-bar" id="myNavbar">
<a class="w3-bar-item w3-button w3-hover-black w3-hide-medium w3-hide-large w3-right" href="javascript:void(0);" onclick="toggleFunction()" title="Toggle Navigation Menu">
<i class="fa fa-bars"></i>
</a>
<i class="myfont icon-heart"></i> HOME
<i class="myfont icon-users"></i> &UumlBER UNS
<i class="myfont icon-th"></i> KURSE
<i class="myfont icon-mail-alt"></i> KONTAKT
</a>
</div>
I want to have the innerhtml (e.g. KONTAKT)
to be read from the database.
the php function returns a string.
I can call the funcion with getKursname(string) within php.
my idea was:
<i class="myfont icon-mail-alt"></i>getKursname("kurs1")
but this is not working... :-(
thanks in advance
Dirk
Assuming you have a directory structure similar to this:
www/
index.html
functions.php
and you want to inject the content before the index.html is sent to the browser.
In which case, the solution would be, to rename index.html to index.php and at the point where you want the output of the function, you write
<?php include_once 'functions.php'; echo getKursname("kurs1"); ?>
(or require_once instead of include_once, and you have to include it only once before it is used, to load the function definition)
however, standard is to put includes in the header:
index.php
<?php
require_once 'functions.php';
?><html>
<head><!-- ... --></head>
<body><!-- ... other stuff -->
<?=getKursname("kurs1");?>
<!-- rest of page -->
</body></html>
to clarify: <?=[something] ?> is short for <?php echo [something] ?> and everything between <?php and the next ?> is evaluated by php, everything else (like the html code) is just printed as is.

Html theme for every page

I'm creating a theme for my php project. I want to decrease my code amount by creating a simple theme.html page and include that to every page.
Can you help me guys?
Example theme.html:
<html>
<body>
<h1>Welcome to panel</h1>
<ul>
<li><a class="home" href="#">Home</a></li>
<li><a class="about" href="#">About us</a></li>
<li><a class="contact" href="#">Contact us</a></li>
</ul>
<!-- content -->
<div class="content"><!-- other page codes will write here --></div>
<!-- content -->
</body>
<html>
And now i want to include this code to index.html and write form codes to content div? Help me...
You can do it using php
for that you have to save the files .php like index.html into index.php
then include the file like below
<?php include 'theme.php';?>
You can also use twig which is a great template manager for php (initially made for Symfony) and really easy to use.

Links not resolved correctly when using relative paths

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

CSS not called from header.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/).

PHP include Image file not displaying

The header for my website is the same across all of it so instead of rewrite the code and link the style sheets, i've decided to use the <?php include ;?> to put it at the top of every document.
My issue is that the logo that should come with the header isn't displaying.
File Structure
As you can see, the header file is where it is and the logo named "Picture2.png" is in the image folder.
PHP
<?php include('./_/includes/header.php'); ?>
HTML (In header.php)
<nav id="navigation">
<ul id="navList">
<li id="navLogo"><img src="/image/Picture2.png"/>Computing</li>
<li><a class="navItem" href="gallery.php">Gallery</a></li>
<li><a class="navItem" href="topics.php">Core Topics</a></li>
<li><a class="navItem" href="courseview.php">Courses</a></li>
<li><a class="navItem" href="index.php">Home </a></li>
</ul>
</nav>
Part of header that isnt' displaying correctly
NOTE** everything else in the header is correctly displayed, I'm using a local server, should that make a difference
You are using an absolute path for your image.
You should put and use a relative path :
<img src="_/includes/image/Picture2.png"/>
instead of
<img src="/image/Picture2.png"/>
Yep, hes using an absolute path for image, but the project isn't in server root folder then you need's to inform the name of the folder in path...
Use <img src="/finalprojectneat/image/Picture2.png"> then you have your logo display on every page. But is not the most indicated because when you send to production server, you didn't have the "finalprojectneat" folder then you have to remove all paths using "projectneat".
One solution is to define a constant in your "index.php", not necessary in "index.php" but required in root folder of project
define ('_IMAGES_', realpath(dirname(__FILE__) . '/image'));
if you put this constant in another file inside root folder, use "require" to import these constants to your views...
and in your views, use
<?php echo _IMAGES_ . '/Picture2.png'; ?>

Categories