简体   繁体   中英

Style 1 <td> in a table with 2 <td>s

I have a table that gets it´s content from a database, and I want to make the text in the column to the right align-right. I know how to do it when its HTML and the content is in the code, but I can't get it to work when the content is outputted from a database.

Down here you can see my code for the table: (in the collumn "ingredienser", I want to set the text-align to right.

<?php
$con = mysql_connect("****","****","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("paj", $con);

$result = mysql_query("SELECT * FROM meny");


echo "<div id='menyer'>";
echo "<table border='1'>
<tr>

</tr>";

while($row = mysql_fetch_array($result))
  { 
    echo "<div class='mall" . $row['mall'] . "'>";
    echo "<tr>";
    echo "<td>" . $row['namn'] . "</td>";
    echo "<td>" . $row['ingredienser'] . "'</td>";
    echo "</tr>";

Just add some CSS above your php code

<style>
table td {
   text-align: right;
}
</style>

The above will right align text inside table cells

echo "<td><div align='right'>" . $row['ingredienser'] . "'</div></td>";

if this is the extent of your table, then you can definitely just use the following css:

.mall{
    text-align:right;
}

I would suggest putting it in your external css file, but if you don't have one, then put it in a style tag inside of your body or table tag, depending on what kind of scope you want it to have like this:

<style>
.mall{
    text-align:right;
}
</style>

set align="right" to your tr

while($row = mysql_fetch_array($result))
  { 
    echo "<div class='mall" . $row['mall'] . "'>";
    echo "<tr align='right'>";
    echo "<td>" . $row['namn'] . "</td>";
    echo "<td>" . $row['ingredienser'] . "'</td>";
    echo "</tr>";

Just add styling to that one particular cell, it will apply to the entire column as it loops. As such,

echo "<td style=\\"text-align:right;\\">" . $row['ingredienser'] . "'</td>";

If you align the Table TD to text-align:right , EVERY single cell will be effected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM