简体   繁体   English

如何在循环内向表添加对齐(左,中,右)

[英]How to add align (left, center, right) to table inside a loop

How to add align to my table loop? 如何添加对齐到我的表循环?

I have code: 我有代码:

<table width="960px" border="0" cellspacing="0" cellpadding="0">
  <tr valign=top>  
<?
activate ('main/inside');
$rid = get ('main/inside');
$l = openFilesList (PAGE, 'id', ASC, 20);
$i = 0;
while ($id = readList ($l)) {
setActive ($id);
if (!$rid) $rid = _say ('Раздел');
$q = 0;
$arr = array('left', 'center', 'right');
if ($i == 0) echo '<td align='.$arr[$q++].'>';
    ?>
"some text"
<?  if ($i++ == 1) {
        echo '</td>';
        $i = 0;
    }
}
if ($i>0) {

    echo str_repeat ('', 2-$i);
    echo '';
}

?>

</tr>
    </table>

The output should be 输出应为

... <td align="left">some text some text</td>
<td align="center">some text some text</td>
<td align="right">some text some text</td>...

but my code does not work properly. 但我的代码无法正常工作。

change 更改

if ($i == 0) echo '<td align='.$arr[$q++].'>';

to

if (!$i) echo '<td style="text-align:'.$arr[$q++].';">';

it will help when you use CSS instead of old HTML. 当您使用CSS而不是旧的HTML时,它将很有帮助。 More about text align here: 有关文本的更多信息,请在此处对齐:

http://www.w3schools.com/cssref/pr_text_text-align.asp http://www.w3schools.com/cssref/pr_text_text-align.asp

also remember that $arr[$q++] can have values: left, right, center, justify and inherit. 还请记住, $arr[$q++]可以具有以下值:左,右,居中,对齐和继承。 No other value will be accepted. 不会接受其他任何值。

The thing you want to do may be done without PHP as well it can be done via JS or CSS. 您想要做的事情也可以不用PHP来完成,也可以通过JS或CSS来完成。 For example if you put in 例如,如果您输入

you can use CSS3 code 您可以使用CSS3代码

 table.mytable tdp:nth-child(1){text-align:left;}
 table.mytable tdp:nth-child(2){text-align:center;}
 table.mytable tdp:nth-child(3){text-align:right;}

it will make first column align to left, second column to center and third column to right. 它将使第一列向左对齐,第二列向中心对齐,第三列向右对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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