php - 仅格式化 PHP 生成的 HTML 表中的一列

标签 php html css

我正在使用 PHP 自动生成一个 HTML 表格,我希望第二列右对齐。我在某处读到你可以使用 <tgroup><colspec>去做这个。这是我的代码:

$con = mysqli_connect("localhost","user","password","dbase");
$sql="SELECT Document, COUNT(*) as count FROM table WHERE event LIKE 'Photo%' GROUP BY Document ORDER BY count DESC LIMIT 10";
$result = mysqli_query($con,$sql);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
};
$colNames = array_keys(reset($data));
echo "<table id='topTen'><tgroup><colspec column='2' align='right'>";
echo "<thead><tr><th>Photo</th><th>Counts</th></tr></thead><tbody>";
foreach($data as $row) {
    echo "<tr>";
    foreach($colNames as $colName) {
    echo "<td>".$row[$colName]."</td>";
    }   
    echo "</tr>";
    }
echo "</tbody></tgroup></table>";
mysqli_free_result($result);
mysqli_close($con); 

但它不起作用。感谢您的任何建议。

最佳答案

这是一种有点粗略的方法 - 只需检查第二个 <td> is == 2,如果是,添加样式(未测试):

$style = ''; //set var
foreach($data as $row) {
  echo "<tr>";
  $i=1; // start counter
  foreach($colNames as $colName) {
    if($i == 2){
      $style = ' style="text-align: right"'; // style if second col
    }
    echo "<td$style>".$row[$colName]."</td>";
    $style = ''; // reset style var
    $i++;
  }   
  echo "</tr>";
}

关于php - 仅格式化 PHP 生成的 HTML 表中的一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34591442/

相关文章:

javascript - onchange表单输入抓取值并提交表单

jquery - 如何显示/隐藏元素 onclick jquery

html - 主元素不得作为节元素的后代出现

html - Flex 导航栏不在 768 像素及更小的屏幕上居中

html - 我该如何修复这个响应式导航错误?

css - 内容填充第一列然后换行到下一列

php - prestashop 中的 Smarty 分配不分配数组

php - 如何在 PHP 的 https 页面中使用私有(private) SSL 将锁定符号添加到我的网站

html - img 外部 div

php - 使用 CodeIgniter 内置表单验证器类,无需实际提交表单