get php file from folder using html <a href=""> - php

My site is http:\www.xxx.com/xxx
I'm having problem to get the specific file inside a folder
The file is inside admin_pages folder under the root
The menus folder are under root too
In my i put
<a href="../admin_pages/adminAtividadesByMonthChart.php">
and it returns to http:\www.xxx.com
If i use
<a href="../../admin_pages/adminAtividadesByMonthChart.php">
Its the same...
This is my image project
any help?

You may use a constant for base url.
It may be BASE_URL
So ;
<?php
define('BASE_URL', 'http://www.example.com');
<a href="<?php echo BASE_URL;?>/admin_pages/adminAtividadesByMonthChart.php">
Good luck

Related

Adjusting link paths in a Navbar PHP file for subdirectories

I'm trying to create a portfolio website. This is my third attempt, and I've been attempting to use PHP files for the first time. In my previous websites, I would copy large swaths of HTML into each page so that they would be formatted the same. With this website, I'm instead just trying to link to a few PHP files that contain code shared by multiple pages, like the navigation bar for instance.
My issue is this. Say in my index file
mywebsite/index.php
I include this PHP file which contains HTML code for a navigation bar
<?php
echo <<<EOT
<!-- Navigation Bar -->
<div id="nav">
<a class="nav-link" id="index-link" href="">About</a>
<div class="nav-divider"></div>
<a class="nav-link" id="resume-link" href="resume">Resume</a>
<div class="nav-divider"></div>
<a class="nav-link" id="portfolio-link" href="portfolio">Portfolio</a>
<div class="nav-divider"></div>
<a class="nav-link" id="contact-link" href="contact">Contact Me</a>
</div>
EOT;
?>
However, the way the links work, if I wanted to include this same navigation bar in a sub-directory webpage
mywebsite/portfolio/index.php
All of the links would stop working.
I've tried doing absolute paths to the root, but then the webpage's url shows the absolute path as well, including things public_html in the url which looks bad.
I've tried doing paths like "mywebsite.com/..." and "mywebsite/..." but thats just shows up as "mywebsite.com/mywebsite/..." and doesn't actually work. If I try saying "public_html/..." public_html becomes included in the url.
I don't want to create a second PHP file just to have the same code with slightly different href paths. Is there a path I could use for both my main directory and subdirectories that doesn't show up weird in the url? If not, is there a way to use Javascript or Jquery to detect if a file is in a subdirectory, and add "../" to all links inside the included PHP files?
I can't seem to find an answer to this question, maybe I'm not using the right words in my searches? This feels like it would be a common issue.

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

Link to local html file

Im trying to put a link to local html file called Rules.html,
but for some reason I cannot make it work.
Html file is in the same folder as my php file.
Note that Im running my forum on localhost:1337, Should that be somewhere in that line too?
Here is my piece of code:
<li class="copyright">
<a class="rules" href="file:///C:/xampp/htdocs/SMF/smf_2-0-6_install/Themes
/Fresh_v2_RC5/Rules.html">Rules</a>
</li>
I would suggest using relative links. So the link would be
href="theFile.html"
if it's in the same folder as the file you are trying to write the link in.
Use relative path from your root dir. Root dir is a dir where located your html file within this code. For example, if you put this code in html file, located in htdocs dir, so the relative link is:
<li class="copyright">
<a class="rules" href="SMF/smf_2-0-6_install/Themes/Fresh_v2_RC5/Rules.html">Rules</a>
</li>
If it is in the same folder as the file you are calling it from, you can just call it by name, instead of it's complete path.
<li class="copyright">
<a class="rules" href="Rules.html">Rules</a></li>
Also, if you are calling it by it's full path name, you do not want to use file: to open a linked html document. You would call:
edit: (added // in front of localhost.)
<li class="copyright">
<a class="rules" href="//localhost/../Rules.html">Rules</a></li>
And pull it up in your apache server. Using file is like opening the file in your file system, but to view html, use your web server.
As others have said, it's easiest in this case to simply use relative urls.
The way to do this with the code you've provided:
<li class="copyright">
<a class="rules" href="Rules.html">Rules</a>
</li>
Make sure that this Rules.html file is in the same directory (dir) as the file from which you're calling it. If not, you'll need to do some directory traversing to get to it.
Here's a question with some helpful examples.

Categories