2012-04-12 136 views
0

我有一个变量持有一个表的结构,我想在表内添加php代码。我试着将代码语句添加到变量中,然后获取表格中的变量数据,但我认为不可行,那么做什么是正确的方式。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='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 . '

内表如:

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

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

+1

为什么是不可能的?任何错误?看起来像缺少分号 – 2012-04-12 15:40:54

+0

解析错误:语法错误,意外T_STRING – 2012-04-12 15:44:17

+2

不要执行多行分配/回声。改为使用[HEREDOC](http://php.net/heredoc)。你也在'$ table = ...'行末尾缺少';'。 – 2012-04-12 15:44:27

回答

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; 
+0

谢谢,这工作 – 2012-04-12 16:35:25

1

你可以把你想要的内容查找到$通知变量?

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['Mmsg']['Mreg-outcome']?那么你不需要像那样切换。

您还需要echo桌子吗?为什么不只是有可用的$notification变量的页面,然后有表做了这样的事在页面:

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

最后,你最好不要使用该表比表格中显示其他任何东西数据或者你会让小猫哭泣。它看起来很可疑,好像您可能计划将其用于页面格式化。

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 
?> 

,如果发生任何错误,这将是更易于调试和跟踪。

+0

是否有某种在线语法格式化为PHP?使代码更具可读性? – 2012-04-12 15:48:48

+1

有PHPLint:http://www.icosaedro.it/phplint/ 这是一个代码验证程序。我不确定它格式化代码。任何文本编辑器都可以做到。我认为TextMate,Sublime,Eclipse等都可以做格式化。 – 2012-04-12 15:56:00

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; 
0

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

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

看看,如果这个工程

相关问题