首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >混用PHP和html。

混用PHP和html。
EN

Stack Overflow用户
提问于 2011-08-03 06:53:14
回答 4查看 578关注 0票数 1

我有以下PHP代码:

代码语言:javascript
运行
复制
$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框中插入以下内容;

攻击;

代码语言:javascript
运行
复制
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>";

特种武器;

代码语言:javascript
运行
复制
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,>所有的错误。

有人能告诉我我哪里做错了吗?非常感谢您的帮助。

下面是我正在尝试的完整代码...分裂,我猜:

代码语言:javascript
运行
复制
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
}

这就是我想要实现的目标,但似乎我留下了一些东西。

代码语言:javascript
运行
复制
$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>

            ';
EN

回答 4

Stack Overflow用户

发布于 2011-08-03 06:57:27

在字符串中提到数组值时,需要将其括在{和}中。示例:

代码语言:javascript
运行
复制
$temp_str .= " - <a href=attack.php?quark=1&planet_num={$planets['planet_id']}>Fire Quark Displacer</a>";

代码语言:javascript
运行
复制
$temp_str .= " - <a href=attack.php?quark=1&planet_num=" . $planets['planet_id'] . ">Fire Quark Displacer</a>";
票数 0
EN

Stack Overflow用户

发布于 2011-08-03 06:58:12

<a>标记的href中有"planet.php?planet_id=$planets[planet_id]>Have Pass</a>"planet_id是一个变量还是一个键?因为它是双引号的,所以您可以执行类似"planet.php?planet_id={$planets['planet_id']}>Have Pass</a>"的操作

票数 0
EN

Stack Overflow用户

发布于 2011-08-03 06:58:53

不知道你是如何把它‘放’进‘一个盒子’的,但是如果你想让一个字符串包含它所封装的引号,你应该转义这些引号,如下所示:

代码语言:javascript
运行
复制
$string = 'This is a text with quotes. Don\'t forget to escape';

使用双引号可能更容易。请注意,与单引号字符串不同,您可以在双引号字符串中使用变量,因此$string = "--- $a ---"将在字符串中填充$a的值。

代码语言:javascript
运行
复制
$string = "Single 'quotes' can be used in double-quote-embedded strings";

如果你需要在你的PHP代码中嵌入大量的超文本标记语言或者JavaScript代码,你可能需要使用下面的代码:

代码语言: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.

编辑

代码语言:javascript
运行
复制
// 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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6919677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档