List Info

Thread: Re: Setting HTML as variable




Re: Setting HTML as variable
country flaguser name
United States
2008-03-26 21:24:25

--- In php-list%40yahoogroups.com">php-listyahoogroups.com, James Keeline <keeline...> wrote:
&gt;
> --- whoisquilty <whoisquilty...> wrote:
&gt;
> > I want to set a variable that will include HTML code and MySQL query results.
> > Is this possible?
>
> Yes. Instead of using print or echo simply build up a big variable. This can
> contain any variables or dynamic content you desire.
>
> $out = "&quot;;
> $out .= "first line<br />n&quot;;
> $out .= "second line<br />n&quot;;
>
> To send the output to the browser simply:
>
> print $out;
&gt;
> You can also play games with output buffering -- ob_*() functions -- but these
&gt; require that your opening PHP tag be on the first line, first character
> position. I don't think it will work if you start to send output to the
> browser. It may not be as picky as header(), set_cookie(), etc. which write to
> the HTTP headers.
>
> James
&gt;

Thanks, James. What if I want to pass $out to another page? I tried that and since my $out
has code in it, the page is reading it as code and not as a variable in a link.

Jeremy

__._,_.___
.

__,_._,___
Re: Re: Setting HTML as variable
country flaguser name
United States
2008-03-26 23:27:29

--- whoisquilty < whoisquilty%40gmail.com">whoisquiltygmail.com> wrote:

> Thanks, James. What if I want to pass $out to another page? I tried that and
> since my $out has code in it, the page is reading it as code and not as a
> variable in a link.
&gt;
> Jeremy

Most of the time PHP is used to generate custom HTML. Some of that custom
content is determined by the logic of the program and some may come from a
database query or other resource.

If you are generating a block of content (text + HTML) you can best pass it
from one page to another using session variables.

The basic procedure in PHP is to start your opening PHP tag (<?php) start on
the first line on the first character position. Then do a session_start(),
build up your $out variable, and save it in a session variable.

_____
<?php
session_start();
$out = "&quot;;
$out .= "first line<br />n&quot;;
$out .= "second line<br />n&quot;;
# ...
$_SESSION['html'] = $out;
?&gt;

The underscores are not part of the file of course. It's important that you do
not do any print or echo statements before you save the value in the $_SESSION
superglobal.

In your second page you do something similar:
_____
&lt;?php
session_start();
$html = $_SESSION['html'];

print $html;
?>

James

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )