Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > Assign variable to a block of html code
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 10th April 13:25
php
External User
 
Posts: 1
Default Assign variable to a block of html code



Hi All,

Is it possible to assign variable to a block of html code ?

Something like this :

$myblokvar = "
<table width="487" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="487" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/bartitle_login.gif" alt="Login" width="475"
height="30" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="produk"><table width="100%" border="0" cellpadding="3"
cellspacing="2">
<tr>
<td class="katalog">
<?=$log_info?>
</td>
</tr>
</table></td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="produk">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
";

Although example above is not working, what I want to achieve is something
like that. Is it possible how can I do that ?

Regards,

Feris
  Reply With Quote


  sponsored links


2 10th April 13:25
maillists
External User
 
Posts: 1
Default Assign variable to a block of html code



What you have will work, you just need to escape out the double quotes in
the html.


--
Stephen Johnson
The Lone Coder

http://www.ouradoptionblog.com
*Join us on our adoption journey*

stephen@thelonecoder.com
http://www.thelonecoder.com

*Continuing the struggle against bad code*
--
  Reply With Quote
3 10th April 13:25
xavier
External User
 
Posts: 1
Default Assign variable to a block of html code


You should try the HEREDOC structure.
See link: http://php.net/heredoc

It should look to something like:

$myblokvar = <<<EOF
<table blabla>
<tr>
<td>
Welcome $name to this website!
</td>
</tr>
</table>
<<<EOF;

No need of quotes or php start/end tags when placing a variable.

To use it afterwards simply call the $mylokvar variable.

Hope it helped!

Xavier
Web Developer
Site: http://www.eds.mu


-----Original Message-----
From: Stephen Johnson [mailto:maillists@thelonecoder.com]
Sent: jeudi 20 décembre 2007 07:43
To: php mail; PHP General List
Subject: Re: [php] Assign variable to a block of html code

What you have will work, you just need to escape out the double quotes in
the html.


--
Stephen Johnson
The Lone Coder

http://www.ouradoptionblog.com
*Join us on our adoption journey*

stephen@thelonecoder.com
http://www.thelonecoder.com

*Continuing the struggle against bad code*
--

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Reply With Quote
4 10th April 13:25
darren
External User
 
Posts: 1
Default Assign variable to a block of html code


You can use Heredoc quoting for this. (http://uk2.php.net/types.string)

---------
<?php
$log_info = "Your logged"; $myblokvar = <<<html
<table width="487" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="487" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/bartitle_login.gif" alt="Login" width="475"
height="30" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="produk"><table width="100%" border="0" cellpadding="3"
cellspacing="2">
<tr>
<td class="katalog"> $log_info
</td>
</tr>
</table></td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="produk">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table> html; echo $myblokvar;
?>
---------

Darren
  Reply With Quote


  sponsored links


5 10th April 13:25
darren
External User
 
Posts: 1
Default Assign variable to a block of html code


Xavier,

You should test this before you send it.. it doesn't even parse!
The closing EOF should not start with the "<<<". It should only be the
identifier ("EOF") followed by ; and a new line.
----- $myblokvar = <<<EOF
<table blabla>
.....
....
EOF; ----- Darren
  Reply With Quote
6 10th April 13:26
pete
External User
 
Posts: 1
Default Assign variable to a block of html code


You could just swap all the double quotes in the HTML tags for single quotes -
that would work in this instance...

$myblokvar = "
<table width='487' border='0' cellspacing='0' cellpadding='0'>
....
</table>
";


Or perhaps a HereDoc syntax:

$myblokvar = <<<EndOfMyHTMLBlock
<table width="487" border="0" cellspacing="0" cellpadding="0">
....
</table>
EndOfMyHTMLBlock;

Then you don't need to worry about what quotes you use, and if you start putting
stuff like onclick events in you can mix quotes up happily ...


I go to great lengths to avoid escaping quotes - it just looks ugly.
That is, however, a personal foible.

Cheers
Pete
  Reply With Quote
7 10th April 13:26
parasane
External User
 
Posts: 1
Default Assign variable to a block of html code


If you absolutely want to do it that way, then escape your quotes
in the HTML. When you're using quotes to encapsulate a variable, any
quotes contained within the value will cause a parse error. One
alternative is to use single-quotes for variable containment, and
double-quotes throughout, or vice-versa. These two methods will work:

$variable = "This is how you \"escape\" quotes.";
$variable = 'Using single quotes gives you the "literal" value,
which means you can't do newlines and such.\n';

However, your best bet is going to be using HEREDOC:

$html =<<<EOL

Always be sure to use the three left carats as shown in the line
above. You can call EOL anything you want in the world, as long as it
doesn't interfere with an existing part of your code within the same
scope. You can also use $variables within a HEREDOC, but things like
newlines WILL NOT work.\n

Also note that, when using HEREDOC syntax, you don't end the
<<<EOL with a semicolon. And when you're done with your HEREDOC
block, be sure to end it as the first thing on the line. You can't
have any spaces, tabs, or other characters before it. And be sure to
place your semicolon after it, as well, like so:
EOL;

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
  Reply With Quote
8 10th April 16:44
xavier
External User
 
Posts: 1
Default Assign variable to a block of html code


Quote:
"
You could just swap all the double quotes in the HTML tags for single quotes -
that would work in this instance...

$myblokvar = "
<table width='487' border='0' cellspacing='0' cellpadding='0'>
....
</table>
";
"

That's a way too, but it does not preserve the indentation.
The HEREDOC syntax behave much like the <pre> HTML tag for PHP, and it also removed the hassle of placing escape tags!

Xavier de Lapeyre
Web Developer
Enterprise Data Services
24, Dr Roux Street,
Rose Hill
Office: (230) 465 17 00
Fax: (230) 465 29 00
Site: http://www.eds.mu

-----Original Message-----
From: Peter Ford [mailtoete@justcroft.com]
Sent: jeudi 20 décembre 2007 13:17
To: php-general@lists.php.net
Subject: Re: [php] Assign variable to a block of html code

You could just swap all the double quotes in the HTML tags for single quotes -
that would work in this instance...

$myblokvar = "
<table width='487' border='0' cellspacing='0' cellpadding='0'>
....
</table>
";


Or perhaps a HereDoc syntax:

$myblokvar = <<<EndOfMyHTMLBlock
<table width="487" border="0" cellspacing="0" cellpadding="0">
....
</table>
EndOfMyHTMLBlock;

Then you don't need to worry about what quotes you use, and if you start putting
stuff like onclick events in you can mix quotes up happily ...


I go to great lengths to avoid escaping quotes - it just looks ugly.
That is, however, a personal foible.

Cheers
Pete


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Reply With Quote
9 10th April 16:44
xavier
External User
 
Posts: 1
Default Assign variable to a block of html code


Yeps,
Sorry bout that.

Thnks for pointing it out.
Xavier

-----Original Message-----
From: Darren Whitlen [mailto:darren@sirdaz.com]
Sent: jeudi 20 décembre 2007 13:13
To: php-general@lists.php.net
Subject: Re: [php] Assign variable to a block of html code

Xavier,

You should test this before you send it.. it doesn't even parse!
The closing EOF should not start with the "<<<". It should only be the
identifier ("EOF") followed by ; and a new line.
----- $myblokvar = <<<EOF
<table blabla>
.....
....
EOF; ----- Darren

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Reply With Quote
10 10th April 20:06
php
External User
 
Posts: 1
Default Assign variable to a block of html code


Hi Everyone,

Thanks for pointing me to heredoc syntax. I've got it working out nicely now


Regards,

Feris
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666