Jump to content

If Else Statement Within Table, Possible UL


twilitegxa

Recommended Posts

  • Replies 61
  • Created
  • Last Reply

OK, try this:

 

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="simpletree2.css" />
</head>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("smrpg", $con);
  
//get Sailor Moon's data from the "scout" table
$result = mysql_query("SELECT * FROM scout where identity = 'Sailor Moon'") 
or die(mysql_error());  
$row = mysql_fetch_array( $result );
$birthdatemonth = $row["birthdatemonth"];
$birthdateday = $row["birthdateday"];
$birthdateyear = $row["birthdateyear"];
$birthdate = $birthdatemonth . " " . $birthdateday . ", ". $birthdateyear;
$identity = $row["identity"];

echo "<h1><center>$identity</center></h1>";
echo "<table border='0' width='100%'>";
// keeps getting the next row until there are no more to get
echo "<tr><td width='25%'>"; 
	echo "<b>Name:</b></td><td>"; 
echo $row['charactername'];
        echo "</td></tr><tr><td>"; 
	echo "<b>Element Of Influence:</b></td><td>";
echo $row['elementofinfluence'];
        echo "</td></tr><tr><td>"; 
	echo "<b>Age:</b></td><td>";
echo $row['age'];
        echo "</td></tr><tr><td>";
    echo "<b>Birthdate:</b></td><td>";
echo $birthdate;
echo "</td></tr><tr><td>";  
    echo "<b>Height:</b></td><td>"; 
echo $row['heightfeet'];
echo "&#38;#39; ";
echo $row['heightinches'];
echo "&#38;#34;";
        echo "</td></tr><tr><td>"; 
    echo "<b>Blood Type:</b></td><td>"; 
