I have this code, which uses ob_start php function. Which basically puts the echoed data into an html file. It works before. I do not know what version of php I was using then. But my current version is 5.3.0. I cannot explain why it wouldn't work. Because the script below is working and it just puts the output of that script into the html file:
<?php
ob_start();
?>
<h2>Customer Payment Summary</h2>
<img id="tablez" src="../img/system/icons/Oficina-PDF-icon.png"></img>
<?php
if($amtopay>=$curcred){
$custchange=$amtopay - $curcred;
$newcred = 0;
echo "Change: ". $custchange."<br/>";
query_database("DELETE FROM sales_transaction WHERE Cust_Name='$customer'", "onstor", $link);
}else{
query_database("UPDATE customer_credit SET CREDIT='$newcred' WHERE Cust_Name='$customer'", "onstor", $link);
echo "Remaining Balance: ". $newcred."<br/>";;
}
echo "Customer: ".$customer."<br/>";
echo "Amount paid: ". $amtopay. "<br/>";
echo "Date: ". $date." ". date('A');
close_connection($link);
?>
<?php
file_put_contents('../tmp/customerpay.html', ob_get_contents());
?>
Here's the output of the code above:
But when I checked the html file which I specified in the file_put_contents. It gives me this. And I don't really understand why:
My problem is how to get the correct output from the html file that is being produced.
You aren't closing your output buffer before you do a file_put_contents...
At the end of your script change it to the following:
//...
close_connection($link);
$contents = ob_get_contents();
ob_end_clean();
file_put_contents('../tmp/customerpay.html', $contents);
?>
Related
Does PHP give access to all the strings that have been outputted to a page?
<html>
<body>
Link
<?php
echo 'hello world';
echo 'something else';
$s = get_all_output(); // does this exist ?
Of course I could replace every instance of echo 'some text'; by an initial $s = ''; and then $s .= 'some text';.
But without this trick, how to get the current page as a string?
You could make use of the output buffering of PHP (cf. https://www.php.net/manual/en/function.ob-get-contents.php).
<?php
ob_start(); // turn on buffering
echo "Hello ";
$out = ob_get_contents();
ob_end_clean(); // end buffering and clear buffer without "displaying it".
// process $out which contains "Hello "
echo "$out";
You must insert:
ob_start();
at the beginig of your php code and then
$s = ob_get_contents();
at the point where you want to get the content of the page.
how to read a php file and echo it in a html file?
i try to use readfile() , file() file_get_content() to read a php file.
but when i echo file_string its parsed and then show.
how i can prevent to pars stirng var that included php codes.
here my code:
<?php
$path = '..../ex.php';
$source = fopen($path , "r");
echo fread($source,filesize($path ));
fclose($source);
?>
how to echo $source without compiled or parsed.
With this function
<?php
echo htmlspecialchars($text);
?>
php.net/htmlspecialchars
fread should works fine. Remember that when you use echo it prints <?php opening tag and in rendered page it can be not visible.
To test it, just try with var_dump:
$content = fread($source,filesize($path));
var_dump($content);
I'll go out on a limb and guess that the code is not showing up completely in your HTML page, because the browser is trying to interpret <?php as HTML tags. The solution is to HTML encode any text which may contain characters with a special meaning in HTML:
echo htmlspecialchars(file_get_contents('..../ex.php'));
See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).
use this
$path = 'ex.php';
$source = fopen($path , "r");
echo "<textarea style='border:0px; overflow: hidden; width:100%; height:100% '>";
echo fread($source,filesize($path ));
echo "</textarea>";
fclose($source);
How do you append something to the beginning of the output buffer?
For example, say you have the following code:
ob_start();
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
Before flushing the contents to the browser, how can I append something so that it appears before <p>Start of page.</p> when the page loads?
It sounds simple enough, like moving the pointer to the beginning of an array, but I couldn't find how to do it with the output buffer.
** PHP 5.3 **
ob_start(function($output) {
$output = '<p>Prepended</p>'.$output;
return $output;
});
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
** PHP < 5.3 **
function prependOutput($output) {
$output = '<p>Appended</p>'.$output;
return $output;
}
ob_start('prependOutput');
echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
Use 2 ob_start commands and ob_end_flush() after what you want to first display then end the buffer with ob_end_flush again when you want to output the rest of the page.
eg:
ob_start();
ob_start();
echo '<p>Start of page.</p>';
ob_end_flush();
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';
ob_end_flush();
You can get content of buffer using ob_get_contents() function
ob_start();
echo "World! ";
$out1 = ob_get_contents();
echo "Hello, ".$out1;
Are you wanting it before any output at all? If so, then you're looking for the auto_prepend_file directive. http://php.net/manual/en/ini.core.php
See the first parameter of ob_start (documentation here), it allows you to provide a callback to be called when the buffer is flushed or cleaned. It receives a string as a parameter and outputs a string, therefore it should be easy to
function writeCallback($buffer)
{
return "Added before " . $buffer;
}
ob_start("writeCallback");
I want caching some php files partially. for example
<?
echo "<h1>",$anyPerdefinedVarible,"</h1>";
echo "time at linux is: ";
// satrt not been catched section
echo date();
//end of partial cach
echo "<div>goodbye $footerVar</div>";
?>
So cached page should be like as
(cached.php)
<h1>This section is fixed today</h1>
<? echo date(); ?>
<div>goodbye please visit todays suggested website</div>
It may be done with templating but I want it directly. Because I want alternative solution.
Look at php's ob_start(), it can buffer all output and save this.
http://php.net/manual/en/function.ob-start.php
Addition:
Look at http://www.php.net/manual/en/function.ob-start.php#106275 for the function you want :)
Edit:
Here a even simpeler version: http://www.php.net/manual/en/function.ob-start.php#88212 :)
Here some simple, but effective, solution:
template.php
<?php
echo '<p>Now is: <?php echo date("l, j F Y, H:i:s"); ?> and the weather is <strong><?php echo $weather; ?></strong></p>';
echo "<p>Template is: " . date("l, j F Y, H:i:s") . "</p>";
sleep(2); // wait for 2 seconds, as you can tell the difference then :-)
?>
actualpage.php
<?php
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
// Variables
$weather = "fine";
// Evaluate the template (do NOT use user input in the template, look at php manual why)
eval("?>" . get_include_contents("template.php"));
?>
You could save the contents of template.php or actualpage.php with http://php.net/manual/en/function.file-put-contents.php to some file, like cached.php. Then you can let the actualpage.php check the date of cached.php and if too old, let it make a new one or if young enough simply echo actualpage.php or re-evaluate template.php without rebuilding the template.
After comments, here to cache the template:
<?php
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
file_put_contents("cachedir/cache.php", get_include_contents("template.php"));
?>
To run this you can run the cached file directly, or you can include this on an other page. Like:
<?php
// Variables
$weather = "fine";
include("cachedir/cache.php");
?>
This question already has answers here:
HTML into PHP Variable (HTML outside PHP code)
(7 answers)
Closed 4 years ago.
Hi i'd like to store a dinamically generated(with php) html code into a variable and be able to send it as a reply to an ajax request.
Let's say i randomly generate a table like:
<?php
$c=count($services);
?>
<table>
<?php
for($i=0; $i<$c; $i++){
echo "<tr>";
echo "<td>".$services_global[$i][service] ."</td>";
echo "<td>".$services_global[$i][amount]."</td>";
echo "<td>€ ".$services_global[$i][unit_price].",00</td>";
echo "<td>€ ".$services_global[$i][service_price].",00</td>";
echo "<td>".$services_global[$i][service_vat].",00%</td>";
echo "</tr>";
}
?>
</table>
I need to store all the generated html code(and the rest) and echo it as a json encoded variable like:
$error='none';
$result = array('teh_html' => $html, 'error' => $error);
$result_json = json_encode($result);
echo $result_json;
I could maybe generate an html file and then read it with:
ob_start();
//all my php generation code and stuff
file_put_contents('./tmp/invoice.html', ob_get_contents());
$html = file_get_contents('./tmp/invoice.html');
But it sounds just wrong and since i don't really need to generate the code but only send it to my main page as a reply to an ajax request it would be a waste of resources.
Any suggestions?
You don't have to store it in a file, you can just use the proper output buffering function
// turn output buffering on
ob_start();
// normal output
echo "<h1>hello world!</h1>";
// store buffer to variable and turn output buffering offer
$html = ob_get_clean();
// recall the buffered content
echo $html; //=> <h1>hello world!</h1>
More about ob_get_clean()
if the data is so much expensive to regenerate then I would suggest you to use memcached.
Otherwise I would go regenerate it every-time or cache it on the frontend.
for($i=0;$i<=5;$i++)
{
ob_start();
$store_var = $store_var.getdata($i); // put here your recursive function name
ob_get_clean();
}
function getdata($i)
{
?>
<h1>
<?php
echo $i;
?>
</h1>
<?php
ob_get_contents();
}