簡體   English   中英

混合使用PHP和html。

[英]Mixing PHP and html.

我有以下PHP代碼:

$temp_str .= '

            <table width="300" height="84" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <td width="80" rowspan="4"><img src="img/planets/1sm.jpg" width="80" height="82" /></td>
        <td width="110">System '.$user[location].'</td>
        <td width="110">'.$planets[planet_name].'</td>
      </tr>
      <tr>
        <td colspan="2">This planet is guarded by '.$planets[fighters].' Fighters</td>
      </tr>
      <tr>
        <td colspan="2">Clan ID</td>
      </tr>
      <tr>
        <td>Attack</td>
        <td>Special Weapon</td>
      </tr>
    </table>

            ';

足夠簡單,顯示精美。

我正在嘗試在“ AttackSpecial weapon框中插入以下內容;

攻擊;

if(($planets['login_id'] == $user['login_id']) ||
               ($planets['clan_id'] == $user['clan_id'] &&
               $planets['clan_id'] != 0) || ($user['login_id'] == ADMIN_ID) ||
               ($planets['fighters'] == 0) || $user['ship_id'] == NULL) {
                $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]><img src=\"img/icons/Land.png\" alt=\"Land\" /></a>";
            } else {
                if($flag_planet_attack != 0){
                    $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]&attack_planet=1><img src=\"img/icons/Attack.png\" alt=\"Attack\" /></a>";

特種武器;

if(ereg("sv",$user_ship['config'])) { //quark disrupter
                        $temp_str .= " - <a href=attack.php?quark=1&planet_num=$planets[planet_id]>Fire Quark Displacer</a>";
                    } elseif(ereg("sw",$user_ship['config']) && $enable_superweapons == 1) { //terra maelstrom
                        $temp_str .= " - <a href=attack.php?terra=1&planet_num=$planets[planet_id]>Fire Terra Maelstrom</a>";
                    }
                    if($planets['pass'] != '0') {
                        $temp_str .= " - <a href=planet.php?planet_id=$planets[planet_id]>Have Pass</a>";

但是我一直在收到意外的t_string錯誤,>一切。

有人可以告訴我我在做什么錯嗎? 非常感謝您的幫助。

下面是我想分割的完整代碼,我想:

if(($planets['login_id'] == $user['login_id']) ||
               ($planets['clan_id'] == $user['clan_id'] &&
               $planets['clan_id'] != 0) || ($user['login_id'] == ADMIN_ID) ||
               ($planets['fighters'] == 0) || $user['ship_id'] == NULL) {
                $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]><img src=\"img/icons/Land.png\" alt=\"Land\" /></a>";
            } else {
                if($flag_planet_attack != 0){
                    $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]&attack_planet=1><img src=\"img/icons/Attack.png\" alt=\"Attack\" /></a>";
                    if(ereg("sv",$user_ship['config'])) { //quark disrupter
                        $temp_str .= " - <a href=attack.php?quark=1&planet_num=$planets[planet_id]>Fire Quark Displacer</a>";
                    } elseif(ereg("sw",$user_ship['config']) && $enable_superweapons == 1) { //terra maelstrom
                        $temp_str .= " - <a href=attack.php?terra=1&planet_num=$planets[planet_id]>Fire Terra Maelstrom</a>";
                    }
                    if($planets['pass'] != '0') {
                        $temp_str .= " - <a href=planet.php?planet_id=$planets[planet_id]>Have Pass</a>";
                    }
                }
            }
        }
        $planets = dbr(1);
    }//end while
}

這是我要實現的目標,但似乎我有一些開放的余地。

$temp_str .= '

            <table width="300" height="84" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <td width="80" rowspan="4"><img src="img/planets/1sm.jpg" width="80" height="82" /></td>
        <td width="110">System '.$user[location].'</td>
        <td width="110">'.$planets[planet_name].'</td>
      </tr>
      <tr>
        <td colspan="2">This planet is guarded by '.$planets[fighters].' Fighters</td>
      </tr>
      <tr>
        <td colspan="2">Clan ID</td>
      </tr>
      <tr>
        <td>';
        if(($planets['login_id'] == $user['login_id']) ||
               ($planets['clan_id'] == $user['clan_id'] &&
               $planets['clan_id'] != 0) || ($user['login_id'] == ADMIN_ID) ||
               ($planets['fighters'] == 0) || $user['ship_id'] == NULL) {
                $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]><img src=\"img/icons/Land.png\" alt=\"Land\" /></a>";
            } else {
                if($flag_planet_attack != 0){
                    $temp_str .= "- <a href=planet.php?planet_id=$planets[planet_id]&attack_planet=1><img src=\"img/icons/Attack.png\" alt=\"Attack\" /></a></td>
        <td>";
            if(ereg("sv",$user_ship['config'])) { //quark disrupter
                        $temp_str .= " - <a href=attack.php?quark=1&planet_num=$planets[planet_id]>Fire Quark Displacer</a>";
                    } elseif(ereg("sw",$user_ship['config']) && $enable_superweapons == 1) { //terra maelstrom
                        $temp_str .= " - <a href=attack.php?terra=1&planet_num=$planets[planet_id]>Fire Terra Maelstrom</a>";
                    }
                    if($planets['pass'] != '0') {
                        $temp_str .= " - <a href=planet.php?planet_id=$planets[planet_id]>Have Pass</a>";
                    }
                }
            }
        }
        $planets = dbr(1);
    }//end while
}       
        </td>
      </tr>
    </table>

            ';

當您在字符串中提到數組值時,您需要將其括在{和}中。 例:

$temp_str .= " - <a href=attack.php?quark=1&planet_num={$planets['planet_id']}>Fire Quark Displacer</a>";

要么

$temp_str .= " - <a href=attack.php?quark=1&planet_num=" . $planets['planet_id'] . ">Fire Quark Displacer</a>";

<a>標記的hrefs中,您有"planet.php?planet_id=$planets[planet_id]>Have Pass</a>"planet_id是變量還是鍵? 由於用雙引號引起來,因此您可以執行"planet.php?planet_id={$planets['planet_id']}>Have Pass</a>"

不確定如何將其放在“盒子”中,但是如果您想讓字符串包含封裝在字符串中的引號,則應轉義這些引號,如下所示:

$string = 'This is a text with quotes. Don\'t forget to escape';

使用雙引號可能會更容易。 請注意,與單引號引起來的字符串不同,可以在雙引號引起來的字符串中使用變量,因此$string = "--- $a ---"將在字符串中填充$a的值。

$string = "Single 'quotes' can be used in double-quote-embedded strings";

如果您需要在PHP代碼中嵌入大量HTML或JavaScript,則可能需要使用以下代碼:

$string = <<<AnyIdentifier

Any text containing special characters. Note that variables are parsed in 
these strings too.

AnyIdentifier;
// Note, closing identifier must occur at the start of the line.

[編輯]

// Code to create weapon HTML goes here. Rename $temp_str to $weaponCode
....
if($flag_planet_attack != 0){
  $weaponCode .= "- <a href=planet.php?planet_id=$planets[plane
.....

// Code to create attack HTML goes here. Rename $temp_str to $attackCode
$attackCode = '... Generated Attack HTML';

$temp_str = <<<totalHtml
<table>
<!-- Note how you can use variables inside this kind of string declaration. -->
<tr>$weaponCode</tr><tr>$attackCode</tr>
</table>

totalHtml;
// String declaration ends on the line above. Note that that line must not 
// contain leading not trailing spaces.

... $planets[planet_id] ...不起作用。

它必須是... {$planets['planet_id']} ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM