我真的需要你的帮助。我试图从我的数据库中回显一个表,但它首先来自我不想要的数据库中的表名,而且css很奇怪。我希望我的表在每列中有一个不同的标题,然后数据显示在相应列标题下面的统一吗哪。
php代码在这里
<div id="content-1">
<div>
<?php
$table = 'std_details';
// sending query for table
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die ("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>All Students</h1>";
echo "<table border='2' class='gridtable'><tr>";
// printing table headers
for ($i=0; $i < $fields_num; $i++) {
$field = mysql_fetch_field($result);
echo "<td>{$field->name} </td>";
}
echo "</tr>\n";
// printing table rows
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
// $row is array.... foreach( .. ) puts every element
// of $row to $cell variable
foreach ($row as $cell) {
echo "<td> \t$cell</td>";
}
echo "</tr>\n";
}
mysql_free_result($result)
?>
</div>
</div>
表css是
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
谢谢你们。我真的需要帮助!
答案 0 :(得分:1)
对于字段名称中的表名,您只需使用sustr($field->name, strlen ( $fieldName ........))
对于标题,请使用<th>
而非<td>
,并且不要使用标签作为单元格值。
试试这个
// printing table headers
for ($i=0; $i < $fields_num; $i++) {
$field = mysql_fetch_field($result);
// eliminate the tablename
$name = substr($field->name, strlen ( $fieldName ) - strlen('yourTableName),strlen ( $fieldName ) -1 )
// echo the header th
echo "<th>{$name} </th>";
}
echo "</tr>\n";
// printing table rows
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
// $row is array.... foreach( .. ) puts every element
// of $row to $cell variable
foreach ($row as $cell) {
// echo the cell td
echo "<td>$cell</td>";
}
echo "</tr>\n";
}
答案 1 :(得分:1)
我无法理解你为什么使用mysql而不是mysqli,这可能对你有所帮助
<!DOCTYPE html>
<html>
<head>
<style>
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<!--- uncomment these line if you are loading css from external file --->
<!--- <link href="Your css file" rel="stylesheet"> --->
</head>
<div id="content-1">
<div>
<?php
$table = 'std_details';
// sending query for table
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die ("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>All Students</h1>";
echo "<table border='2' class='gridtable'><tr>";
// printing table headers
for ($i=0; $i < $fields_num; $i++) {
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>";
// printing table rows
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
// $row is array.... foreach( .. ) puts every element
// of $row to $cell variable
foreach ($row as $cell) {
echo "<td> \t$cell</td>";
}
echo "</tr>";
}
echo "</table>";
mysql_free_result($result)
?>
</div>
</div>