Jump to content

[SOLVED] Decimal Align in Table?


twilitegxa

Recommended Posts

Is there a way to align an entire column in a table to a decimal point? I have values in my table as:

 

1

16

2

10

2

etc...

 

But I do not want them centered or right or left aligned. I prefer they are aligned to the decimal kind fo like this (they just don't like up right on here, but hopefully you get the idea):

 

1

16

2

10

2

 

Is there a way to do this? My data is being pulled from a database and displayed using PHP, if that makes a difference.

Link to comment
https://forums.phpfreaks.com/topic/124744-solved-decimal-align-in-table/
Share on other sites

Yes, except I don't actually have decimals in my numbers. But I thought it would assume the decimal is at the end, like in math classes lol

 

i.e. 10 = 10.0

 

Will is work if i have no actual decimals and is there a way to do it?

You could also use tables and cheat a little. Split the number up by the decimal and do this:

 

<table>
 <tr>
   <td align="right">1</td><td align="center">.</td><td>10</td>
 </tr>
 <tr>
   <td align="right">10</td><td align="center">.</td><td>0</td>
 </tr>
</table>

I don't understand the string padding thing. Can you help explain it better to me and how I can use it?

 

I couldn't explain it better than the manual and the user notes/examples on the same page.  Specifically, what's the problem you're having with the function?

That's OK, you can put a table within a table.

 

Like this:

<table><tr><td>
<table>
  <tr>
    <td align="right">1</td><td align="center">.</td><td>10</td>
  </tr>
  <tr>
    <td align="right">10</td><td align="center">.</td><td>0</td>
  </tr>
</table>
</td></tr></table>

 

Please excuse the horrible layout.

This is the code I have:

 

echo "<table border='0' width='40%' id='stats'>";
echo "<tr><td colspan='3' id='th'>Character Attributes And Sub-Attributes</td></tr>";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td>Level</td><td>Points</td></tr>";
echo "<tr><td><td></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>"; 
echo $row['attribute_id'];
echo " ";
echo $row['desc'];
echo "</td><td>";
echo $row['level'];
echo "</td><td>";
echo $row['points'];
        echo "</td></tr>";
	}
	echo "</table>";

 

for that part of my page. I tried changing it to this:

 

echo "<table border='0' width='40%' id='stats'>";
echo "<tr><td colspan='3' id='th'>Character Attributes And Sub-Attributes</td></tr>";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td>Level</td><td>Points</td></tr>";
echo "<tr><td><td></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	echo "<tr><td>"; 
echo $row['attribute_id'];
echo " ";
echo $row['desc'];
echo "</td><td><table><tr><td class='right'>";
echo $row['level'];
echo "</td></table></td><td>";
echo $row['points'];
        echo "</td></tr>";
	}
	echo "</table>";

 

because I have a class named 'right' in my CSS that is set to text-align: right, but it didn't work. What am I doing wrong?

The part of the table that includes the 'level' and 'points' from the database. Those two fields are numbers and I want them to be like decimal aligned instead of just right aligned because the cell above those cells is centered and I want the level and points data to be sort of centered within the table, but also decimal aligned within that cell. Does that make sense? Here is a link to the example so you can see it in real time. It might help.

 

http://www.webdesignsbyliz.com/sailormoonrpg/test.php

OK, I think I completely understand now. I thought I did before, but looking at your page made me realize exactly what you want.

 

This will end up a little weird, but here goes:

<?php
echo "<table border='0' width='40%' id='stats'>";
echo "<tr><td colspan='7' id='th'>Character Attributes And Sub-Attributes</td></tr>";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td colspan='3' align='center'>Level</td><td colspan='3' align='center'>Points</td></tr>";
echo "<tr><td><td></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>"; 
echo $row['attribute_id'];
echo " ";
echo $row['desc'];
echo "</td><td width='2%'></td><td width='1%' align='right'>";
echo $row['level'];
echo "</td><td width='2%'></td><td width='1%' align='right'>";
echo $row['points'];
        echo "</td><td width='2%'></td></tr>";
}
echo "</table>";
?>

 

That's an experimental try. Let me know how it goes.

LOL, I see! No, after I saw your code, I decided the table within a table wouldn't do what we want.

 

Try this one (it will at least make the code easier to look at when you view the source):

 

<?php
echo "<table border='0' width='40%' id='stats'>";
echo "<tr><td colspan='7' id='th'>Character Attributes And Sub-Attributes</td></tr>";
echo "<tr class='sub'><td>Attribute/Sub-Attribute</td><td colspan='3' align='center'>Level</td><td colspan='3' align='center'>Points</td></tr>";
echo "<tr><td></td></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>"; 
echo $row['attribute_id'];
echo " ";
echo $row['desc'];
echo "</td><td width='2%'> </td><td width='1%' align='right'>";
echo $row['level'];
echo "</td><td width='2%'> </td><td width='2%'> </td><td width='1%' align='right'>";
echo $row['points'];
        echo "</td><td width='2%'> </td></tr>";
}
echo "</table>";
?>

 

I missed some stuff.

Arg, I missed one thing:

 

<?php
echo "<table border='0' width='40%' id='stats'>\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
while($row = mysql_fetch_array( $result )) {
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";
}
echo "</table>";
?>

Yes! Thank you so much! Do you know anything about, I guess it would be if else statements? See the part in the table where it says all the Items Of Power? What I actually want it to do is display the term Item Of Power once, and then the actual items of power underneath. Kind of like:

 

Item Of Power

      - Upgraded Sailor Suit          1              2

      - Moon Tiara                      1              2

 

ect.

 

Do you know how I can do this? I have the items of power listed in the desc field of my table in my database.

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.