Reputation: 37
I have a Php file for include functions and a css page. I am trying to make a include function to generate most of my html. I just can not figure out how to get the css file to be in the php include function I made.
<?phpe
function HTMLStart( $Title)
{
echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo "<head>\n";
echo " <title>".$Title."</title>\n";
echo " <link href=\ 'IncLab.css' type=\"text/css\" rel=\"Stylesheet\" />\n";
echo "</head>\n";
echo "<body>\n";
}
?>
The problem I have is with the echo
line:
echo " <link href=\ 'IncLab.css' type=\"text/css\" rel=\"Stylesheet\" />\n";
I tried to use double quotes and get an error, so I tried to use single quotes for the "IncLab.css and got no error. I was wounding if this is the right way to do it.
Also, I made a navigation bar in my include file but how to I put in a class (For CSS) to get my links to line up correctly.
<?php
function PageNagivation()
{
echo "<!-- Nav of the page --> \n";
echo "<nav> \n";
echo '<div class="nav-item float-left"> <a href="IncLabPage1.php">Page 1</a>';
echo '<div class="nav-item end-float"> <a href="IncLabPage2.php">Page 2</a>';
echo "</nav> \n";
}
?>
The navigation bar works and display but does not display correctly. The problem i know lies with the <div class....
part of the code. How would i get it to work. Thanks.
Upvotes: 3
Views: 137
Reputation: 11
I don't recommend having html echoed out using php. However, in your code you aren't escaping a href=\ '... Just use single quotes:
echo " <link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";
Regarding the navigation, you aren't closing your divs. Should be like this:
echo "<!-- Nav of the page --> \n";
echo "<nav> \n";
echo '<div class="nav-item float-left"> <a href="IncLabPage1.php">Page 1</a></div>';
echo '<div class="nav-item end-float"> <a href="IncLabPage2.php">Page 2</a></div>';
echo "</nav> \n";
Without knowing what you want your nav to look like and not having the css for the classes that you are calling, it is difficult to assist further. Normall, a nav is in an unordered list like this:
<ul class="nav">
<li><a href="IncLabPage1.php">Page 1</a></li>
<li><a href="IncLabPage2.php">Page 2</a></li>
</ul>
CSS:
ul.nav {
list-style: none;
}
ul.nav li {
display: inline-block;
}
Upvotes: 0
Reputation: 61
For the quote problem, yes it is perfectly acceptable to use single quotes in that situation, that's what they are for. You were escaping one of them, which you don't need to do:
echo "<link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";
For adding the class, you can just input it into the HTML like:
echo '<div class="nav-item end-float myclass"> <a href="IncLabPage2.php">Page 2</a>';
Or, if it is stored in a variable:
echo '<div class="nav-item end-float '.$myclass.'"> <a href="IncLabPage2.php">Page 2</a>';
Or
echo '<div class="nav-item end-float $myclass"> <a href="IncLabPage2.php">Page 2</a>';
Upvotes: 6
Reputation: 2645
You can avoid problems like this by properly leveraging the "template" part of the PHP language.
<?php
function HTMLStart( $Title)
{
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $Title ?></title>
<link href="IncLab.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<?php
}
?>
Upvotes: 1
Reputation: 429
Instead of a single echo for a single line, why you don't do this?
$sOutput = '<html>
<head>
<link href="IncLab.css" type="text/css" rel="Stylesheet">
<!-- Any HTML you want to echo -->';
echo $sOutput;
Or better, use the heredoc syntax:
$sOutput = <<<EOT
<html>
<head>
<link href="IncLab.css" type="text/css" rel="Stylesheet">
<!-- Any HTML you want to echo -->
EOT;
Just a single quote at start and at the end and you can even indent it a bit... And you get rid of worrying escaping so many quotes.
Upvotes: 2
Reputation: 34416
Change the quotes like this -
echo " <link href='IncLab.css' type='text/css' rel='Stylesheet' />\n";
You should do this with all of the HTML you're echoing to keep your code clean and troubleshooting simplified.
Upvotes: 3