1

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>&euro; ".$services_global[$i][unit_price].",00</td>";
 echo "<td>&euro; ".$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?

g0dl3ss
  • 393
  • 1
  • 6
  • 19
  • My suggestion: "please phrase a more specific question" ... –  Jan 02 '12 at 20:20
  • I would also suggest not to generate html straight in code. Better to use some kind of template engine (just php includes will work too). My favorite one is Twig (http://twig.sensiolabs.org/) – petraszd Jan 02 '12 at 20:29

3 Answers3

9

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()

maček
  • 76,434
  • 37
  • 167
  • 198
  • The thing is that if i echo the html then it will be taken as a reply to the ajax request won't it? I'd need to generate the html without echoing it or something like that. – g0dl3ss Jan 02 '12 at 20:43
  • You can store the html in the variable and use it whenever you'd like. You're not forced to echo it until you need it... – maček Jan 02 '12 at 21:10
  • In order to store the html into the internal buffer with ob_start(); i need to generate and echo it. – g0dl3ss Jan 02 '12 at 21:22
  • 1
    Once you've called `ob_start()`, echo will send text into the output buffer rather than onto the page. – benesch Jan 03 '12 at 00:25
  • 1
    @g0dl3ss, yea I'm not sure where you're struggling to understand how `ob_start()` works. After `ob_start()` is called, `echo` will not actually output anything. Instead, the output is captured to the output buffer. From there, you can decide how/when you want to use/display it. – maček Jan 03 '12 at 06:35
  • It looks like the html code is printed anyway even if i `ob_start()`. I'm going to do some tests, thanks for the support. – g0dl3ss Jan 03 '12 at 07:07
  • It's working now, it didn't work cause i had some html written closing the php tags and not echoed through php. Now the html code doesn't show and it gets stored into `$html` using `ob_get_clean();`. Exactly what i was looking for, thanks. – g0dl3ss Jan 03 '12 at 07:30
0
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();
}
0

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.

Community
  • 1
  • 1
GO.exe
  • 646
  • 7
  • 13