Mixing PHP and html.
I have the following PHP code:
$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>
';
Simple enough and displays beautifully.
I'm trying to insert the following in the Attack
and Special weapon
box;
Attack;
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>";
Special Weapon;
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>";
But I keep on getting errors unexpected t_string, > everything.
Can someone please tell me what I'm doing wrong here? Your help is much appreciated.
Below is the full code that I'm trying to ... split, I guess:
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
}
This is what I'm trying to achieve, but it seems that I'm leaving something open.
$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>
';
When you mention an array value in a string you need to enclose it inside { and }. Example:
$temp_str .= " - <a href=attack.php?quark=1&planet_num={$planets['planet_id']}>Fire Quark Displacer</a>";
or
$temp_str .= " - <a href=attack.php?quark=1&planet_num=" . $planets['planet_id'] . ">Fire Quark Displacer</a>";
In your hrefs for your <a>
tags you have "planet.php?planet_id=$planets[planet_id]>Have Pass</a>"
, is planet_id
a variable or a key? Since it's double quoted you could do something like "planet.php?planet_id={$planets['planet_id']}>Have Pass</a>"
No sure how you are 'putting' this in 'a box', but if you want a string to contain the quotes it is encapsulated in, you should escape those quotes, like so:
$string = 'This is a text with quotes. Don\'t forget to escape';
It may be easier to use double quotes. Note that unlike single quoted strings, you can use variables in double quoted strings, so $string = "--- $a ---"
will fill in the value of $a
in the string.
$string = "Single 'quotes' can be used in double-quote-embedded strings";
If you need to embed large chunks of HTML or JavaScript in your PHP code, you might want to use this:
$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.
[edit]
// 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] ...
doesn't work.
it has to be ... {$planets['planet_id']} ...
精彩评论