2012-01-24 148 views
-1

我怎么能包括这些代码,因为它是,然后将其上传到我的数据库,并在数据库中应该有变量,而不是字符串?包含PHP代码的PHP变量中

CODE:

$username1 = $_SESSION['myusername']; 
$sql = mysql_query("SELECT * FROM `members` WHERE `username`='$username1'"); 
while($row=mysql_fetch_array($sql)) { 
    $replytext = '<table cellpadding="0" cellspacing="0" border="0"><tr><td rowspan="10" class="teamimage" style="background-image: url(images/usericons/'.$row['img'].');"><div style="background-image:url(images/teamicons_shadow.png); width: 75px; height: 75px;"></div></td></tr><tr valign="top"><td rowspan="10" style="width:10px;"></td><td style="width:300px;"><table cellpadding="0" cellspacing="0" border="0" style="margin:0px; padding:0px;"><tr><td class="teamname"><b><a href="teams.php?user='.$row['id'].'">'.$row['username'].'</a></b></td></tr><tr><td>Summoner Name: '.$row['summonername'].'</td></tr><tr><td style="border-bottom: 1px solid #888; padding-bottom: 5px;">Clan: '.$row['clan'].'</td></tr><tr><td style="color: #888; padding-top: 3px;">Country: '.$row['country'].'</td></tr><tr><td style="color: #888; padding-top: 3px;">Joined In: '.$row['created'].'</td></tr></table></td><td style="width:10px;"></td><td rowspan="10" style="border: 1px solid #aaa; padding: 3px; width:600px;">'.$replytext.'</td></tr></table>'; 
} 

代码数据库

<table cellpadding="0" cellspacing="0" border="0"> 
    <tr> 
    <td rowspan="10" class="teamimage" 
     style="background-image: url(images/teamicons/default_user.png);"> 
     <div 
     style="background-image:url(images/teamicons_shadow.png); 
      width: 75px; height: 75px;"> 
     </div> 
    </td> 
    </tr> 
    <tr valign="top"> 
    <td rowspan="10" style="width:10px;"> 
    </td> 
    <td style="width:300px;"> 

请注意,我以前解释的代码是$replytext里面的一个!

+3

我不知道你问... – FtDRbwLXw6

+0

你是什么意思“的数据库,而不是字符串” – bowlerae

+0

我要的是能够从我的数据库中的变量,而不是后来呼应了这一点字符串而不是我的变量。 – ZyteX

回答

1

如果我理解你的权利,你想,在代码数据库将是:

<table cellpadding="0" cellspacing="0" border="0"><tr><td rowspan="10" class="teamimage" style="background-image: url(images/teamicons/$row['img'].png) 

另一种解决方案是有一个独特的字符串,不会让PHP认为它是一个变量。 例如改变:

$row['img'] 

###row['img'] 

,当你从数据库中提取该代码使用一个简单的str_replace函数来代替### 符号与$。

当然,你需要有一个独特的字符,如果你使用的是简单的字符,如“一”,它会在你的代码改变所有的“A”字符,以$ ...不是一个很好的主意。

+0

我试着但它只是这样回显出来: '。$ row ['username']'。召唤师姓名:'。$ row ['summonername']'。 氏族:'。$ row ['clan']'。 国家:'。$ row ['country']'。 加入:'。$ row ['created']'。 '。$ replytext'。'; }?> – ZyteX

+0

我刚刚标记你的答案作为其中一个为我所做的工作了,但我可以说,它没有......不过感谢您的回答这一切结束了与工作小时后的解决方案。谢谢! – ZyteX

0

您可以逃脱单引号,像这样:

$replytext = '<table cellpadding="0" cellspacing="0" border="0"><tr><td rowspan="10" class="teamimage" style="background-image: url(images/usericons/$row[\'img\']);"><div ... 

使用\转义为所有的数组键:

url(images/usericons/$row[\'img\']); 
0

这是有帮助吗?

$string = 'cup'; 
$name = 'coffee'; 
$str = 'This is a $string with my $name in it.'; 
eval("\$str = \"$str\";"); 
echo $str. "\n"; 

不确定tho。参考http://php.net/manual/en/function.eval.php