Смешивание PHP и HTML. - PullRequest
       2

Смешивание PHP и HTML.

1 голос
/ 03 августа 2011

У меня есть следующий код 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>

            ';

Достаточно прост и прекрасно выглядит.

Я пытаюсь вставить следующее в поля Attack и Special 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>

            ';

Ответы [ 4 ]

0 голосов
/ 03 августа 2011

... $planets[planet_id] ... не работает.

должно быть ... {$planets['planet_id']} ...

0 голосов
/ 03 августа 2011

В ваших hrefs для ваших тегов <a> есть "planet.php?planet_id=$planets[planet_id]>Have Pass</a>", является ли planet_id переменной или ключом? Поскольку это двойные кавычки, вы можете сделать что-то вроде "planet.php?planet_id={$planets['planet_id']}>Have Pass</a>"

0 голосов
/ 03 августа 2011

Не знаю, как вы «помещаете» это в «коробку», но если вы хотите, чтобы строка содержала кавычки, в которые она заключена, вам следует избегать этих кавычек, например:

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

Если вам нужно встроить большие куски HTML или JavaScript в код PHP, вы можете использовать это:

$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.
0 голосов
/ 03 августа 2011

Когда вы упоминаете значение массива в строке, вам нужно заключить его в {и}. Пример:

$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>";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...