PHP Header Include (nav bar) not showing up [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
CLARIFICATION: Being that I've never used PHP, it was unclear how to format the PHP file, so that when you include it in the index, then header(nav bar) would show up. No posts explained why I had to change my index.html to index.php to make it work. And again, being that I haven't used PHP, I was under the impression that changing the extension meant that I would have to convert my HTML code to PHP commands. I was trying to avoid having to change my whole code around. That's where the confusion was.
I was going to delete the question once I got it figured out. Then again, I'm sure I'm not the only person that may run into this while making their first website.
SOLUTION. Save only the nav code in html language with a PHP extension (header.php). Change index/home file from .html to a .php extension (don't have to modify any code. but accepts the include prompt). Then, include the header.php in your index.php. Same goes for your footer. Thanks for the help everyone.
*ORIGINAL POST: Through another post, I was told it would be easier to use a nav/footer on multiple pages by using PHP.
I created a file and tried including it and it is not displaying. I'm not sure what I am doing wrong.
There is some CSS in my nav bar as well, do I need to import the css file in the PHP file? or does the index.html file automatically attach to the included (php) file.
PHP Code:
<?php
echo '<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>';
?>
HTML Code (include):
<body>
<?php include '/header.php';?>
....other code....
</body>
If there's a problem with the PHP file, if I'm missing something, can someone show an explain please?

no need to add <?php ?> code in the header file just keep it as it is :
Create a new file named header.php and add this code to it
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
Include header.php anywhere you want

Remove the <?php tag from your code. As you generated html output. <?php tags are used to generate output by php.
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>
If you want to use php tags, than you have to write the code in this way:
<?php
echo '
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>
';
?>

Your problem is most likely the pathing of you include. Often you'd need to specify from the server root, and not just /header.php.
To specify your including-path, you could specify the root like this
include $_SERVER['DOCUMENT_ROOT']."/header.php";
You can also set the default include path in your php.ini file, thus making the $_SERVER['DOCUMENT_ROOT'] an automatic process, that can be done with running this code, that will alter your php.ini file.
ini_set('include_path', '/usr/lib/pear');
As others have pointed out, you don't need the contents of your /header.php to be echoed out as it's pure HTML. Anything that's included will be included the way it is, so having the contents of /header.php as pure HTML just makes for better practice, as it's easier to read.

Related

How to make different layout.php files and include into different languages?

I'm interested in solving this problem, since I can't figure out what to do. I made a fade in/out text under menu for mobile version to show some information, but since the code has to be between two HR lines I added the code to layout.php file, and now I can't put that on 3 other languages because it's on German.
<div class="">
<div class="">
<hr class="" />
<div class="" style="">
<div class="">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<hr class="" style="" />
</div>
</div>
This is the code for that fade In/Out effect and changing from one to another, I managed to do that with nth-child selector, nothing hard...
My web site has 4 languages, but every language has 2 web sites that are different. I'm not using any framework just bare code. I've tried searching for anything that would be connected to that HR line so I could add manually on every site another line and that text between them, but there is none because it uses layout.php to show that line.
<div id="" class="">
<? if ($special_fullscreen != true): ?>
<div id='' class="">
</div>
<div class="">
<div class="">
<hr class="" style="" />
<div class="" style="">
<div class="">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<hr class="" style="" />
</div>
</div>
<div class="">
<div class="">
</div>
</div>
So, I would like to hear an idea that would resolve this issue, like making a different layout.php for every language or something like that and how would that be possible. I know that this code probably doesn't have anything to do with the problem, but I'm in a hurry and just tried to copy at least something... Thanks in advance :D
I am completely not sure if I know what you mean but it may be helpful info.
If you want to include different files on the same page depended on language - you can set cookie with info about chosen language and then, based on that, load proper layout.php file for it.
You can also make all language versions in one layout.php file, include it in proper place and make if statement checking which content should get displayed (inside that layout.php file) - also based on value of COOKIE.
Instead of cookie, you can also use $_SESSION but that may not work on the way you really want (according to my understanding of your problem).

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.

My PHP includes won't appear

I'm trying to include the header and footer of my site as PHP include files, but can't seem to get it to work. I've changed the index.html to index.php. Then, I removed the header and footer and replaced them with PHP include statements, like so:
<?php include 'includes/header.php';?>
In the PHP include files, the code looks like this:
<footer>
<div class="container">
<div class="row">
<div class="col-sm-3">
<a class="btn btn-default" href="#" target="_blank"><i class="fab fa-facebook-f"></i></a>
<a class="btn btn-default" href="#" target="_blank"><i class="fab fa-twitter"></i></a>
</div>
<div class="col-sm-3 offset-sm-6">
<p>© 2018 Ace in the Hole</p>
</div>
</div>
</div>
</footer>
This is just the HTML code for the footer that I'm inserting into the index.php. Is there a PHP statement that should be used here to pull this information? Like an echo statement?
Thanks for any help.
Check the path of the included file
You must run the using a server like wamp,xampp etc
sample index.php
<?php
include 'includes/header.php';
?>
sample header.php file in the "includes" folder
<footer>
<div class="container">
<div class="row">
<div class="col-sm-3">
<a class="btn btn-default" href="#" target="_blank"><i class="fab fa-facebook-f"></i></a>
<a class="btn btn-default" href="#" target="_blank"><i class="fab fa-twitter"></i></a>
</div>
<div class="col-sm-3 offset-sm-6">
<p>© 2018 Ace in the Hole</p>
</div>
</div>
</div>
</footer>
I'm not sure where there error was in my paths, but I moved the includes out of their folder and onto the same level as the index.php (not ideal, bu it will do for now). They work fine this way, called in a statement like this:
<?php include 'header.php';?>
Thanks for your suggestions.
Take in mind include references directories, not urls, for instance
If you have include_me.php in /var/www/html/includes/, to include it in a file located in /var/www/html/ you should:
include(__DIR__ . "/includes/include_me.php");
also I recommend you to check your apache log looking for messages:
centos
/var/log/httpd/error_log
ubuntu/debian
By default, /var/log/apache2/error.log.
This can be configured in /etc/php5/apache2/php.ini
Hope it helps you

Conditional PHP to show different HTML blocks

I'm working on a login system for a website in which the top right corner will change depending on whether or not the user is "signed in" (a check against a session variable). I am using the following to try and show only one these two states.
<?php if(isset($_SESSION['id'])): ?>
<div class="large-2 large-offset-5 columns">
<p class="right text-pad-top">#MuffinTheFox</p>
</div>
<div class="large-1 columns">
<img class="avatar" src="https://pbs.twimg.com/profile_images/429861192207982592/lxaKQ4Rp.jpeg">
</div>
<?php else: ?>
<div class="large-2 large-offset-7 columns">
<a class="right text-pad-top" href="login.php">Log in/Sign up</a>
</div>
<div class="large-1 columns">
<img class="avatar" src="http://placehold.it/50x50">
</div>
<?php endif; ?>
The problem is, when I upload the code to my server and run it, both HTML blocks display, regardless of the PHP statements.
There aren't any obvious errors and according to my research into the topic, this should work. Also, I tried the way without the endif; statement as well by using brackets, and that didn't work either. At this point I'm somewhat lost as to why this is happening and any help or insight would be appreciated.
Edit: Something of note, the PHP is embedded into a HTML document with a .html extension. I was under the impression that you could just inline php into a html document without issue, but I have a feeling that this might be the cause of the issue.
Your file is not getting parsed as PHP because PHP is not configured to parse .html files unless you tell it to. See this question for insight into how to do that. In the meantime, change your file extension to .php and it should start working as you expect.

ckeditor auto content change

I am using ckeditor to insert some html structure into db. I firstly use code view to paste html structure into editor, after inserting into mysql db, until echo at webpage, the html structure data are still maintained.
However, when the same data is called into the same ckeditor again for updating, the structure is destroyed, and I found the ckeditor has automatically changed html tags to the codes, below is an extract:-
A) html code to be pasted to editor code view for inserting (good)
<a href="#">
<img src="../catalog/view/theme/default/images/services/b1.jpg" alt="" />
<div class="btn-gradient-yellow">Details
<div class="btn-arrow"></div>
</div>
<div class="cat-title-bg">
SERVICES
</div>
</a>
B) db data (good)
<a href="#">
<img src="../catalog/view/theme/default/images/services/b1.jpg" alt="" />
<div class="btn-gradient-yellow">Details
<div class="btn-arrow"></div>
</div>
<div class="cat-title-bg">
SERVICES
</div>
</a>
C) webpage data called from db (good)
called by html_entity_decode($record["content"],ENT_COMPAT, 'UTF-8');
<a href="#">
<img src="/catalog/view/theme/default/images/services/b1.jpg" alt="" />
<div class="btn-gradient-yellow">Details
<div class="btn-arrow"></div>
</div>
<div class="cat-title-bg">
SERVICES
</div>
</a>
D) code view in ckeditor, with contents filled from the above data (changed)
called by html_entity_decode($record["content"],ENT_COMPAT, 'UTF-8');
<img alt="" src="../catalog/view/theme/default/images/services/b1.jpg" />
<div class="btn-gradient-yellow">Details
<div class="btn-arrow"> </div>
</div>
<div class="cat-title-bg">SERVICES</div>
How can I ask ckeditor not to add changes to the data?
CKEditor modifies your HTML because it is incorrect. You cannot put block elements inside of <a> unless you use HTML5 which isn't supported by CKEditor yet.
See my answer for related question. Also take a look at this one regarding blocks in <a>.

Categories