0

dynamic html code in php variable

I am trying to write a generic class/view to display data from Oracle SQL to a HTML table. along with the data user also has the ability to add additional columns with buttons/controls etc. (as HTML + php)

File1:

// database access object
$selObj1 = new LstParPrA($parID, TRUE);

// table properties
$tabInfo = array();
$tabInfo[TAB_BORDER] = 1;
$tabInfo[TAB_WIDTH] = 50;

// test script 
$scriptInfo = array();
$scriptInfo[0] = 'Test text: #1';
$scriptInfo[1] = '/#1/'; /* patterns */
$scriptInfo[2] = '{$testStr}'; /* replacements */

// column prperties
$colInfo = array();
$colInfo[] = array(COL_NAME => 'PR_BU', COL_WIDTH => 33, COL_ALIGN => "center",
   COL_DATA => 'PR_BU', COL_TYPE => COL_TYPE_DBDATA);
$colInfo[] = array(COL_NAME => 'PR_ID', COL_WIDTH => 33, COL_ALIGN => "center",
   COL_DATA => 'PR_ID', COL_TYPE => COL_TYPE_DBDATA);
$colInfo[] = array(COL_NAME => 'APPR_DATE', COL_WIDTH => 33, COL_ALIGN => "center",
   COL_DATA => 'APPR_DATE', COL_TYPE => COL_TYPE_DBDATA);
$colInfo[] = array(COL_NAME => 'Option', COL_WIDTH => 33, COL_ALIGN => "center",
   COL_DATA => $scriptInfo, COL_TYPE => COL_TYPE_SCRIPT);

$listObj = new vwGenLstA();
$listObj->viewList($selObj1, $tabInfo, $colInfo);
// further processing

File2:

class vwGenLstA implements appListView {

    public function viewList(& $dataObj, & $tabInfo, & $colInfo) {

        $dataRow = NULL;
        $testStr = "this is a test string";

        // fetch data into array
        if (($dataRow = $dataObj->fetchData()) === FALSE) {
            // log error
        }

        // other processing
        // other processing
        // other processing

        <?php if ($dataRow != NULL): ?>
            <p>
                <table border="<?php echo($tabInfo[TAB_BORDER]); ?>" width="<?php echo($tabInfo[TAB_WIDTH]); ?>%" >
                <?php foreach ($colInfo as $value) { ?>
                    <col width="<?php echo($value[COL_WIDTH]); ?>%">
                <?php } unset($value); ?>

                <tr>
                    <?php foreach ($colInfo as $value) { ?>
                        <th align="center"><?php echo($value[COL_NAME]); ?></th>
                    <?php } unset($value); ?>
                </tr>

                <?php do { ?>
                    <tr>
                    <?php foreach ($colInfo as $value) { ?>
                        <?php
                        /* reinit variable */
                        $varData = NULL;

                        /* take appropriate action based on the flag */
                        switch ($value[COL_TYPE])
                        {
                            case COL_TYPE_DBDATA:
                              $varData = $dataRow[$value[COL_DATA]];
                              break;

                            case COL_TYPE_FUNC:
                              $varData = $value[COL_FNAME]($value[COL_DATA]);
                              break;

                            case COL_TYPE_SCRIPT:

                                if (is_string($value[COL_DATA])) {
                                    $varData = $value[COL_DATA];
                                } elseif (is_array($value[COL_DATA])) {
                                    $temp = $value[COL_DATA];
                                    $pattern = $temp[1];
                                    $replacement = $temp[2];
                                    $string = $temp[0];
                                    $varData = preg_replace($pattern, $replacement,$string);
                                    echo(">>> string: $string | pattern: $pattern | replace: $replacement | varData: $varData | testStr: $testStr <<<" . PHP_EOL);
                                }
                                break;

                            default:
                              assert(FALSE);
                              break;
                        }
                        ?>
                    <td align="<?php echo($value[COL_ALIGN]); ?>"><?php echo($varData); ?></td>
                    <?php } unset($value); ?>
                    </tr>
                <?php } while ($dataRow = $dataObj->fetchData()); ?>
                </table>
            </p>
        <?php endif; ?>

    }

I have got stuck with case: COL_TYPE_SCRIPT. When printing with echo, $testStr does not display "this is a test string" ($testStr variable is being displayed as blank) in the output unlike other columns (COL_TYPE_DBDATA, etc) that are getting displayed properly.

I have also tried sending data as text from file1, no success: $scriptInfo = 'Test text: $testStr';

Request your guidance on this issue please...

jin
  • 3
  • 4
  • Quite a complex & broad issue + I don't understand what you mean by "expand in the output" (I guess "display"). I would suggest to narrow down the problem by storing debug infos in a log file (or at worst `echo`ing them). For example in "file2" check the value of `$value` before the `switch/case`... – Maxime Pacary Sep 12 '16 at 13:54
  • my apologies for not being clear. Yes, the value of $testStr is not being echo'ed. The values are being passed properly and the results of the database are getting displayed, the last field is not. I am getting "Test text: {$testStr}", i am expecting "Test text:this is a test string" – jin Sep 12 '16 at 14:03
  • updated the debug statement, following is the output: >>> string: Test text: #1 | pattern: /#1/ | replace: {$testStr} | varData: Test text: {$testStr} | testStr: this is a test string << – jin Sep 12 '16 at 14:27
  • 1
    Hopefully this Q&A could help you http://stackoverflow.com/questions/15065387/how-replace-variable-in-string-with-value-in-php if you still struggle I'll try to have a more detailed look tomorrow – Maxime Pacary Sep 12 '16 at 20:34
  • Thank you for the guidance/tip, i was able to fix the issue using "str_replace". Thank you once again – jin Sep 13 '16 at 09:11

1 Answers1

0

What i found is we can use HTML template engine or third party template library such as PHP XTemplate, smarty, pear etc. to embed data in your output. Or as built-in solution as we are using

<?php echo $var; ?> OR short tags like <?= $var ?> 

(short tags should be enable to use).

Thanks

Patrick R
  • 6,621
  • 1
  • 24
  • 27