0

我有一个保存表格结构的变量,我想在表格中添加 php 代码。我尝试将代码语句添加到变量中,然后在表中获取变量数据,但我认为这是不可能的,正确的做法是什么。

$table='<table border="1" cellspacing="0" cellpadding="0"><tr>
        <td width="638" valign="top">**ADD CODE HERE**</td></tr>
        </table>';
echo $table;

这是我要在列中添加的代码:

if($_SESSION['Mmsg']['Mreg-err'])
    {
        echo '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';
        unset($_SESSION['Mmsg']['Mreg-err']);
    }       
if($_SESSION['Mmsg']['Mreg-success'])
    {
        echo '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';
        unset($_SESSION['Mmsg']['Mreg-success']);
    }

我尝试在 $notification 中添加代码,然后添加

$notification='if($_SESSION['Mmsg']['Mreg-err'])
    {
    echo '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';
        unset($_SESSION['Mmsg']['Mreg-err']);
    }       
    if($_SESSION['Mmsg']['Mreg-success'])
        {
        echo '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';
        unset($_SESSION['Mmsg']['Mreg-success']);
        }';

' . $通知。'

表内如下:

$table='<table border="1" cellspacing="0" cellpadding="0"><tr>
            <td width="638" valign="top">' . $notification . '</td></tr>
            </table>';
echo $table;

但这是不可能的,有没有办法做到这一点?我是新手

4

5 回答 5

3

编辑添加了您的未设置...

if($_SESSION['Mmsg']['Mreg-err'])
    $notification = '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';
if($_SESSION['Mmsg']['Mreg-success'])
    $notification = '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';

unset($_SESSION['Mmsg']['Mreg-err']);
unset($_SESSION['Mmsg']['Mreg-success']);

$table='<table border="1" cellspacing="0" cellpadding="0"><tr>
        <td width="638" valign="top">' . $notification . '</td></tr>
        </table>';
echo $table;
于 2012-04-12T15:47:00.527 回答
1

你能把你想要的内容放到 $notification 变量中吗?

   var $notification=""; // is 'var' idiomatic PHP? Not used it much lately.
   if($_SESSION['Mmsg']['Mreg-err'])
     {
       $notification= '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';  
       unset($_SESSION['Mmsg']['Mreg-err']);     
     }
  if($_SESSION['Mmsg']['Mreg-success'])
     {
        $notification = $notification . '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';
         unset($_SESSION['Mmsg']['Mreg-success']);
   } 

  $table='<table border="1" cellspacing="0" cellpadding="0"><tr>             <td width="638" valign="top">' . $notification . '</td></tr>             </table>';     
  echo $table; 

然而,有几件事对我来说似乎很奇怪。

首先,为什么要为错误或成功消息设置单独的 Session 变量?为什么没有$_SESSION['Mmsg']['Mreg-outcome']?那你就不需要那样切换了。

你还需要echo桌子吗?为什么不让您的$notification变量可用于页面,然后让表格在页面中执行如下操作:

<table><tr><td>$notification</td></tr></table>

最后,除了显示表格数据之外,您最好不要将该表用于其他任何用途,否则您会使小猫哭泣。它看起来很可疑,好像您可能打算将其用于页面格式化。

于 2012-04-12T15:42:30.960 回答
1
<?php
    // snipped code
    $table='<table border="1" cellspacing="0" cellpadding="0"><tr>
           <td width="638" valign="top">' . $notification . '</td></tr>
           </table>'
    echo $table;
    // snipped code
?>

可以这样重写:

<?php
    // snipped code
?>
<table border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td width="638" valign="top"><?=$notification?></td>
    </tr>
</table>
<?php
    // snipped code
?>

干净多了,不是吗?

这些if语句还具有替代语法,我发现它更容易遵循。

<?php
// snipped code
if($_SESSION['Mmsg']['Mreg-err'])
{
echo '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';
    unset($_SESSION['Mmsg']['Mreg-err']);
}       
if($_SESSION['Mmsg']['Mreg-success'])
    {
    echo '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';
    unset($_SESSION['Mmsg']['Mreg-success']);
    }
// snipped code
?>

这可以重写:

<?php
// snipped code
?>

<?php if($_SESSION['Mmsg']['Mreg-err']): ?>
    <div class="err"><?=$_SESSION['Mmsg']['Mreg-err']?></div>
    <?php unset($_SESSION['Mmsg']['Mreg-err']); ?>
<?php endif; ?>
<?php if($_SESSION['Mmsg']['Mreg-success']): ?>
    <div class="success"><?=$_SESSION['Mmsg']['Mreg-success']?></div>
    <?php unset($_SESSION['Mmsg']['Mreg-success']); ?>
<?php endif; ?>

<?php
// snipped code
?>

现在,如果出现任何错误,调试和跟踪会容易得多。

于 2012-04-12T15:47:13.567 回答
0

您所做的是完全正确的,只是您没有为通知变量正确分配值。现在应该可以了。

 if($_SESSION['Mmsg']['Mreg-err'])
 {
   $notification= '<div class="err">'.$_SESSION['Mmsg']['Mreg-err'].'</div>';  
   unset($_SESSION['Mmsg']['Mreg-err']);     
 }
if($_SESSION['Mmsg']['Mreg-success'])
 {
    $notification = $notification . '<div class="success">'.$_SESSION['Mmsg']['Mreg-success'].'</div>';
     unset($_SESSION['Mmsg']['Mreg-success']);
 } 

$table='<table border="1" cellspacing="0" cellpadding="0"><tr><td width="638" valign="top">' . $notification . '</td></tr></table>';     
echo $table; 
于 2012-04-12T15:58:49.720 回答
0

你的问题不清楚。我试图猜测你的问题是什么,试试这个:

$table='<table border="1" cellspacing="0" cellpadding="0"><tr>
        <td width="638" valign="top">' . htmlentities($notification) . '</td></tr>
        </table>';
echo $table;

看看这是否有效

于 2012-04-12T16:08:12.803 回答