Robb Kerr wrote:

On Sat, 24 Jul 2004 23:23:49 +0200, Tularis wrote:


Robb Kerr wrote:


I have a complicated table that needs to be altered depending upon the
contents of a field in one of my recordsets. I've tried several approaches
and need to know what you recommend.

I've created three totally different tables and put the HTML code for each
into a database. Then I used PHP to insert the relevant code into the page
depending upon the contents of a field in a different database. Result -
the appropriate code is inserted properly into the HTML. Problem - the HTML
table I'm trying to insert contains PHP code which is not executed after
being inserted.

Because sometimes the HTML table needs 4 rows and other times only 2, I
tried enclosing the appropriate <tr>s in a PHP IF statement (see below).
Problem - PHP IF wasn't executed, both <tr>s embedded appeared on page
anyway. And, sometimes, the relevant <tr>s will include PHP code so this
embedding technique won't work.

<?php
 if ($row_RS_PageContent['PageType'] != "2") {
?>
 <tr>
   <td width="200" height="0" bgcolor="99CCCC">&nbsp;</td>
   <td width="20" height="0">&nbsp;</td>
   <td height="0" align="left" valign="top">&nbsp;</td>
   <td height="0" align="right" valign="top">&nbsp;</td>
   <td width="20" height="0">&nbsp;</td>
   <td width="5" height="0" bgcolor="333366">&nbsp;</td>
 </tr>
 <tr>
   <td width="200" height="0" bgcolor="99CCCC">&nbsp;</td>
   <td width="20" height="0">&nbsp;</td>
   <td height="0" align="left" valign="top">&nbsp;</td>
   <td height="0" align="right" valign="top">&nbsp;</td>
   <td width="20" height="0">&nbsp;</td>
   <td width="5" height="0" bgcolor="333366">&nbsp;</td>
 </tr>
<?php
}
?>

Should I simply describe the entire relevant <tr>s on one line,
appropriately escape them, assign the to a variable and then use an ECHO to
put them on the page? Or does someone have a simpler more elegant solution?

Thanx

just use eval() on that string, it's not elegant, nor is it really secure, but it'll work fine.


This sounds like exactly what I need. But, can you help me with the synatx?
I've read the entry in the PHP documentation and I don't completely
understand. Let's assume all of the code to define the HTML table
(including appropriate PHP) is stored in a database table called
FooterTable. The field containing the code is called FooterField.

Do I first need to assign the field to a variable via the eval()...

$vFooterText = eval(FooterTable['FooterField']);
echo $vFooterText;

or does the eval statement automatically include the ECHO...

eval(FooterTable['FooterField']);

or do I have to read the contents into a variable first, then eval(), then
echo...

$vFooterText = FooterTable['FooterField'];
eval($vFooterText);
echo $vFooterText;
once you get the code you posted a few lines up out of the database it usually already resides in a variable. eg. $footertable['FooterField']. What eval() does is take a string and just "imagine" it is actually an included piece of PHP. If this helps you, you could imagine the eval string as follows:
---------
// write the string contents to a new php file
$stream = fopen('tmpfile.php', 'w+');
fwrite($stream, $string, strlen($string));
fclose($stream)
include 'tmpfile.php'; // include it, seems obvious...
unlink('tmpfile.php'); // remove the file again
--------


This means that if you pass a string like eg:
<?php
if($a == $b) {
?>A is B!<br /><?php
} else {
?>A is not B!<br /><?php
}
?>

it would parse that as-if it were a piece of php-code, which it is in this case. Remember though that everything you tell php to do in that string is done at the time the eval() is called. That means that if you use that string, eg. called $string (how obvious :P) in such an expression:

<?php
$string = '...'; // that php code but then as a string, or gotten from the database
$a = 1;
$b = 2;
echo 'starting...<br />';
eval($string);
echo 'stopping';
?>


it would output:
starting...
A is not B!
stopping

Hope that helps :)
- Tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to