php folder structure XAMPP - php

Using PHP (XAMPP)
I have a simple folder layout as such:
For right now, my index.php file just contains "mainbar.php"
mainbar.php:
<?php
require(config.php);
include ("login/session.php");
global $session;
$logged=$session->CheckSession();
?>
<!DOCTYPE html>
<html>
<head>
<script src='scripts/jquery.js'></script>
<link rel='stylesheet' href='styles/mainbar.css'/>
</head>
<body>
<section class='topcontainer'>
<nav id='mainnav'>
<ul>
<li><a href='#' class='acti' id='homepage'>Home<span class='badge red'>3</span></a></li>
<li><a href='qms/qms.php' id='qmspage'>QMS<span class='badge yellow'>35</span></a></li>
<li><a href='#' id='modelpage'>Model Search</a></li>
<li><a href='#' id='partsdbpage'>Parts Database</a></li>
<li><a href='#' id='wddbpage'>WD Database</a></li>
<li><a href='#' id='toolspage'>Tools</a></li>
<li><img src='styles/img/profile.png' width='25px'/></li>
</ul>
</nav>
</section>
When the user clicks on the "QMS" button (href='qms/qms.php').
I get the following error (using chrome):
Failed to load resource (http://localhost/control/qms/scripts/jquery.js)
PHP is going to the directory "qms" and then trying to find the "scripts" folder which I do not want it to do.
Keep in mind I just can't make it say "../scripts/jquery.js" because that may not always be the case.
How do I set this up so that "jquery.js" will have a permanent reference back to the parent folder "control/scripts/"??

you can just do as :
<script src='http://localhost/control/scripts/jquery.js'></script>
or use some global variable for this purpose

Amit suggests using the absolute path, but this is inflexible. You'll have to change the paths of these when you deploy the site, and if you ever change the name of your localhost, you'll have to manually update everything.
I'm assuming that your site's configured to use the root directory of localhost/controls. If this is the case, you should be able to link to the script from anywhere in the controls folder using "/scripts/jquery.js".
To find your root directory, run this in a PHP script: echo $_SERVER['DOCUMENT_ROOT'].

Related

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

Linking Not working

I am so confused what's the difference between those links below:
<link rel="stylesheet" href="http://<?php echo $_SERVER['HTTP_HOST'] ?>/demo/assets/css/style.css"
and
<li> HOME </li>
First of all, the first link is working very well, but the second link is not working. As it is written The requested URL /demo/contents/test.php was not found on this server.
My directory files is public_html > demo > contents > test.php. Any ideas?
Its best to use
$_SERVER['DOCUMENT_ROOT']
Then access the folder from the root directory.
For example:
href="http://<?php echo $_SERVER['HTTP_HOST'] ?>/demo/assets/css/style.css"
href="http://<?php echo $_SERVER['HTTP_HOST'] ?>/demo/contents/test.php"
Hope this helps :)
This is used for directly loading into the Page onload:
<link rel="stylesheet" href="http://<?php echo $_SERVER['HTTP_HOST'] ?>/demo/assets/css/style.css"
And this:
<li> HOME </li>
Is for user-actions, so you link a destination the user should be able to reach when clicking on it.

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

Menu formatting is not retained

I wrote a menu in one menu.php and called it to another file. For reduction of code, I have only included what seem minimal/sufficient.
This is definition in style of main page.
#menu{position:absolute;top:100;left:260;height:40;width:490;}
This is where I call the menu.php
<div id="menu"><?php include("menu.php")?></div>
This is how I have defined menu.
<ul id="menubar">
<li><a href=#>HOME</a></li>
<li><a href=#>NEWS</a></li>
<li><a href=#>RESOURCES</a></li>
<li><a href=#>ABOUT US</a></li>
</ul>
This is the style of menu in menu.php
*{margin:0px;padding:0px;}
body{font-size:120%;font-family:arial;background-color:#D4F1FA;padding:50px;}
ul#menubar{list-style-type:none;}
ul#menubar li{width:120px;text-align:center;position:relative;float:left;}
ul#menubar a{color:#DAECF2;text-decoration:none;display:block; width:120px;height:40px;line-height:40px;background-color:#153945}
ul#menubar li:hover>a{background-color:rgba(20,66,82,0.6);color:#C9E6F0}
The problem is The font size and color is not preserved in main file where it works in menu.php only. Any suggestions?
When you include a php file that includes other files you have to write the full path of your css.
One more advice is to use tools like the chrome inspector to see if all your files loaded correctly.
so for example :
Don't do
<?php
<link rel="stylesheet" type="text/css" href="../css/style.css">
?>
instead do
<?php
<link rel="stylesheet" type="text/css" href="/website/css/style.css">
?>
This is an unorthodox way of adding common content into file. You could make it more flexible by adding the html into a function and calling it at will.
function displayNav()
{
echo "<ul id='menubar'>
<li><a href=#>HOME</a></li>
<li><a href=#>NEWS</a></li>
<li><a href=#>RESOURCES</a></li>
<li><a href=#>ABOUT US</a></li>
</ul>";
}
You can add more functions for other common content as required.
Furthermore I would suggest moving your css into an external stylesheet; it will make it easier maintain and improve the readability of the markup pages.

base_url() not working properly in internet explorer (CodeIgniter)

My navigation bar images and links load fine in CHROME but when loading the same page in INTERNET EXPLORER the images don't load but displays the no image icon. The links in IE are like this
CodeIgniter/index.php/news/index.php/news/
instead of just
CodeIgniter/index.php/news
HOWEVER, IE manages to load the css successfully even though that is also using the base_url()... here is my code:
test.php
<head>
<base href="<?php echo base_url() ?>">
<link rel="stylesheet" type="text/css" href="public/css/main.css">
</head>
<nav>
<ul>
<li><a href="index.php/news">
<img src = "public/images/home.png" alt="Home" title="Home"/>
</a></li>
<li><a href="index.php/news/create">
<img src = "public/images/create.png" alt="Create new article" title="Create new article"/>
</a></li>
</ul>
</nav>
config.php
$config['base_url'] = 'http://localhost/CIgniter/CodeIgniter/';
Answer
Quote:Well Fabios suggestion was successful thanks <img src = "<?=base_url('public/images/home.png')?>" alt="Home" title="Home"/> works even though it is gonna be a pain in future. Thanks for all your help!
Well Fabios suggestion was successful thanks
<img src = "<?=base_url('public/images/home.png')?>" alt="Home" title="Home"/>
works even though it is gonna be a pain in future. Thanks for all your help!
The real problem is that you are using relative paths e.g public/images/home.png instead of /public/images/home.png, the slash at the start tells the browser that it should retrieve content based on the absolute path you've specified at $config['base_Url'] = ... instead of the current path it is (I mean you get to CodeIgniter/index.php/news and you've specified a relative path to it so that's why it appened to it producing a wrong path CodeIgniter/index.php/news + index.php/news/)
so when you add a slash at the start of your path it will be refering to the absolute path and it will get to you at CodeIgniter/index.php/news.

Categories