php include echo error - php

I am trying to use the php include tag for my header and footer, and can't seem to find the problem. My include code is fine in my index file, and my header.php file shows no errors, but t doesn't include the file when I preview it in a browser. Is this an easy fix or am I missing something.
My index.php file include snippet
<div id="container">
<?php include('include/header.php');?>
<div id="body">
My header.php file code
<?php
echo <<<END
<header>
<nav>
<li onclick="location.href='index.html';" style="cursor:pointer;">Home</li>
<li onclick="location.href='#';" style="cursor:pointer;">Contact</li>
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
<li onclick="location.href='#';" style="cursor:pointer;">Product Solutions</li>
<li onclick="location.href='#';" style="cursor:pointer;">Services</li>
<li onclick="location.href='#';" style="cursor:pointer;">Request a Quote</li>
</nav>
</header>
END;
?>

It seems ok on my computer. Are you sure that's your header file is the right directory? It should be:
---- index.php
---- include (directory)
|---- header.php
If it's ok you must file permissions and be sure that index can include the header file.
Why do you need to use heredoc syntax?

Why not do this instead:
<?php
... some code ...
?>
<header>
<nav>
<li onclick="location.href='index.html';" style="cursor:pointer;">Home</li>
<li onclick="location.href='#';" style="cursor:pointer;">Contact</li>
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
<li onclick="location.href='#';" style="cursor:pointer;">Product Solutions</li>
<li onclick="location.href='#';" style="cursor:pointer;">Services</li>
<li onclick="location.href='#';" style="cursor:pointer;">Request a Quote</li>
</nav>
</header>
<?php ... more code ... ?>
<!-- more html -->
You don't need echo. Any HTML not within tags are automatically outputted anyways. Remember that PHP is based on HTML. You can think of PHP as HTML with <?php?> tags mixed in.

Change this line:
<li onclick="location.href='#';" style="cursor:pointer;">FAQ's</li>
With this:
<li onclick="location.href='#';" style="cursor:pointer;">FAQ\'s</li>
The error was this:
FAQ's
And I changed it to this:
FAQ\'s

what do you mean by
echo <<<END ???
however the file header.php is in a separate folder ? look at in the console to see if it can locate the file or not... for me i used to work with :
require_once("include/header.php");

Related

How to make navbar constant across multiple pages?

I have looked at prevous questions about this and people say php and have not found an answer. how do I convert my navbar to php and use it in multiple html pages. Could someone tell me how to do so? I am currently taking a codecademy course on php and it is really hard for me to understand so please be patient.
<div class="tabs">
<ul>
<a href="http://degraphic-design.dunked.com/contact-me" style="text-decoration:none">
<li class="contact">Contact</li>
</a>
<li class="dropdown">Shop</li>
<li class="forum">Forum</li>
<a href="index.html" style="text-decoration:none">
<li class="about">About</li>
</a>
<li class="team">Team</li>
<a href="http://degraphic-design.dunked.com/" style="text-decoration:none">
<li class="portfolio">Portfolio</li>
</a>
</ul>
</div>
Say you have about.php and home.php in the root of your website. Create a directory called partials (or whatever), go into partials and put the contents of your navigation HTML in a file called nav.php. Then in about.php and home.php, use this where you want to include the navigation code
<?php include 'partials/nav.php'; ?>
Here is one way (extremly basic):
Create a PHP file called index.php
<!DOCTYPE html>
<html>
<body>
<header>
<?php
include 'header.php';
/**
* say you wanted a different header for shop
* if($_GET['page'] === 'shop') {
* include 'header-shop.php';
* } else {
* include 'header.php';
*}
*/
?>
</header>
<div id="main">
<?php
include $_GET['page'].'.php'; // assuming your storing your pages in same path as index
?>
</div>
<footer>
<?php
include 'footer.php';
?>
</footer>
</body>
</html>
Then a header.php
<div class="tabs">
<ul>
<li>Contact</li>
<li>Shop</li>
</ul>
</div>
And create your page files contact.php, shop.php ect.
Updated to a slightly more elaborate example to give you the idea.

Problems adding class using jquery and PHP

