As of right now I have this code in one of my templates on my Wordpress theme.
// blah blah
<div class="content">
<?php include_once( 'http://www.website.com/new/file.php?variable=1' ); ?>
</div>
// blah blah
Now, it will load everything in this template except this php include! It will load the sidebar which is included above the /* blah blah */ and it will load the footer, navigation, etc.
When I Inspect Element on Google Chrome it shows this:
<div class="content">
(it's empty)
</div>
Please help me if you know what I'm doing wrong! Thank you!
include and require don't take URLs. They take server filepaths. Try this:
<?php include_once(ABSPATH.'/new/file.php'); ?>
EDIT: Forgot to mention that query strings cannot be included in filepaths. If a query string is necessary for proper loading, consider loading the page with all necessary query variables in your current URL, loading the part using an iframe, or using file_get_contents()
Iframe:
<iframe src="http://www.website.com/new/file.php?variable=1"></iframe>
file_get_contents()
<?php echo file_get_contents('http://www.website.com/new/file.php?variable=1'); ?>
Related
I'm building my own CMS system and I want to create new pages dynamicly from a template. Just like in wordpress when you add a new Page.
This is the template:
<?php require_once('backend-nav.php');?>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
// My content needs to go here!
</article>
<?php require_once('backend-sidebar.php')?>
</div>
</div><!-- End main content container -->
<?php require_once('backend-footer.php')?>
<?php } else {
echo '<div class="container">You have to be logged in to view this page:.
'Login'.'</div>';
}
?>
I have made a form to submit the content that I want on the page, and then use the following code to open the templatet php file and save it as a new file on the server with the content for the page
$doc = new DOMDocument();
$doc->loadHTMLFile("new_page.php");
$article = $doc->getElementById('article-wrapper');
$p = $doc->createElement('p');
$addP = $article->appendChild($p);
$content = $doc->createTextNode($page_content);
$addP->appendChild($content);
// the url for the page is also submitted to the form and later added to the menu, which works.
$doc->saveHTMLFile($page_url.'.php');
Since there is no loadPHP function I'd recon I use this one. It also worked for me when adding the link to my main menu, which is also a PHP file.
Now the content gets added to the file, and is saved accordingly but for some reason it fucks up the code in some places like this, some sign are replaced like ? and > etc.:
require_once('backend-header.php');
?>
<?php if (isset($_COOKIE['username'])) { ?>
<?php require_once('backend-nav.php');?>
<html>
<body>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
<p>test content added in p tags</p></article>
<?php require_once('backend-sidebar.php')?> </div><!-- End main content container -->
</div>
<?php require_once('backend-footer.php')?><?php } else {
echo '<div class="container">You have to be logged in to view this page: ' .
'Login'.'';
}?></body></html>
the PHP ending tag before the html end tag is replaced
I have also tried fread/write to alter the file but I probably are not using things the right way.
Is there a way to add code to php file with php, or a other way to get what I'm trying to do?
Thanks!
DOMDocument only use to read XML and HTML, these have a structure. When you insert PHP code into html file, it is not realy a html anymore. Let see an example below.
The html code:
<a>text</a>
There is a node that named "a" have a content. DOMDocument can understand it well.
But
<a><?php if (false) : ?>true</a><? else: ?>false</a><?php endif ?>
DOMDocument can not understand php and it will read the first < /a> as the closer of < a>. How about the second one, the reader may try to read by fixing it or just ignore it or append something to make it become structured. So, you can not use DOMDocument in this case. You could try to use file_get_contents and replace the content then use file_put_contents to write it back.
I have such type of code in view, add.ctp file in Cake PHP.
<div id="container">
<div id="content">
------------------
</div>
<div id="sidebar">
----------------
</div>
</div>
Now in Layout, in default.ctp file, we access this code by this line.
<?php echo $this->fetch('content'); ?>
I have sidebar in each and every view file, and if I need some changes then I will go in each and every file and then change.
Now My Question is that, can I made a file in layout like sidebar.ctp or any thing else that I just call this file in my view. If I can, then how I will made such type of file.
You could do it with include or elements like this
<?php echo $this->element('sidebar'); ?>
With the element, you make the sidebar.ctp file in the View/Elements/ folder.
Check for more information: Cakephp 2 Elements
The other way is with include (not my choice, but another way to accomplish it)
<?php include('../View/Layouts/sidebar.ctp'); ?>
You can use elements and if the content in elements is dynamic you can use the blocks supported in latest version of cakephp.
http://book.cakephp.org/2.0/en/views.html
Im working on a Wordpress portfolio site and there is a line of text
<?php get_template_part( 'post-meta-page' ); ?>
that pulls information from that file and displays it on the page. Right now it displays only the title of the blog/page. I want it to display a unique line of text for each page while keeping the original title. I looked into it a little bit and this could be referred to as parsing?
Example: from external file.
<html>
<p class="homepage">Text for homepage</p>
<p class="interior">Text for interior page</p>
<p class="contact">Text for content page</p>
</html>
In other words, I want to call a specific class on one page and another class in another page. Any snippet of Javascript or PHP that can do this?
You need to edit the post-meta-page page and check where you are and add the appropirate text.
For example:
if(is_home()){
<p class="homepage">Text for homepage</p>
}
For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?
But for the moment, in every single view I got code like the following:
<?php $this->_include('/includes/doctype.php'); ?>
<head>
<?php $this->_include('/includes/head.php'); ?>
<title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">
<h1>Some title</h1>
<p>Blah blah blah.</p>
</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>
Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable?
After that how will it be possible to customize the title and the actual menus?
I do this all the time. I have a custom header and footer file that are called at the start and end of every page.
<?PHP
Require("includes/header.php");
...
Require("includes/footer.php");
?>
The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.
The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.
This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.
I like variables akin to:
$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar. NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used. Left pane.
$display_right_column - page contents when two columns used. Right pane.
$display_footer - appears in footer division.
My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.
Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.
// Code here generates the menu from database
// Code here genereates popup alert messages from other users
//permanent links to external style sheets go here.
//You can also select skins here.
<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>
<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>
</head>
<body <?PHP echo $display_onload_scripts;?> >
<div id="page_area" >
<div id="banner">
</div>
<?php
echo $display_above_menu;
if(!$hide_menu){echo $display_menu;} //Insert the menu variable here.
echo $display_below_menu;
?>
<div id="content_area">
<div id="inner_content">
<?PHP
if($display_number_of_columns==1)
{
echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
}
if($display_number_of_columns==2)
{
echo "<div id='leftcolumn'>".$display_left_column."</div>"; //these are left for legacy support from before I got better at CSS.
echo "<div id='rightcolumn'>".$display_right_column."</div>";
}
echo "<div id='footer'>".$display_footer."</div>"; //just in case - I hardly use it.
echo $display_pop_box; //for user alert messages to other users
?>
</div>
</div>
</div>
<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.
</body>
</html>
<?PHP
$mysqlidb->close();
?>
Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.
What I'm trying to do:
I have two files: One is the header.php which is included (by php include) into my second file content.php
I am going to be use my header.php with many different 'content' files, just so you know
Now, I have a div that is going to display a title text provided by content.php and any other pages that includes the header.php: <div class="title"> </div> This div is only in the header.php.
My problem is that I do not know how to include a text into the header.php div area <div class="title"> </div> from my content.php which again includes the header.php.
Header.php (contains the <div class="title"></div>)
Content.php (needs to have the title text and be included into the header.php <div class="title"> <div> area)
My question is: How in heck can I do this with php?
And I am not very knowledgeable about php or any type of coding for that matter. I have just been winging it, now I'm rather stuck. So If anyone could possibly explain how to do this in a simplified way, I would really appreciate it.
A very basic simple system would work like this:
header.php:
<div class="title"><?php if (isset($title)) { echo $title; } ?></div>
content.php:
<?php
$title = 'This is my content page';
?>
blah blah blah html here.
<?php include('header.php'); ?>
blah blah more html her e
header.php would check for the presence of a "title" variable, and output it if need be. If there's no title varaible, you get an empty div.
The content page is responsible for setting the title variable. And when the header page is included, it'll be in the same "scope" as the content.php code, pick up the variable, and run with it.