Div in if statement

C++, C#, Java, PHP, ect...
Post Reply
pretender5552
Posts: 127
Joined: Mon Jan 03, 2011 5:38 am

Div in if statement

Post by pretender5552 »

I am trying to put a div into an if statement and it isn't really work could someone help me please, its the whole problem with php and div if I put the ?> outside it works but seem to stop being a if statement if I put it inside I get the unexpected end error

Code: Select all

<?php
include_once 'connect.php';
include_once 'bannerbox.php';
?>

<link href="style.css" rel="stylesheet" type="text/css" />

<?php

$playerinfo="SELECT * from player where name='Bryan'";
$playerinfo2=mysql_query($playerinfo) or die((mysql_error()));
$playerinfo3=mysql_fetch_array($playerinfo2);

$experienceneeded = ($playerinfo3['level'] * 30) * 1.5;
$experience2 = $experienceneeded - $playerinfo3['experience'];

if ($playerinfo3['experience'] >= $experienceneeded){
	echo "<meta http-equiv='refresh' content='0;url=http://www.immortal.x10.mx/levelplayer.php'>";
}
?>

if ($playerinfo3['statadd'] > 0){
?>
<div id="profile">;
<?php	
	echo "<table border='1' width='330' height='470' cellspacing='1' bordercolor=green><tr><td bgcolor='2e652e' align='left' valign='top' style='vertical-align:top'><img src='hacker.jpg' width='330' height='470' align='top'></td></tr></table>";
?>
</div>

<div id="statbar">
<?php	
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Energy " . $playerinfo3['maxenergy'] . "</b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Energy</font></a></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Tech " . $playerinfo3['maxtech'] . "</b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Tech</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Attack " . $playerinfo3['attack'] . "</b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Attack</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Defense " . $playerinfo3['defence'] . "</b></td><td background='home.jpg'><a href='energy.php'style='text-decoration: none'><font color='2bab4c'><center>Increase Defense</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Health " . $playerinfo3['maxhealth'] . "</b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Health</font></center></tr></table>";	

exit;
?>
</div>
}else{

<div id="profile">;
<?php
echo "<table border='1' width='1005' height='20' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><center>" . $playerinfo3['name'] . "  --  Level: " . $playerinfo3['level'] . " -- Hacker</b></font></td></tr></table><br>";

echo "<table border='1' width='330' height='470' cellspacing='1' bordercolor=green><tr><td bgcolor='2e652e' align='left' valign='top' style='vertical-align:top'><img src='hacker.jpg' width='330' height='470' align='top'></td></tr></table>";
?>
</div>

<div id="nextlevel">
<?php
echo "<table border='1' width='400' height='20' cellspacing='1' bordercolor=green><tr><td bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b><center>You need " . $experience2 . " experience points to reach the next level</center></b></td></tr></table>";
?>
</div>

<div id="stats">
<?php
echo "<table border='1' width='400' height='20' cellspacing='1' bordercolor=green><tr><td width='400' height='20' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Skill Points</b></td></tr>";
echo "<td bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b><br>Energy " . $playerinfo3['maxenergy'] . " Do more missions</b>";
echo "<font color='2bab4c'><b><br><br>Tech " . $playerinfo3['maxtech'] . " Have more fights (2 points ea.)</b>";
echo "<font color='2bab4c'><b><br><br>Attack " . $playerinfo3['attack'] . " Be more effective in combat</b>";
echo "<font color='2bab4c'><b><br><br>Defence " . $playerinfo3['defence'] . " Survive longer in combat</b>";
echo "<font color='2bab4c'><b><br><br>Health " . $playerinfo3['maxhealth'] . " Survive longer in combat<br></b></td></tr></table>";
?>
</div>

<div id="fightstats">
<?php
echo "<table width='400' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b>Fight Stats</td></tr></table>";
echo "<table width='400' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><br>Ranking<br></b><font color='2bab4c'><br><b>Wins<br></b><font color='2bab4c'><b><br>Losses<br><br></b></td>";
echo "<td width='50%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b><br><center>" . $playerinfo3['ranking'] . "</center></b>";
echo "<font color='2bab4c'><b><br><center>" . $playerinfo3['win'] . "</center></b>";
echo "<font color='2bab4c'><b><br><center>" . $playerinfo3['lose'] . "</center><br></b></td></tr></table>";
?>
</div>

<div id="weapon">
<?php
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><center>Weapons</center></td></tr></table>";
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><br></b></td></tr></table>";
?>
</div>

<div id="specielitem">
<?php
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><center>Special Items</center></td></tr></table>";
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><br></b></td></tr></table>";
?>
</div>

<div id="vehicle">
<?php
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><center>Vehicles</center></td></tr></table>";
echo "<table width='1005' border='1' cellspacing='1' bordercolor=green><tr><td bgcolor='000000'><font color='2bab4c'><b><br></b></td></tr></table>";
}
?>
</div>
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Div in if statement

Post by PaxBritannia »

Its actually pretty simple, pretender:

The problem with putting a div inside an echo statement is that the quotations marks end the echo prematurely.

Code: Select all

echo "<div class="divclass">Content</div>";

