繁体   English   中英

php列表宽度

[英]Php column table width

我正在尝试使列的宽度相同,当我有空列时,它们的大小正确,但是当我从MySQL数据库插入表时,它们会发生变化,并且一列比其他列大,一列名为“ BristolQualification”。 我认为这可能与实际表有关,因为我尝试过移动BristolQualification ,并且每个不同的列中的大小都相同。 有两项BathQualification因为我只是在玩耍和测试。 有谁知道为什么会这样? 我尝试用width="25%"替换它们(因为有4列25 * 4 = 100%)。

<?php 
mysql_connect("host", "user", "pwd") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error());

$data = mysql_query("
SELECT  
`Bath`.`BathCountry` ,  
`Bristol`.`BristolCountry` ,  
`Bath`.`BathQualification` ,  
`Bristol`.`BristolQualification` 
FROM  
`Bristol` 
LEFT JOIN  
`Bath` 
ON  
`Bristol`.`BristolCountry` =  `Bath`.`BathCountry` 
ORDER BY  
`Bristol`.`BristolCountry`;
") 
or die(mysql_error());

Echo "<table class=\"imagetable\" border cellpadding=3 style=\"width:100%;\">"; 
while($info = mysql_fetch_array( $data )) 
{ 
Echo "<tr>"; 
Echo "<th width=\"100\">Country:</th>
   <th class=\"Bristol\" width=\"100\">Qualifications to Bristol:</th>
   <th class=\"USW\" width=\"100\">Qualifications to USW:</th> 
   <th class=\"Bath\" width=\"100\">Qualifications to Bath:</th>"; 
Echo "</tr>";
Echo "<tr>";
Echo "<td width=\"100\">".$info['BristolCountry'] . "</td> 
   <td class=\"Bristol2\" width=\"100\">".$info['BathQualification'] . " </td> 
   <td class=\"Bath2\" width=\"100\">".$info['BathQualification'] . " </td> 
   <td class=\"USW2\" width=\"100\">".$info['BristolQualification'] . " </td> "  ;
Echo "</tr>"; 
 } 
 Echo "</table>"; 
 ?>

发生此问题是因为变量字符串长度不相等。我在上一年也遇到过类似问题。您可以按照以下说明解决此问题。

大段引用

$info['BathQualification'] 
$info['BathQualification'] 
$info['BristolQualification'] 

Are not all the same size in length. Guess the strings length are l1, l2, l3 respectively. So   
determine that which one is bigger.Suppose that you have determind that l2>l1>l2 .

Then add :
(l2-l1) spaces with $info['BathQualification'] 
(l2-l2) spaces with $info['BathQualification'] 
(l2-l3) spaces with $info['BristolQualification'] 

现在享受。

您应该使用以下CSS属性: word-break:break-all; word-wrap:break-word word-break:break-all; word-wrap:break-word (强制您的内容适合块的宽度)和table-layout:fixed在表上。

table {
    word-break:break-all;
    word-wrap:break-word;
    table-layout:fixed;
}

尝试这个:

Echo "<table class=\"imagetable\" border cellpadding=3 style=\"width:100%;\">"; 
while($info = mysql_fetch_array( $data )) 
{ 
Echo "<tr>"; 
Echo "<th width=\"100\">Country:</th>
<th class=\"Bristol\" width=\"33%\">Qualifications to Bristol:</th>
<th class=\"USW\" width=\"33%\">Qualifications to USW:</th> 
<th class=\"Bath\" width=\"33%\">Qualifications to Bath:</th>"; 
Echo "</tr>";
Echo "<tr>";
Echo "<td width=\"100\">".$info['BristolCountry'] . "</td> 
<td class=\"Bristol2\">".$info['BathQualification'] . " </td> 
<td class=\"Bath2\">".$info['BathQualification'] . " </td> 
<td class=\"USW2\">".$info['BristolQualification'] . " </td> "  ;
Echo "</tr>"; 
} 
Echo "</table>";

还要确保内容不占用所有td的空间,因此,如果td的宽度为100px(不包含内容),请确保其内容不超过100px

暂无
暂无

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

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