I've got a PHP page (content.php), containing plain HTML and content delivered by PHP variables
<html>
<head>
</head>
<body>
<h1><?php echo $title; ?></h1>
</body>
</html>
Then there is another PHP page, where I need the contents of content.php in a String, but already processed by the PHP parser. I already tried the file_get_contents() function, but this gives the raw output (PHP not processed). What I'm looking for, is this:
$var contents = somefunction('contents.php') with content:
<html>
<head>
</head>
<body>
<h1>Title</h1>
</body>
</html>
Any help much appreciated!
Try:
ob_start();
include ('contents.php');
$html = ob_get_clean();
ob_end_clean();
Have you tried include? Alternatively, if you need to get it from "outside" (like if you loaded it in your browser) use file_get_contents with the full http://example.com/filename.php URL.
either i am missing something badly here or else instead of using a function, in your second page why not just directly assign the contents to a string like this:
$my_String = "<html>
<head>
</head>
<body>
<h1>". $title ."</h1>
</body>
</html>";
Related
I'm trying to make a simple php script that puts a list in the header, here is what I have
<html>
<head>
<?php
function menu1($link1, $button1) {
echo "<ul><li><a href = '$link1'>$button1</a></li></ul>";}
menu1('index.php','Home')
?>
</head>
<body>
<p>Home</p>
</body>
</html>
Yet for some reason when I run the script it outputs this
<html>
<head>
</head>
<body>
<ul><li>Home</li></ul>
<p>Home</p>
</body>
</html>
Is there any way I can get this to post in the head?
Thanks in advance!
The <head> element can only contain certain special elements, such as <title>, <link>, and <meta>, among others. It cannot contain any elements which are rendered on the page.
If you view source, you'll see that the markup is being output the way you requested. However, this markup is invalid, and is being "repaired" by the browser by moving these elements into the <body>.
i have an h1, which i want to get the text from and then exacute it as an echo. for example something like this - http://jsfiddle.net/mZGH7/.
<html>
<head>
<title><?php echo (".example"); ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
While you could do that with PHP's DOM extension and XPath:
<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile(__FILE__);
$xpath = new DOMXPath($dom);
?>
<html>
<head>
<title><?php echo $xpath->evaluate('string(//h1[#class="example"])'); ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
or with a third party lib implementing Selectors, like ZF's DOM Query component:
<?php
libxml_use_internal_errors(true);
$dom = new Zend_Dom_Query(file_get_contents(__FILE__));
?>
<html>
<head>
<title><?php echo $dom->query('.example')->current()->nodeValue; ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
it is completely pointless to do it that way, because it takes much more time and is much more complicated than what you really should do, namely
<?php $title = 'this is going to be the title'; ?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1 class="example"><?php echo $title ?></h1>
</body>
</html>
If that is not an option, use JavaScript and make sure to read:
http://www.developer.com/tech/article.php/923111/Client-side-Versus-Server-side-Coding---Part-1.htm
I think you are mixing php with javascript. Something like this works:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
<script>
var salida = $(".example").html();
$('title').html(salida);
</script>
</body>
</html>
Quick Short answer
Create a html hidden field, can be accessed, both, by JavaScript, and PHP, at the same time.
Extended Boring answer
Developing a web page is different than developing a desktop application.
When a html page has controls, like text, radiobuttons, checkboxes. And the web page was generated dinamically using PHP. And the user sends them back to the server, the values can be accessed like variables in php.
There is something called "hidden field" that acts like a variable, or control, but, its contents, its not displayed, as it happens with a textbox control.
Remember that a webpage is executed 2 times, first, generated at the server, using PHP, HTML or another web language, and later, at the client's browser, with HTML, and JavaScript, if there is any.
Summary
You may want to learn about how html controls are generated and read back int PHP, and access with Javascript.
as everyone else already said, it's not the best way to do it (using PHP, you should rather use javaScript if there aren't more complexities involved). that being said, PHP allows you to do it, by use of output control functions, like so :
<?php
function callback($buffer)
{ //using Gordon's answer ( http://stackoverflow.com/a/9245074/487891 )
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($buffer);
$xpath = new DOMXPath($dom);
$buffer = str_replace("{{my_title}}",$xpath->evaluate('string(//h1[#class="example"])'),$buffer);
return $buffer;
}
ob_start("callback");
?>
<!-- and then your html -->
<html>
<head>
<title>{{my_title}}</title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
<?php ob_flush(); ?>
i haven't tested this, so there might be errors in this code, but this would be the basic idea
It appears to me as if you are trying to run PHP client side. PHP is server side. It generates the HTML and passes it to the browser. Once the HTML is generated, you then can use Javascript to manipulate the DOM. For that, you'll really want to use jQuery.
I can't think of a way to do that in PHP, but some some simple Javascript should do it. Something like this:
document.title = document.getElementsByTagName("h1")[0].innerHTML;
Here is the structure of the web site:
PHP index file
//my class for analyzing the PHP query
$parameter = new LoadParameters();
//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
// $parameter->design - PHP file which contains the design
// HTML code of the page
// $parameter->content - Different PHP file which should be loaded
// inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);
//load the design file
include($parameter->design);
PHP design file
Just the generic structure. Obviously it has a lot more design elements.
<html>
<head>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Question
So here is the problem I experience. The $parameter->content file is a dynamic PHP file, meaning the content also changes according to the query.
For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.
This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:
<html>
<head>
...
</head>
<body>
...
<title>sometitle</title>
...
</body>
</html>
And having <title></title> within <body></body> is not allowed.
So how can I change the title without breaking the XHTML markup validation?
Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.
Thanx in advance.
why not do a second include to perform the title in the proper place?
<html>
<head>
<?php
inlcude($parameter->title);
?>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Can't you just change the PHP code so that you can do something like:
<html>
<head>
<title><? print($parameter->title); ?></title>
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
I'd move all of the <head> code into a 'common function' called something like html_head($title) and then have it put the title where it belongs.
Then simply call that function from within the pages and it's fixed.
Don't forget to include the <body> tag in that function, otherwise it won't work!
Elaborating ;)
function html_head($title) {?>
<html>
<head>
<title><?=$title?></title>
<!-- Put whatever you want... here! -->
</head>
<body>
<?}
Then in $parameter->content, call html_head("Title")
It would be easier if $parameter->content could be included without displaying its HTML code, but instead have a $parameter->display (or similar) function that displays the HTML code. That way, you can include the PHP code at the beginning of the file and not worry about being unable to access the title.
<?php
require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title><?php echo $parameter->title; ?></title>
</head>
<body>
<?php
echo $parameter->display;
?>
</body>
</html>
This is how I solved the issue.
I changed the PHP design to something like:
//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);
//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
<head>
<title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
</title>
</head>
<body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
</body>
</html>
EndHere;
//output the html
echo $html;
And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.
//set the title
$parameter->title = "sometitle";
//set the content HTML
$parameter->html = "some HTML here";
It's not a very clean solution but it works fine.
I'm using a file, page.php, as an HTML container for several content files; i.e., page.php defines most of the common structure of the page, and the content files just contain the text that's unique to every page. What I would like to do is include some PHP code with each content file that defines metadata for the page such as its title, a banner graphic to use, etc. For example, a content file might look like this (simplified):
<?php $page_title="My Title"; ?>
<h1>Hello world!</h1>
The name of the file would be passed as a URL parameter to page.php, which would look like this:
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php include($_GET['page']); ?>
</body>
</html>
The problem with this approach is that the variable gets defined after it is used, which of course won't work. Output buffering also doesn't seem to help.
Is there an alternate approach I can use? I'd prefer not to define the text in the content file as a PHP heredoc block, because that smashes the HTML syntax highlighting in my text editor. I also don't want to use JavaScript to rewrite page elements after the fact, because many of these pages don't otherwise use JavaScript and I'd rather not introduce it as a dependency if I don't have to.
Most people store the output of the included page into another variable. Have you tried putting all the content of the included page into the output buffer, then storing the ob_get_clean() into a variable like $page_html, then having your page look like this:
<?php include($_GET['page']); ?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $page_html; ?>
</body>
</html>
Edit: So the second page would look something like this:
<?php
$page_title="My Title";
ob_start();
?>
<h1>Hello world!</h1>
<?php $page_html=ob_get_clean(); ?>
The best thing I can think of would be to separate the inclusion of the file from the rendering. So your template looks like this:
<?php include($_GET['page']); ?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php renderPage() ?>
</body>
</html>
And the file you are including looks like this:
<?php
$page_title="My Title";
function renderPage() {
?>
<h1>Hello world!</h1>
<?php
}
?>
This is also nice since you can pass parameters to renderPage() so that the template can pass info along to the page it is including.
Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx is content. I want to display the contents of b.aspx inside a <div> on a.aspx. I know with PHP you can do it like so:
//a.php
<html>
<head>
<title>test</title>
</head>
<body>
<?PHP
include "b.php";
?>
</body>
</html>
//b.php
<?PHP
echo "Content String";
?>
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
Thanks!
This scenario is handled by masterpages and or composing the page out of (user)controls in ASP.NET. As described at for instance here.
Probably Server.Execute will help.
//a.aspx
<html>
<head>
<title>test</title>
</head>
<body>
<% Server.Execute("b.aspx"); %>
</body>
</html>
//b.aspx
Content String
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
By the way, I do not recommend this approach. It's just to show it can be done. Master pages and user controls are normally the way to go.
create a B.ascx that does everything you need, and then both B.aspx and A.aspx can include that control.
It sounds like MasterPages will accomplish this for you. Is this not an option for you?
you can go old-skool and use an IFRAME
alternatively, could use a WebRequest in a.aspx.cs to open b.aspx, store the results in a string, and return that string inside a div on a.aspx