I have main site to which i put code using require function, but when i add php functions into it, they appear as plain text. The php code is rather long so i won't paste it here. Could anyone help me with that?
Thanks
ok i am adding some code:
require part with only one function:
<?php
$content=<<<EOF
echo 'hello';
EOF;
require 'whatever.php';
?>
and main part:
<?php
echo <<<CONT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
$content
</div>
<div id="add_func">
</div>
</body>
</html>
CONT;
?>
Make sure that You wrap your code in <?php.....?> tags.
Update:
No need to use the heredoc syntax for what you are doing, you could modify you code like this:
<?php
echo 'hello';
require 'whatever.php';
?>
And:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content; ?>
</div>
<div id="add_func">
</div>
</body>
</html>
just get rid of heredoc
require part
<?php
echo 'hello';
require 'whatever.php';
?>
main part
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content ?>
</div>
<div id="add_func">
</div>
</body>
</html>
that's all
PHP is not Perl, heredoc is useless here, even if properly used.
Just never use this ugly syntax.
Related
Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try
So im trying to import a header.php into my index.php file file but it just doesnt work.
index.php:
<?php
require "header.php";
?>
<main>
<p> online! </p>
<p> offline! </p
</main>
<?php require "footer.php"; ?>
header.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
</body>
</html>
Footer:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> This should be on the bottom </p>
</body>
</html>
when i open the project i will only see what is written in index without header or footer
Your code return invalid HTML, because DOCTPYE, html, head and body are defined twice.
Change header.php to:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
#-- not closing body and html
Change footer.php to:
<p> This should be on the bottom </p>
</body>
</html>
Make sure the path to your header and footer is correct.
Then try including it in your page like this
require(“header.php”);
//Your html code here
require(“footer.php”);
I'm trying to parse a mixed HTML/PHP file (viewfile.php) to output it into a html email using the following code:
<?php
//define variables and objects to be used in the view
...
ob_start();
include 'viewfile.php';
$email_content=ob_get_contents();
ob_end_clean();
send_email($email_content);
?>
The viewfile.php to be included looks somehow like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Dear <?= $name ?>,</p>
<p>You've purchased the following items:</p>
<?php foreach ($items as $i){?>
<p><?= $i['name'] ?><br/>
<?php if (isset($i['item_options'])) foreach ($i['item_options'] as $io) {?>
<?= $functions->option_name($io['option']).': '.$functions->option_value($io['value']) ?><br/>
<?php }
}?></p>
<p>Best wishes</p>
</body>
</html>
Unfortunately, my webhost doesn't have output_buffering. So, the functions ob_start(); ob_get_contents(); and ob_end_clean(); don't work.
Is there any other way to read file into a variable and parse all the PHP in there.
I'm using PHP 5.4.
Thank you very much for your help.
I'm developing a web app and I'm having a problem with the templating fase. The problem concerns the generation o html code in a php object. The code is as follows:
<?php class abcHtml {
function prepare() { ?>
<!DOCTYPE html>
<html>
<head>
<?php echo $this->meta; ?>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
<?php echo $this->cont; ?>
</div>
</div>
</body>
</html>
<?php }} ?>
The goal is to have a php file with the cleanest html code possible(Don't want to use any framework).
<?php
class abc extends abcHtml
{
protected $meta;
protected $cont;
public function render(){
return parent::prepare();
}
public function setMeta($meta){
$this->meta = $meta;
return $this;
}
?>
This class inherites abcHtml to make possible to add diferent html modules. The problem is that when calling render(), the returned html code is note in the right order, the html code in the $meta and $cont variables appear before the html that is inside the prepare function.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css/base_style.css"/>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
</div>
</div>
</body>
</html>
This problem obviously means that php is running the php code first and then attaching the html. Is there a solution to fix this?
pCharts documentation says that you should be able to render the image to the browser using this code.
mypic.php
$myPicture->stroke;
mypage.html
<IMG SRC=‘mypic.php‘>
The img tag is supposed to invoke the php script. Within the PHP script, the stroke function sets the content-type: image/png.
So here is what I have:
netsales.php
<?php
include('../class/pData.class.php');
include('../class/pDraw.class.php');
include('../class/pImage.class.php');
/* query sales and create new image */
$myPicture->stroke;
?>
index.php
<?php
include ('netsales.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<img src="netsales.php" />
</div>
</body>
</html>
I'm not getting any errors, just the red X for a missing image.
try removing the
<?php
include ('netsales.php');
?>
From index.php, and add
header('Content-Type: image/png');
Before the Stroke call.
I try something like this and works:
//html file//////////////////////
<?php include ('my.php'); ?>
<html>
<head></head>
<body>
<div> <img src="my.php" /> </div>
</body>
</html>
///////////////////////////////////
//my.php file/////////////////////////
<?php
/* pChart library inclusions */
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pImage.class.php");
//...
$myPicture->autoOutput("picture.png");
?>
///////////////////////////////////
I just got it working for me I had comment include function in HTML file:
<?php //include ('my.php'); ?>