PHP回音表及其css
我真的需要你的帮助。我试图从我的数据库中回显一个表,但首先它带有我不想要的数据库中的表名,而且css很奇怪。我希望我的表格在每列中有一个不同的标题,然后在相应的列标题下以统一的方式显示数据 php代码在这里PHP回音表及其css,php,html,mysql,css,Php,Html,Mysql,Css,我真的需要你的帮助。我试图从我的数据库中回显一个表,但首先它带有我不想要的数据库中的表名,而且css很奇怪。我希望我的表格在每列中有一个不同的标题,然后在相应的列标题下以统一的方式显示数据 php代码在这里 <div id="content-1"> <div> <?php $table = 'std_details'; // sending query
<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>
谢谢各位。我真的需要帮助
对于字段名中的表名,您可以使用一个简单的
sustr($field->name,strlen($fieldName……)
对于标题,使用
而不是
,并且不使用单元格值的制表符。
像这样试试
// 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";
}
//打印表头
对于($i=0;$i<$fields_num;$i++){
$field=mysql\u fetch\u字段($result);
//删除表名
$name=substr($field->name,strlen($fieldName)-strlen($yourTableName),strlen($fieldName)-1)
//回显标题th
回显“{$name}”;
}
回音“\n”;
//打印表行
while($row=mysql\u fetch\u row($result)){
回声“;
//$row是数组….foreach(..)放置每个元素
//将$row设置为$cell变量
foreach($行作为$单元格){
//回显手机td
回声“$cell”;
}
回音“\n”;
}
我无法理解您为什么使用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>
表格{
字体系列:verdana、arial、无衬线字体;
字体大小:11px;
颜色:#333333;
边框宽度:1px;
边框颜色:#666666;
边界塌陷:塌陷;
}
table.gridtable th{
边框宽度:1px;
填充:8px;
边框样式:实心;
边框颜色:#666666;
背景色:#dedede;
}
table.gridtable td{
边框宽度:1px;
填充:8px;
边框样式:实心;
边框颜色:#666666;
背景色:#ffffff;
}
不知道你所说的“css很奇怪”是什么意思。我们看不到您的数据库,所以我们不知道从中得到了什么以及用什么替换它!而且,mysqli比不推荐使用的mysql要好。你们有SQL注入,甚至HTML注入。若你们想要单独的标题,那个么不要从数据库中提取。保持列标题为静态并从数据库加载数据这是my database@MrListerPlease的图像请注意,mysql
不再受支持。改用PDO
或mysqli
。如何使用PDO或mysqli?我是新来的。我只想完成这件事,然后开始新的事情。请帮助我,我使用PDOyes is substr。(基本php str函数)我已经更新了答案。那么PDO或mysqli是如何进来的呢?如果我可能会问,我是否必须用我想要的特定列的标题替换“$fieldName”?我如何使用mysqli?我从来没用过。有什么区别@GULIMbecause mysql是不推荐使用的,只是因为它的语法不同
<!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>