問題描述
CodingBiz 更新:
我把它放在我的代碼中:
I'm putting this in my code:
for($i=1;$i<=$numRows;$i++) {
$output .= '<tr>';
$row = $this->fetchAssoc($result);
$colRow = $this->fetchAssoc($colResult);
foreach($colRow as $colName) {
$output .= "<td>".$row[$colName]."</td>";
}
$output .= '</tr>';
}
代替
for($i=1;$i<=$numRows;$i++) {
$output .= '<tr>';
$row = $this->fetchAssoc($result);
for($j=1;$j<=$colNumRows;$j++) {
$colRow = $this->fetchAssoc($colResult);
$output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
}
$output .= '</tr>';
}
這有什么問題嗎?
原帖:
我正在 PHP 類中編寫一個函數(shù)來在表中顯示查詢結(jié)果.我沒有自己構(gòu)建任何表格,我希望它使用 PHP 完成所有工作.到目前為止,這是我的代碼:
I'm writing a function in a PHP class to display the results of a query in a table. I'm not structuring any of the table myself, I want it everything to be done using PHP. Here is my code so far:
function allResults($table,$cols) {
if(isset($cols)) {
$query = "SELECT $cols FROM $table";
}
else {
$query = "SELECT * FROM $table";
}
$result = $this->query($query);
$numRows = $this->numRows($result);
$colQuery ="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='shareride' AND TABLE_NAME='$table'";
$colResult = $this->query($colQuery);
$colNumRows = $this->numRows($colResult);
$output = '<table class="allResults">';
$output .= '<tr>';
for($i=1;$i<=$colNumRows;$i++) {
$colRow = $this->fetchAssoc($colResult);
$output .= "<td>".$colRow["COLUMN_NAME"]."</td>";
}
$output .= '</tr>';
for($i=1;$i<=$numRows;$i++) {
$output .= '<tr>';
$row = $this->fetchAssoc($result);
for($j=1;$j<=$colNumRows;$j++) {
$colRow = $this->fetchAssoc($colResult);
$output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
}
$output .= '</tr>';
}
$output .= '</table>';
return $output;
}
如果不清楚,query
指的是mysqli_query
,numRows
指的是mysqli_num_rows
,>fetchAssoc
指的是 mysqli_fetch_assoc
.數(shù)據(jù)庫名稱是shareride".
In case it is unclear, query
refers to mysqli_query
, numRows
refers to mysqli_num_rows
, and fetchAssoc
refers to mysqli_fetch_assoc
. The database name is "shareride."
我知道我在這一行中遺漏了一些東西:
I know I am missing something in this line:
$output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
但我只是不知道它是什么.現(xiàn)在,我正確顯示了所有表格列標(biāo)題,并獲得了正確數(shù)量的內(nèi)容行,但我無法使用數(shù)據(jù)庫中的實(shí)際數(shù)據(jù)填充這些行.
but I just don't know what it is. Right now, I get all the table column titles displayed correctly, and I get the correct number of content rows, but I just can't populate those rows with the actual data from the database.
我錯過了什么?任何幫助將非常感激!
What am I missing? Any help would be GREATLY appreciated!
推薦答案
從同一個結(jié)果集中獲取數(shù)據(jù)和列名
Get the data and column names from the same result set
<?php
$i = 0;
$colNames = array();
$data = array();
while($row = ***_fetch_assoc($res)) //where $res is from the main query result not schema information
{
//get the column names into an array $colNames
if($i == 0) //make sure this is done once
{
foreach($row as $colname => $val)
$colNames[] = $colname;
}
//get the data into an array
$data[] = $row;
$i++;
}
?>
更新:@YourCommonSense 建議替換上面的代碼,它有效,簡單且更短 - 一種無需像我那樣循環(huán)即可獲取列名/數(shù)組鍵的方法
$data = array();
while($row = mysql_fetch_assoc($res))
{
$data[] = $row;
}
$colNames = array_keys(reset($data))
繼續(xù):打印表格
<table border="1">
<tr>
<?php
//print the header
foreach($colNames as $colName)
{
echo "<th>$colName</th>";
}
?>
</tr>
<?php
//print the rows
foreach($data as $row)
{
echo "<tr>";
foreach($colNames as $colName)
{
echo "<td>".$row[$colName]."</td>";
}
echo "</tr>";
}
?>
</table>
測試結(jié)果
您可以看到我如何將數(shù)據(jù)檢索與表生成分離.它們現(xiàn)在相互依賴,您可以通過使用靜態(tài)數(shù)據(jù)填充數(shù)組來測試沒有數(shù)據(jù)庫的表生成
You can see how I separated the data retrieval from table generation. They are dependent of each other now and you can test your table generation without the database by populating the arrays with static data
你也可以把它們做成單獨(dú)的函數(shù).
You can also make them into separate functions.
這篇關(guān)于使用 PHP 在 HTML 表格中顯示 MySQL 結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!