You can prevent this by only using single quotes inside ('):

Code: Select all

echo "<div class='divclass'>Content</div>";


Alternatively, you can try the 'heredoc' syntax:

Code: Select all

echo <<<EOT
    <div class="divclass">Content</div>
EOT;
Ther heredoc syntax allows you to use unescaped characters inside and will display everything as is with a few exceptions such as variables. If you decide to use the heredoc format, there are 2 things to remember. First is that after '<<<' you must specify an identifier (that is, a word) for when ending the echo. Secondly, when ending a heredoc echo, you must have the same identifier folowed by a semicolon. This ending identifier cannot be indented at all.

Also one last thing.

Code: Select all

<?php
echo "<table border='1' width='400' height='20' cellspacing='1' bordercolor=green><tr><td bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b><center>You need " . $experience2 . " experience points to reach the next level</center></b></td></tr></table>";
?>
Instead of echoing the whole thing, you can just open it up to echo the variable in the middle of the whole thing. This will simplify the whole thing.

Code: Select all

<table border='1' width='400' height='20' cellspacing='1' bordercolor=green><tr><td bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b><center>You need <?php echo $experience2;?> experience points to reach the next level</center></b></td></tr></table>
I hope that helps, looking forward to some screenshots. ;)

pax.
pretender5552
Posts: 127
Joined: Mon Jan 03, 2011 5:38 am

Re: Div in if statement

Post by pretender5552 »

I am going to have to buy you something with all the helping you are giving me, anyway it isn't working for some reason it isn't recognizing that the divs are there

Code: Select all

if ($playerinfo3['statadd'] > 0){
	
	echo "<div class='profile'><table border='1' width='330' height='470' cellspacing='1' bordercolor=green><tr><td bgcolor='2e652e' align='left' valign='top' style='vertical-align:top'><img src='hacker.jpg' width='330' height='470' align='top'></td></tr></table></div>";


	echo "<div class='statbar'><table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Energy " . $playerinfo3['maxenergy'] . "</b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Energy</font></a></center></tr></table></div>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Tech " . $playerinfo3['maxtech'] . "</b></td><td background='home.jpg'><a href='tech.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Tech</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Attack " . $playerinfo3['attack'] . "</b></td><td background='home.jpg'><a href='attack.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Attack</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Defense " . $playerinfo3['defence'] . "</b></td><td background='home.jpg'><a href='defence.php'style='text-decoration: none'><font color='2bab4c'><center>Increase Defense</font></center></tr></table>";
	echo "<table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Health " . $playerinfo3['maxhealth'] . "</b></td><td background='home.jpg'><a href='health.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Health</font></center></tr></table>";	

exit;
}else{
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Div in if statement

Post by PaxBritannia »

Not recognising the divs?

The code should be fine, but maybe the stylesheet got cached, so try clearing your browser's cache.

Code: Select all

<?php

if ($playerinfo3['statadd'] > 0){?>
   
   <div class='profile'><table border='1' width='330' height='470' cellspacing='1' bordercolor=green><tr><td bgcolor='2e652e' align='left' valign='top' style='vertical-align:top'><img src='hacker.jpg' width='330' height='470' align='top'></td></tr></table></div>


   <div class='statbar'><table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Energy <?php echo $playerinfo3['maxenergy'];?></b></td><td background='home.jpg'><a href='energy.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Energy</font></a></center></tr></table></div>
   <table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Tech <?php echo $playerinfo3['maxtech']; ?></b></td><td background='home.jpg'><a href='tech.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Tech</font></center></tr></table>
   <table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Attack <?php echo $playerinfo3['attack']; ?></b></td><td background='home.jpg'><a href='attack.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Attack</font></center></tr></table>
   <table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Defense <?php echo $playerinfo3['defence']; ?></b></td><td background='home.jpg'><a href='defence.php'style='text-decoration: none'><font color='2bab4c'><center>Increase Defense</font></center></tr></table>
   <table border='1' width='350' height='20' cellspacing='1' bordercolor=green><tr><td width='30%' bgcolor='000000' align='left' valign='top' style='vertical-align:top'><font color='2bab4c'><b>Health <?php echo $playerinfo3['maxhealth']; ?></b></td><td background='home.jpg'><a href='health.php' style='text-decoration: none'><font color='2bab4c'><center>Increase Health</font></center></tr></table><?php

exit;
}else{
If that doesn't work, you might want to try this. In fact, if it works we still might want to use this. The code turns out cleaner and it is less likely to give errors.

pax.


p.s. Don't mention it! It's my duty to help you, but in times when I need help, I'm sure you'll be there for me as well. You don't need to buy me anything, its the thought that counts. Thanks for thinking about me!
Last edited by PaxBritannia on Sun Jan 16, 2011 9:11 am, edited 2 times in total.
pretender5552
Posts: 127
Joined: Mon Jan 03, 2011 5:38 am

Re: Div in if statement

Post by pretender5552 »

Yeah one one is stuck under the other instead of where I placed it in the style.css and putting them in this new format breaks of the tables instead playing them flat like one on top of the other
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Div in if statement

Post by PaxBritannia »

Does it work now?

A screenshot would definitely help in seeing the problem.

pax.
pretender5552
Posts: 127
Joined: Mon Jan 03, 2011 5:38 am

Re: Div in if statement

Post by pretender5552 »

Image
doesn't really work yet, the photo is supposed to be under the buttons I have made, and the stat increasing buttons are supposed to be to the right of the photo
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Div in if statement

Post by PaxBritannia »

If I understand correctly, the buttons and photo are supposed to be on the right, with the photo under the buttons? Am I correct?

And did it work before the php errors?

pax.
pretender5552
Posts: 127
Joined: Mon Jan 03, 2011 5:38 am

Re: Div in if statement

Post by pretender5552 »

It worked when the if statement wasn't working and yes photo on the left below the buttons, and the other buttons on the right below the buttons
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: Div in if statement

Post by PaxBritannia »

This is getting into CSS territory, and there is no one right way to do it, so well have to work together.

First off, style.css please.

pax.
Last edited by PaxBritannia on Sun Jan 16, 2011 9:54 am, edited 1 time in total.
Post Reply

Return to “Coding”