To start off, if this has already been answered please point me to the right area as I have not yet been able to find it.
I have built a web sit which contains 4+ pages and I am using the php code include(filename.php); where filename.php is the name of the php file containing my header navigation. I also have a class named "current" which, before adding the include() statement was manually placed on each link within the navigation bar. I am trying to do this using jQuery as I have had to remove this class within the linked php file. I have tried writing the code as such:
<body>
<?php
$file_included = true;
// common code used in every page
if ($file_included == true) {
include("header.php");
} else {
header("Location:remedies.php");
}
?>
<script>
$(document).ready(function() {
$("#about").addClass("current");
});
</script>
however when I test this site on my server, the "current" class does not move to the currently selected link. The code contained within the linked .php file is as follows:
<div id="title">
<header>
<img src="images/Dragon-Catcher-Web-Logo.jpg" alt="Dragon Catcher Web Design Logo" id="design" style="padding-right: 15%; padding-left: 1%; padding-top: 1%;"/>
<span style="text-align: center;">Dragon Catcher Herbs</span>
</header>
<nav>
<ul id="navlist">
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>Beginner Herbalists</li>
<li>Herb List</li>
<li>Remedies</li>
<li>User Recommended</li>
</ul>
</nav>
not sure where I went wrong with my code but any and all help would be great.
I used your code and make two separate files as below:
question.php
<html>
<head>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
</head>
<body>
<?php
include("header.php");
?>
<script>
$(document).ready(function() {
$("#about").addClass("current");
});
</script>
</body>
</html>
header.php
<nav>
<ul id="navlist">
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>Beginner Herbalists</li>
<li>Herb List</li>
<li>Remedies</li>
<li>User Recommended</li>
</ul>
</nav>
And css class current applies on #about.
I think you didn't included jquery.js in your code. Just try this code.

Catch the URL and redirect somewhere else

i ve done this below menu in php which is autogenerated with a function :
As you know, if i click on "Red Chicken", it will try to open a ../Red%20Chicken URL, which doesnt exist till it comes from a big table which change everytime.
What i want to do is : to know where we clicked (example : by generating the url and cut it ) and then redirect to a page like result.php (+ get something like the variable to know where do we come from). And of course, i don't want to create a .php page for each element of my table.
Is this thing possible ?
EDIT : i found a way, it's to change my function and how it generates the menu.
Not really what i wanted but it's okay.
<ul class=\"niveau1\">
<li class=\"sousmenu\"><a href=\"Food\">Food</li>
<ul class=\"niveau2\">
<li class=\"sousmenu\">Meat</li>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Poultry</li>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Red Chicken</li>
</ul>
</ul>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Beef</li>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Hamburgers</li>
</ul>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Steak</li>
</ul>
</ul>
</ul>
<ul class=\"niveau2\">
<li class=\"sousmenu\">Dairy</li>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Cow</li>
</ul>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Sheep</li>
</ul>
</ul>
</ul>
<ul class=\"niveau1\">
<li class=\"sousmenu\">name</li>
</ul>
http://jsfiddle.net/w10j0a38/1/
The PHP-Way would probably be to use URLs like <a href='menuhandler.php?sel=cow'>Cow</a>, and if you want to be lazy, you could also JS/jquery to manupulate the URLs and replace <a href="something">with a call in the style shown before - but then this would not work w/o JS, so it is not a real option IMHO.
changed my function to generate it in another way
<a href=\"Recette.php?var=food\">
Not really what i wanted but it's okay.

How do I apply my php code to a bootstrap code?

