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

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. More about text align here:

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

also remember that $arr[$q++] can have values: left, right, center, justify and inherit. 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. For example if you put in

you can use CSS3 code

 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.