混合PHP和HTML。

时间:2011-08-02 22:53:14

标签: php html-table

我有以下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,&gt;一切。

有人可以告诉我这里我做错了什么吗? 非常感谢您的帮助。

以下是我正在尝试的完整代码......我猜错了:

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>

            ';

4 个答案:

答案 0 :(得分:0)

当您在字符串中提及数组值时,需要将其括在{和}中。例如:

$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>";

答案 1 :(得分:0)

在您的<a>代码的href中,您有"planet.php?planet_id=$planets[planet_id]>Have Pass</a>"planet_id是变量还是密钥?由于它是双引号,您可以执行"planet.php?planet_id={$planets['planet_id']}>Have Pass</a>"

之类的操作

答案 2 :(得分:0)

不确定你是如何'把'放在'一个盒子里',但如果你想要一个字符串包含它被封装的引号,你应该逃避这些引号,如下所示:

$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.

答案 3 :(得分:0)

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

必须是... {$planets['planet_id']} ...

相关问题