I am trying to display something from my database on a bootstrap bar and I have no idea how to combine or implement the two. Ive made my bootstrap html's file a .php, was that the correct thing to do? Here's my code (ignore the filler words)
<ul class="nav nav-list well">
<li class="nav-header"></li>
<li class="active">HIT INFO</li>
<li>Linky link</li>
<li class="divider"></li>
<li>ANOTHER HIT INFO</li>
<li>ANOTHER LINKY LINK</li>
<li class="divider"></li>
<li>YET ANOTHER HIT</li>
<li>AAAAAnd another link</li>
</ul>
and this is the php i want to include (the code to making what i want to display is in the file)
<?php include 'one.php'; ?>
where "linky link" is, i want to display this but it didnt seem to work when i put that php right there. Another thing i want to do is when a link from the database gets displayed, it displays as a bootstrap button. i tried and am just not sure how to implement php code in my bootstrap code.
Print " <td>".$row['link'] . "</td></tr> ";
How would i add this bootstrap code to that so when a link from my database gets displayed, it gets displayed as this button
<i class="icon-heart icon-white"></i>Do this HIT!
Your code seems right to me except to your "<td> and <tr>" tag. You're not using it right.
Try this approach
In one.php
<?php
$links = array( 0 => array("url"=>"http://google.com","text"=>"Google") );
?>
In Menu
<?php include('one.php')?>
<ul>
<li>Menu one</li>
<?php foreach( $links as $link){ ?>
<li><a href="<?php print $link['url']?>"><?php print $link['text']?></li>
<?php } ?>
</ul>

How to have the class="selected" depending on what the current page/url is

This is my first post so forgive as I am just new in the world of web development.
Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.
Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.
My question is how do I solve this problem:
1.) To have the class="selected" depending on what the current page/url is.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
Thank You :)
If you're looking for a non-javascript / php approach...
First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this
HTML within php file
Call a php function inline within the hyperlink <a> markup passing in the links destination request uri
<ul>
<li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
<li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
<li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
<li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
<li>Support
<ul>
<li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
<li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
</ul>
</li>
</ul>
PHP function
The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class
<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri)
echo 'class="selected"';
}
?>
You could ID each link and use JavaScript/Jquery to add the selected class to the appropriate link.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li id="home-page">Home</li>
<li id="about-page">About </li>
<li id="services-page">Services </li>
<li id="features-page">Features</li>
<li id="support-page">Support
<ul>
<li id="support1-page">Support 1</li>
<li id="support2-page">Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
On your content page use jQuery to do something like:
$(document).ready(function(){
$("#features-page").addClass("selected");
});
Another method you could use is:
Add class element based on the name of the page
Give each link a separate id then use jQuery on the individual pages.
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
On the services page:
$(document).ready(function(){
$("#services").addClass("selected");
});
Or even better as robertc pointed out in the comments, there is no need to even bother with the id's just make the jquery this:
$(document).ready(function(){
$("[href='services.php']").addClass("selected");
});
One variant on Chris's approach is to output a particular class to identify the page, for example on the body element, and then use fixed classes on the menu items, and a CSS rule that targets them matching. For example, this page:
<DOCTYPE HTML>
<head>
<title>I'm the about page</title>
<style type="text/css">
.about .about,
.index .index,
.services .services,
.features .features {
font-weight: bold;
}
</style>
</head>
<body class="<?php echo basename(__FILE__, ".php"); ?>">
This is a menu:
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
</ul>
</body>
...is pretty light on dynamic code, but should achieve the objective; if you save it as "about.php", then the About link will be bold, but if you save it as "services.php", then the Services link will be bold, etc.
If your code structure suits it, you might be able to simply hardcode the page's body class in the page's template file, rather than using any dynamic code for it. This approach effectively gives you a way of moving the "logic" for the menu system out of the menu code, which will always remain the same for every page, and up to a higher level.
As an added bonus, you can now use pure CSS to target other things based on the page you're on. For example, you could turn all the h1 elements on the index.php page red just using more CSS:
.index h1 { color: red; }
You can do it from simple if and PHP page / basename() function..
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="selected" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="selected" <?php } ?>>About</a> </li>
<li><a href="services.php" <?php if (basename($_SERVER['PHP_SELF']) == "services.php") { ?> class="selected" <?php } ?>>Services</a> </li>
<li><a href="features.php" <?php if (basename($_SERVER['PHP_SELF']) == "features.php") { ?> class="selected" <?php } ?>>Features</a></li>
</ul>
</div>
</div>
Sorry for my bad English, however may be it could help. You can use jQuery for this task. For this you need to match the page url to the anchor of menu and then add class selected to it. for example the jQuery code would be
jQuery('[href='+currentURL+']').addClass('selected');

Categories