I'm attempting to make a template based system to deliver content but I've run into a problem that I just can't seem to solve. When I try to echo out variables that have data from includes it gets outputted in the wrong section of my html.
Below is 'newstuff.php' which is my page to be executed on the browser, the offending variables are $php $head $content.
<?php
$php = include "templates/content/newstuff/phpCode.php";
$head = include "templates/content/newstuff/head.html";
$content = include "templates/content/newstuff/content.php";
include realpath(dirname(__FILE__)).'/templates/templateMain.php';
?>
Below is 'tempalteMain.php' this is my tempalte. Note the location of the echoing of $php $head $content.
<?php
echo $php;
?>
<!DOCTYPE html>
<html>
<head>
<?php echo $head; ?>
</head>
<body class="body1" onload="inputBlur2()">
<div class="borderLine"></div>
<div class="banner"></div>
<div class="mainContent1" >
<div class="header1" >
<div class="headerContainer" >
<ul class="navList1">
<li><a id = "B0" href="index.php">New Stuff</a></li>
<li><a id = "B1" href="MainPage.php">Products</a></li>
<li><a id = "B2" href="ProjectsPage.php">Projects</a></li>
<li><a id = "B3" href="AOrdering.php">About Ordering</a></li>
<li><a id = "B4" href="ContactMe.php">Contact Us</a></li>
<li><a id = "B5" href="FAQPage.php">FAQ</a></li>
<li><a id = "B6" href="SCart.php">My Cart</a></li>
</ul>
</div>
</div>
<div class="content1">
<?php echo $content; ?>
</div>
</div>
</body>
</html>
Below is 'head.html' this provides the code to be delivered by the $head PHP variable.
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/../StylePR.css">
<title>KickUp Electronics</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
Below is 'content.php' this provides the code to be delivered by the $content PHP variable.
<p>Welcome to the new stuff page!!!</p>
Finally, this is the page source that gets outputted taken from the chrome DOM editor. Note the locations of the information from content.php are wrong and there are strange '1's that are echoed out (also, when viewing the page source, the information from head.html is placed outside the html tags).
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/../StylePR.css">
<title>KickUp Electronics</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body class="body1" onload="inputBlur2()">
<p>Welcome to the new stuff page!!!</p> <!--Wrong Location!-->
1
1
<div class="borderLine"></div>
<div class="banner"></div>
<div class="mainContent1">
<div class="header1">
<div class="headerContainer">
<ul class="navList1">
<li><a id="B0" href="index.php">New Stuff</a></li>
<li><a id="B1" href="MainPage.php">Products</a></li>
<li><a id="B2" href="ProjectsPage.php">Projects</a></li>
<li><a id="B3" href="AOrdering.php">About Ordering</a></li>
<li><a id="B4" href="ContactMe.php">Contact Us</a></li>
<li><a id="B5" href="FAQPage.php">FAQ</a></li>
<li><a id="B6" href="SCart.php">My Cart</a></li>
</ul>
</div>
</div>
<div class="content1">
1 </div>
</div>
</body></html>
I tried searching many times for a solution to no avail. Is this a problem with the echo being executed before the html fully loads? Any help would be highly appreciated!
You're using includes wrong.
$php = include "templates/content/newstuff/phpCode.php";
is immediately outputting the output of that file, and setting $php to 1 (i.e. "it worked!").
Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. - http://php.net/manual/en/function.include.php
You can use output buffering to capture the output, but a better solution is probably moving the include calls directly into templateMain.php.
Related
I have a problem with highlighting using php include.
Here's my navigation bar:
<?php
echo '
<link rel="stylesheet" href="css/index-css.css" type="text/css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/womsy/index.php">Womsy</a>
</div>
<ul class="nav navbar-nav">
<li <?php echo $active[1] ?>Home</li>
<li <?php echo $active[2] ?>Blog</li>
<li <?php echo $active[3] ?>Projects</li>
<li <?php echo $active[4] ?>Contact</li>
<li class="active">About</li>
</ul>
</div>
</nav>';
$active[$current] = "class=active";
?>
I made this with bootstrap, and am trying to highlight via this: http://webdevjunk.com/coding/css/17/php-menu-includes-with-css-style-to-highlight-active-page-link/
<?php
$current = 1;
include 'php/menu.php';
?>
This is what I use on the page, but it doesn't highlight, but when I change the class in the <li> it does work.
Can someone help me figure this out?
function setActive($pageName)
{
if (stripos($_SERVER['REQUEST_URI'], $pageName)) {
echo 'class="active"';
}
}
//$pageName will be home ,blog,projects and contacts
Declare page variable at the very top of each individual page.
Example: <? $page="home";?> goes at top of homepage and <? $page="blog";?> would go on top of blog page, etc.
For each list item in your menu add if statement with corresponding variable and active class.
Example: <ul><li class="<?if($page=="home"){?>active<?}?>"><b>Home</b></li><li class="<?if($page=="blog"){?>active<?}?>"><b>Blog</b></li></ul>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I have an Excel Template that i use to fill in data and export that to CSV to populate the following page on my website.
http://play.mink7.com/ifocus_v4/careers.php
When i export the file in Windows i get the formatting for the new line charcter right. But when i export the same excel file from MAC i dont get the new line character.
Windows File
play.mink7.com/ifocus/win.csv
and
MAc File
play.mink7.com/ifocus/mac.csv
.
My Code
<?php
include_once("libs/csv2json/CSVParser.php");
$jsonParser = CSVParserFactory::Create("json");
$path = "uploads/jobs.csv";
//This property will use the first row to convert the results into a
//json object array
$jsonParser->IsFirstRowHeader = true;
$jobs = $jsonParser->Parse($path);
$jobs = json_decode($jobs, true);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>iFocus - Careers</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,400,700,300' rel='stylesheet' type='text/css'>
<script src="js/modernizr.custom.34639.js"></script>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.accordion.min.js"></script>
<link rel="stylesheet" href="css/normalize.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div id="header" class="wrapper">
<header class="container">
<div id="logo"><img src="img/logo.png" width="185" height="69" alt="ifocus logo"></div>
<nav id="primaryNav">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Case Studies</li>
<li>Services</li>
<li>Testimonials</li>
<li><a class="active" href="careers.php">Careers</a></li>
<li>Contact Us</li>
</ul>
</nav>
<div class="clearfix"></div>
</header>
</div>
<div id="mainBody">
<div class="container">
<h3>Careers</h3>
<h4>iFocus is looking for experienced professionals in the following areas: </h4>
<p>Email your resume to jobs#ifocussystec.com</p>
<div class="active-jobs">
<?php $i = 1; ?>
<?php foreach($jobs as $j): ?>
<h3 class="accordion" id="nav-section<?php echo $i++; ?>"><?php echo $j['job_code']." - ".$j['title']; ?><span></span> </h3>
<div class="accordion-content">
<p><strong>Relevant Experience:</strong> <?php echo $j['experience']; ?></p>
<p><strong>Job Description::</strong></p>
<?php $job_desc = explode("\n", $j['job_desc']);?>
<ul>
<?php foreach($job_desc as $jd):?>
<li><?php echo $jd; ?></li>
<?php endforeach; ?>
</ul>
<p><strong>Skill Set:</strong></p>
<?php $skillsets = explode("\n", $j['skillsets']);?>
<ul>
<?php foreach($skillsets as $s):?>
<li><?php echo $s; ?></li>
<?php endforeach; ?>
</ul>
<p><strong>Key Words:</strong> <?php echo $j['keywords']; ?></p>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.accordion').accordion({
defaultOpen: 'nav-section1',
cookieName: 'accordion_nav'
});
});
</script>
</body>
</html>
Lib Link: https://github.com/crosbymichael/php-csv-to-xml-json
i have tried using "\r\n" but it doesnt give the formating either
PHPs file functions do not reliably recognize line endings from Macs (if they use \r character, as Mac Excel does, if I recall correctly.)
You need to set auto_detect_line_endings in php.ini to true, then it will properly recognize line endings, whether they are \n, \r, or \r\n.
This setting is PHP_INI_ALL, which means that it can be set anywhere, i.e., in php.ini, in .htaccess for a particular directory, or even with ini_set, e.g., ini_set('auto_detect_line_endings', true)
So I'm redesigning some tools at work to make a cohesive user experience and I have 2 files that I will include in all the rest. They are a header and a footer. They work independently but when I try to include them in the same file the footer does not show (I even tried putting them in one include file).
Here's the header:
<?php
function pageHeader($title){
$html = "<!DOCTYPE html>
<html>
<head>
<link href='../../bootstrap/css/bootstrap.css' rel='stylesheet'>
<title>$title</title>
</head>
<body>
<div class='navbar navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container-fluid'>
<a class='brand' href='/platform'>Platform Tools</a>
<div class='btn-group pull-right'>
<a class='btn dropdown-toggle' data-toggle='dropdown' href='#'>
<i class='icon-user'></i>
Username
<span class='caret'/>
</a>
<ul class='dropdown-menu'>
<li>
<a href='#'>Profile</a>
</li>
<li class='divider'/>
<li>
<a href='#'>Sign Out</a>
</li>
</ul>
</div>
<div class='nav-collapse'>
<ul class='nav'>
<li class='active'>
<a href='#'>
<i class='icon-home icon-white'></i>
APEX Home
</a>
</li>
<li>
<a href='/platform/cms'>
<i class='icon-home icon-white'></i>
CMS Home
</a>
</li>
<li>
<a href='/platform/outbound'>
<i class='icon-home icon-white'></i>
Outbound Home
</a>
</li>
<li>
<a href='/platform/urs'>
<i class='icon-home icon-white'></i>
URS Home
</a>
</li>
<li>
<a href='#bug'>
<i class='con-fire icon-white'></i>
Report a problem
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<script src='/bootstrap/js/jquery.js'/>
<script src='/bootstrap/js/bootstrap-transition.js'/>
<script src='/bootstrap/js/bootstrap-alert.js'/>
<script src='/bootstrap/js/bootstrap-modal.js'/>
<script src='/bootstrap/js/bootstrap-dropdown.js'/>
<script src='/bootstrap/js/bootstrap-scrollspy.js'/>
<script src='/bootstrap/js/bootstrap-tab.js'/>
<script src='/bootstrap/js/bootstrap-tooltip.js'/>
<script src='/bootstrap/js/bootstrap-popover.js'/>
<script src='/bootstrap/js/bootstrap-button.js'/>
<script src='/bootstrap/js/bootstrap-collapse.js'/>
<script src='/bootstrap/js/bootstrap-carousel.js'/>
<script src='/bootstrap/js/bootstrap-typeahead.js'/> ";
return $html;
}
?>
and Here's the Footer:
<?php
function pageFooter(){
$html ="
<nav class='footer'>
<a href='#'>Apex</a>
<a href='#'>Contest</a>
<a href='#'>Outbound</a>
<a href='#'>URS</a>
<a href='#'>Third Party Tools</a>
<a href='#'>Report a Problem</a>
<a href='#'>Our SLA</a>
<a href='#'>Contact</a>
</nav>
</body>
</html> ";
return $html;
}
?>
When I include them in my FrontDoor.php file it looks like this (going to change to relative path):
<?php include('C:\xampp\htdocs\webapps\Redesign\Oreo\src\Header.php'); ?>
<?php include('C:\xampp\htdocs\webapps\Redesign\Oreo\src\Footer.php'); ?>
<?php echo pageHeader('Platform Tools'); ?>
<?php echo pageFooter(); ?>
When I try to run it it runs the header then closes the body and html tags and doesn't run the footer.
Any Suggestions and Thanks?
You could try changing your script tags from <script src='/bootstrap/js/bootstrap-typeahead.js'/> to <script src='/bootstrap/js/bootstrap-typeahead.js'></script>
Ok, I do this all the time,
basically what I do is this
<?php include_once('Header.php'); ?>
Any Page content
<?php include_once('Footer.php'); ?>
The header includes
<html><head></head><body><div class="some-div">
And footer includes
</div></body></html>
This way you get constant header and footer, you don't need to make a separate function to close php document, it's unproductive.
EDIT:
And if you have index.php , for example, that has included files header.php and footer.php.
I checked out the code and it is rendering perfectly. Not a compulsion but you should follow the way #Toby has asked for closing script tags.
I am sure its the issue with the way you are putting your script tags. Please correct them and everything will be fine.
EDIT
When you do the view source in the browser, the footer is seen in the HTML source as expected.
Only while rendering the footer is not shown, but once you correct the way script tags are added i.e. put proper end </script> browser will render it correctly
I am Trying to get the page title (<title>bla..bla..bla..</title>) to be changable in php with a multi-file layout like so:
Functions.php is included into index.php, then get_header() is called from functions.php to include the page header.php the title tag is inside the header file. I would like to be able to set the title from index.php how can i do this?
For examle this is what i have tried:
Index.php:
<? require_once('includes/functions.php'); global $t; $t = '/home/s0urc3'; get_header();?>
<div id="main">
<h2>NEEDED</h2>
<p class="postmeta">Permalink | <span class="date">Revision Date</span></p>
<p>CONTENT AND CRAP</p>
<!-- main ends -->
</div>
<?php /*test*/echo($title);/*test*/ get_footer();?>
Header.php:
<?php //include('functions.php')?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<? title('$t')?>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<?php get_theme_css()?>
</head>
<body>
<!-- wrap starts here -->
<div id="wrap">
<!--header -->
<div id="header">
<h1 id="logo-text"><img src="<?php get_url('images/Logo.png')?>" alt="S0URC3"><!-- S0URC3 --></h1></div>
<p id="intro">
Just another poorly coded website!
</p>
</div>
<div id="nav">
<!-- <div id="navi"><div id="menu" class="fixed"> -->
<ul class="">
<li class="">Home</li>
<li class="">Blog</li>
<li class="">Forums</li>
<li class=""> Comments</li>
<!--<li class="">Clans</li>-->
<li class="">-astro-</li>
<!--<li class="">Inspiration</li>
<li class="">Resources</li>
<li class="">Tutorials</li>
<li class="">WordPress</li>-->
</ul><!-- </div></div> -->
</div>
<!--header ends-->
</div>
<!-- content-wrap starts -->
<div id="content-wrap">
Functions.php:
<?php
require_once('constants.php');
//===============//
//Start Functions//
//===============//
//Gets a file from the domain http://files01.s0urc3.ismywebsite.com/
function get_url($file)
{
echo (FILE_ROOT . $file);
}
//gets the url of the theme
function get_theme_css() {echo('<link rel="stylesheet" href="' . FILE_ROOT . 'colourise/style.css" type="text/css" />');}
function get_header() {require_once('includes/header.php');}
function get_footer() {require_once('includes/footer.php');}
//Gets the URL of the current page
function page_url($p)
{
$s = empty($_SERVER["HTTPS"]) ? ''
: ($_SERVER["HTTPS"] == "on") ? "s"
: "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
: (":".$_SERVER["SERVER_PORT"]);
if ($p == 'yes')
echo ($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
else
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2)
{
return substr($s1, 0, strpos($s1, $s2));
}
//gets the year
function cur_year() {echo (YEAR);}
function ads($code) {echo('<script type="text/javascript" src="http://links.ismywebsite.com?i='. $code .'"></script>');}
function title($title)
{echo('<title>Index Of: '.$title.'</title>');}
//=============//
//End Functions//
//=============//
?>
P.S. I only am including functions once all functions are available in the header and footer when functions.php is called in index.php
First, the file header.php is being included from within a function, so it does not have access to the global variable $t. You will need to do one of the following:
In functions.php:
function get_header() {
global $t;
require_once('includes/header.php');
}
Or, in header.php:
<?php //include('functions.php')
global $t;
?>
<!DOCTYPE html>
<!-- etc -->
That way, the variable you have declared will be available to the function's local variable scope.
Second, you need to call the function with either double quotes or no quotes at all. Single quotes will not parse a variable.
Right
title($t);
title("$t");
title("{$t}");
Wrong
title('$t');
Here is the PHP Manual Page on the String datatype -- be sure to check out single quoted strings versus double quoted strings.
Nitpicky Things:
You should always use the full opening tags (<?php), and not the short open tags (<?)
Your functions, like title(), should always return a value, and you can echo that, rather than echoing directly from inside the function.
The strleft() function you implemented, though clever, already exists. See: strstr() Actually, this function does the opposite of what you want; I mis-remembered what it did. Carry On. EDIT AGAIN: No, apparently I was right, you pass the optional third parameter as true. You should generally use functions that already exist because, with certain exceptions, they will be faster.
A Notable exception to this rule is array_rand()
Try to change
<? title('$t')?>
to
<? title($t)?>
in your code.
I am loading in the following navbar html from a required PHP file:
<ul id="navlist">
<li id="active">Home</li>
<li>About</li>
<li>News</li>
<li>Applying</li>
<li>Current <br />Residents</li>
<li>Alumni</li>
<li>Contact</li>
</ul>
Depending on the page that I am on (let's say I am on the alumni.php page) I want that list item to be given the ID "active"?
Edit: Here is my header.php code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="styles/main.css" type="text/css" media="screen" charset="utf-8"/>
<link rel="stylesheet" href="styles/navbar.css" type="text/css" media="screen" charset="utf-8"/>
<title>some title</title>
</head>
<body>
<div id="header">
<div id="left">
<img src="images/tree.png" alt="tree" width="87" height="98"></img>
</div>
<div id="right">
<
</div>
</div>
<div id="navigation">
<ul id="navlist">
<li>Home</li>
<li>About</li>
<li>News</li>
<li>Applying</li>
<li>Current <br />Residents</li>
<li>Alumni</li>
<li>Contact</li>
</ul>
</div>
I assume that I need to do this through Javascript once the page loads? How would I do this?
as said in comment, PHP will be a better way.
You can simple doing it like this :
<?php
$header = file_get_content('header.html');
$page = 'about.php';
$header = str_replace('<li><a href="'.$page.'">', '<li id="active"><a href="#">', $header);
You should assign the ID (which should be a class, semantically speaking, IMHO) using PHP whilst generating the page. Using JS is not only troublesome (you have to go and check your location, probably match a regexp, etc), but also inelegant.
I'd say that in common coding for javascript where you want a particular element to be 'active' or 'highlighted' or 'enabled', make use of the class attribute. Your id attribute implies a static attribute of the data being used.
I think this will do what you want.
<ul id="navlist">
<li id="home">
Home
</li>
<li id="about">
About
</li>
<li id="news">
News
</li>
<li id="applying">
Applying
</li>
<li id="currentResidents">
Current Residents
</li>
<li id="alumni">
Alumni
</li>
<li id="contact">
Contact
</li>
</ul>
<script type="text/javascript">
var pagePath = window.location.pathname;
var pageName = pagePath.substring(pagePath.lastIndexOf('/') + 1);
var currentActive;
function setActivePage(page)
{
if(currentActive)
document.getElementById(currentActive).removeAttribute("class");
document.getElementById(page).setAttribute("class", "active");
currentActive = page;
}
if(pageName == "about.html")
setActivePage("about");
else if(pageName == "otherpage.html")
setActivePage("otherpage");
// Etc...
</script>
If you were using jQuery this may have been done in a better and lesscode way... but I assume you're not using it.
Hope it helps :)
While it may be possible (I haven't actually tried it), you would not typically change the id of an element in the page. Instead, it would be a better approach to use class="active" instead of id="active".
Also, you probably want to generate the appropriate html for it on the server-side, as you're building the rest of the page. Something like this would work (though there are many different ways to build this code, depending on your server's implementation):
<ul id="navlist">
<li class="<?php echo (($currentPage=='Home')?'active':''); ?>">Home</li>
<li class="<?php echo (($currentPage=='About')?'active':''); ?>">About</li>
<li class="<?php echo (($currentPage=='News')?'active':''); ?>">News</li>
<li class="<?php echo (($currentPage=='Applying')?'active':''); ?>">Applying</li>
<li class="<?php echo (($currentPage=='Residents')?'active':''); ?>">Current <br />Residents</li>
<li class="<?php echo (($currentPage=='Alumni')?'active':''); ?>">Alumni</li>
<li class="<?php echo (($currentPage=='Contact')?'active':''); ?>">Contact</li>
</ul>
Note: I've also removed the id="current" attribute from the anchor (<a ...>), because I'm assuming that this would change depending on the current page as well, and it's unnecessary, because you can build CSS selectors to address the anchor, without giving it its own special id or class.
Here's what your CSS might look like:
#navlist li.active {
/* css rules for the active LI */
}
#navlist li.active a {
/* css rules for the active (a.k.a. "current") anchor inside the active LI */
}
hope this helps.
[edit] As I said above, it all depends on the architecture of your php code. But assuming that you have a bunch of php pages (eg: "Home.php", "About.php", "News.php", etc.); and each of those pages includes your nav code using something like: require("nav.php");. Then you can just do the following in each of your main php files:
<?php
/* $currentPage, declared here, will be available to php code inside nav.php */
$currentPage = strtolower(basename(__FILE__));
require("nav.php");
?>
Just be sure that you set $currentPage, in each page's main script, somewhere prior to including your nav code (ie. before you call require(...)). The nav code will then be able to "see" $currentPage and use it.
So, for example, if the above code is executed in a file called "About.php", then $currentPage will be set to "about.php" (filename gets converted to all lowercase by the call to strtolower(...)). Then, when "nav.php" gets included, it will be able to access $currentPage and "see" that we're on the 'about' page.
You can change my example above, as follows, to use values of $currentPage that were generated from the filename using the approach I've described here.
<ul id="navlist">
<li class="<?php echo (($currentPage=='home.php')?'active':''); ?>">Home</li>
<li class="<?php echo (($currentPage=='about.php')?'active':''); ?>">About</li>
<li class="<?php echo (($currentPage=='news.php')?'active':''); ?>">News</li>
<li class="<?php echo (($currentPage=='applying.php')?'active':''); ?>">Applying</li>
<li class="<?php echo (($currentPage=='residents.php')?'active':''); ?>">Current <br />Residents</li>
<li class="<?php echo (($currentPage=='alumni.php')?'active':''); ?>">Alumni</li>
<li class="<?php echo (($currentPage=='contact.php')?'active':''); ?>">Contact</li>
</ul>