echo $row['bloodtype'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Hobbies:</b></td><td>"; 
echo $row['hobbies'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Favorite Color:</b></td><td>"; 
echo $row['favoritecolor'];
        echo "</td></tr><tr><td>";   
    echo "<b>Favorite Gemstone:</b></td><td>"; 
echo $row['favoritegemstone'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Favorite Food:</b></td><td>"; 
echo $row['favoritefood'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Least Favorite Food:</b></td><td>"; 
echo $row['leastfavoritefood'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Favorite School Subject:</b></td><td>"; 
echo $row['favoriteschoolsubject'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Least Favorite School Subject:</b></td><td>"; 
echo $row['leastfavoriteschoolsubject'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Strengths:</b></td><td>"; 
echo $row['strengths'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Weaknesses:</b></td><td>"; 
echo $row['weaknesses'];
        echo "</td></tr><tr><td>"; 
    echo "<b>Goal:</b></td><td>"; 
echo $row['goal'];
echo "...";
        echo "</td></tr></table><p>";  
echo $row['biography'];
        echo "</p>"; 
        
echo "</td></tr>"; 
echo "<tr><td> </td></tr>";

echo "</table>";

$result = mysql_query("SELECT * FROM stats where identity = 'Sailor Moon'") 
or die(mysql_error());  
$row = mysql_fetch_array( $result );
echo "<table width='100%' valign='top'><tr><td width='50%' align='right' valign='top'>\n"; // added align='right' valign='top'
echo "<table width='100%' border='0' id='stats' align='right'>\n";
// keeps getting the next row until there are no more to get
echo "<tr><td colspan='3' id='th'>Stats And Derived Values</td></tr>\n";
echo "<tr><td>\n"; 
	echo "<b>Body:</b></td><td col class='right'>\n"; 
	echo $row['body'];
echo "</td></tr><tr><td>\n"; 
	echo "<b>Mind:</b></td><td col class='right'>\n";
	echo $row['mind'];
echo "</td></tr><tr><td>\n"; 
	echo "<b>Soul:</b></td><td col class='right'>\n";
	echo $row['soul'];
echo "</td></tr><tr><td>\n";
	echo " </td></tr><tr><td>\n";
	echo "<b>Health Points:</b></td><td col class='right'>\n"; 
	echo $row['health'];
echo "</td></tr><tr><td>\n"; 
	echo "<b>Energy Points:</b></td><td col class='right'>\n"; 
	echo $row['energy'];
echo "</td></tr><tr><td>\n"; 
	echo "<b>Attack Combat Value:</b></td><td col class='right'>\n"; 
	echo $row['acv'];
echo "</td><td col class='right'>\n"; // added </td>
	echo " (";
	echo $row['acv_sa'];
	echo " for Sailor Scout Attack)";
echo "</td>\n";
echo "</td></tr><tr><td >\n"; 
	echo "<b>Defense Combat Value:</b></td><td col class='right'>\n"; 
	echo $row['dcv'];
echo "</td><td col class='right'>\n"; // added </td>
	echo " (";
	echo $row['dcv_sa'];
	echo " for Sailor Scout Attack)";
//echo "</td>\n"; // that's an extra one
echo "</td></tr><tr><td>\n";   
	echo "<b>Total Character Points:</b></td><td col class='right'>\n"; 
	echo $row['tcp'];
echo "</td></tr>\n"; 
echo "</table>\n";

//while statement for character_attributes
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>\n"; //indent six spaces
		echo $row['attribute_id'];
		echo " ";
		echo $row['desc'];
	echo "</td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['level'];
	echo "</td><td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['points'];
	echo "</td><td width='2%'> </td></tr>\n";
}
//end while statement

$result = mysql_query("SELECT * FROM character_attributes ORDER BY attribute_id ASC") 
or die(mysql_error());  
$row = mysql_fetch_array( $result );
echo "</td><td valign='top' width='50%' rowspan='2'>\n"; // added this whole line
echo "<table width='100%' border='0' id='stats' align='left'>\n";
echo "<tr><td colspan='7' id='th'>Character Attributes And Sub-Attributes</td></tr>\n";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td colspan='3' align='center'>Level</td><td colspan='3' align='center'>Points</td></tr>\n";
echo "<tr><td></td></tr>\n";
// keeps getting the next row until there are no more to get
$attribute_id = "";
while($row = mysql_fetch_array( $result )) {
	if ($row['sub']==2){
		echo "<tr><td>\n{$row['attribute_id']} {$row['desc']}" .
				"</td><td width='2%'> </td><td width='1%' align='right'>\n{$row['level']}</td>" .
				"<td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n" .
				"{$row['points']}</td><td width='2%'> </td></tr>\n";
	}
	elseif ($row['sub']==1){
		if ($row['attribute_id']!=$attribute_id){
			echo "<tr><td colspan='7'>{$row['attribute_id']}</td></tr>\n";
			$attribute_id = $row['attribute_id'];
		}
		echo "<tr><td>\n  {$row['desc']}" .
				"</td><td width='2%'> </td><td width='1%' align='right'>\n{$row['level']}</td>" .
				"<td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n" .
				"{$row['points']}</td><td width='2%'> </td></tr>\n";
	}
	elseif ($row['desc']!=""){
		if ($row['attribute_id']!=$attribute_id){
			echo "<tr><td colspan='7'>{$row['attribute_id']}</td></tr>\n";
			$attribute_id = $row['attribute_id'];
		}
		echo "<tr><td>\n   - {$row['desc']}" .
				"</td><td width='2%'> </td><td width='1%' align='right'>\n{$row['level']}</td>" .
				"<td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n" .
				"{$row['points']}</td><td width='2%'> </td></tr>\n";
	}
}
echo "</table></td></tr><tr><td valign='top' width='50%'>\n";		
$result = mysql_query("SELECT * FROM character_defects where identity = 'Sailor Moon' ORDER BY defect_id ASC") 
or die(mysql_error());  
$row = mysql_fetch_array( $result );
echo "<table border='0' width='100%' id='stats' align='right'>\n";
echo "<tr><td colspan='3' id='th'>Character Defects</td></tr>\n";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>\n"; 
		echo $row['defect_id'];
		echo " ";
		echo $row['desc'];
	echo "</td><td>\n";
		echo $row['bp'];
		echo " BP";
	echo "</td></tr>\n";
}
echo "</table>\n";
echo "</td><td>\n";		
echo "</td></tr></table>\n";
?>

It is SO close! I still need to get Scout Power at the very top of the list. Then the next problem is Items Of Power need a dash in front of them, as do the Sailor Scout Attacks. The Focused Combat only needs to have the desc displayed beside it with no dash in front of it (because it's in a parentheses). And for some reason some of the attributes are missing at the bottom?

I was working with the code some, too, and this was how I got the Scout Power first:

 

$result = mysql_query("SELECT * FROM character_attributes WHERE sub = '2'") 
or die(mysql_error());  
$row = mysql_fetch_array( $result );
echo "</td><td valign='top' width='50%' rowspan='2'>\n"; // added this whole line
echo "<table width='100%' border='0' id='stats' align='left'>\n";
echo "<tr><td colspan='7' id='th'>Character Attributes And Sub-Attributes</td></tr>\n";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td colspan='3' align='center'>Level</td><td colspan='3' align='center'>Points</td></tr>\n";
echo "<tr><td></td></tr>\n";
	echo "<tr><td>\n"; 
		echo $row['attribute_id'];
		echo " ";
		echo $row['desc'];
	echo "</td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['level'];
	echo "</td><td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['points'];
	echo "</td><td width='2%'> </td></tr>\n";
$result = mysql_query("SELECT * FROM character_attributes WHERE sub = '1'")
or die(mysql_error());  
$row = mysql_fetch_array( $result );
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>\n"; 
		echo $row['attribute_id'];

	echo "</td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['level'];
	echo "</td><td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n";
		echo $row['points'];
	echo "</td><td width='2%'> </td></tr>\n";
	}
echo "</table>";

 

The rest still needs to be adjusted like you are adjusting it, but do you see what I did? Could you use this?

You should be able to modify what I wrote to fix it. As far as it sorting, you need to do that in your SELECT statement. The reason some aren't showing up is because of this:

else (if the desc is blank), don't display anything

Add this to line 184 (after the last elseif in the last one):

 

		if ($row['sub']==2){
		echo "<tr><td>\n{$row['attribute_id']} {$row['desc']}" .
				"</td><td width='2%'> </td><td width='1%' align='right'>\n{$row['level']}</td>" .
				"<td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>\n" .
				"{$row['points']}</td><td width='2%'> </td></tr>\n";
	}

It would appear that there are Scout Powers without desc and some with. And using your logic, one with a desc and the same attribute_id, so they display the same.

 

Look through what I've wrote and try to learn how it works. I will answer any questions you have about it.

I am about to give up. I don't understand these if/else types of statements, and I don't know how to write them. I don't even know what to ask, other than how do I do this or that. I am looking at the code, but I can't figure out what to ask or where to start. You obviously know a lot more about this than I do! :-( Thank you for trying to help me though...

Go through this tutorial. It helped me a lot when I was first learning.

http://www.w3schools.com/php/php_if_else.asp

 

You obviously know enough to write what you want it to look like. So, follow that tutorial, compare it to the code I put in there for you, and modify it to your liking.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.