2012-01-24 149 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