Php列表宽度

时间:2014-04-13 20:19:04

标签: php html mysql css

我试图让我的列宽度相同,当我有空列时它们的大小正确但是当我从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>"; 
 ?>

3 个答案:

答案 0 :(得分:2)

问题正在发生,因为变量字符串长度不相等。我在前一年也面临着喜欢。你可以按照以下说明来解决这个问题。

  

块引用

$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'] 
  

块引用   现在享受。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

试试这个:

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