我正在尝试从php中的mysql中提取一个表,其中列名将用作标题,列中的值将显示为值。
| id | firstname | lastname | | 1 | Joe | Jones | | 2 | Cal | Clark | | 3 | Rob | Robin |
问题是我要回来了
| id | firstname | lastname | | 1 | Joe | Jones | | id | firstname | lastname | | 2 | Cal | Clark | | id | firstname | lastname | | 3 | Rob | Robin |
请帮助我做错了。
P.S我对编程很新,也不介意糟糕的HTML。
$query = "SELECT * ";
$query .= "FROM {$selected_table} ";
$query .= "LIMIT 0, 30";
$confirmed_query = confirm_query($query);
$query = mysql_query($confirmed_query);
echo "<table>";
while ($query_result = mysql_fetch_assoc($query)) {
echo "<tr>";
foreach ($query_result as $columns => $rows) {
echo "<th>{$columns}</th>";
}
echo "</tr><tr>";
foreach ($query_result as $colums => $rows) {
echo "<td>$rows</td>";
}
echo "</tr>";
}
echo "</table>";
有没有其他方法可以使用foreach从数组中取出列名,这将导致它返回数组中每条记录的列名?
答案 0 :(得分:3)
这是do...while
的工作!
$query_result = mysql_fetch_assoc($query);
if ($query_result) {
echo "<table>";
echo "<tr>";
foreach ($query_result as $columns => $rows) {
echo "<th>{$columns}</th>";
}
echo "</tr>";
do {
echo "<tr>";
foreach ($query_result as $colums => $rows) {
echo "<td>$rows</td>";
}
echo "</tr>";
} while ($query_result = mysql_fetch_assoc($query));
echo "</table>";
}
但我也给@outis的答案+1,因为我们都应该使用PDO - 而不是陈旧和劣质的mysql扩展。
答案 1 :(得分:2)
将表格标题部分移到while循环之外。
答案 2 :(得分:2)
您必须只编写一次表头,而不是每个记录。使用这样的东西:
$headerWritten = false;
echo "<table>";
while ($query_result = mysql_fetch_assoc($query)) {
// Write header
if(!$headerWritten) {
echo "<tr>";
foreach ($query_result as $columns => $rows) {
echo "<th>{$columns}</th>";
}
echo "</tr>";
$headerWritten = true;
}
// Write rows
echo "<tr>";
foreach ($query_result as $colums => $rows) {
echo "<td>$rows</td>";
}
echo "</tr>";
}
echo "</table>";
答案 3 :(得分:1)
使用PDO和PDOStatement::getColumnMeta
在循环之前获取列名称。
完整的解决方案有点涉及,因为它还应该分开数据访问和显示。以下内容仅用作说明,而不是生产代码中应使用的任何内容。
<?php
abstract class Model {
abstract function fields();
function __get($name) {
/* Won't work with overloaded methods (`__call`) by design. Accessing
non-nullary methods as properties will cause warnings. To prevent this
at a time-cost, use ReflectionMethod to check the arity of the method.
*/
if (method_exists($this, $name)) {
return $this->$name();
}
}
...
}
class ModelCollection extends Model {
// use the delegate pattern, rather than inheritance
protected $_delegate, $_fields;
function __construct($result) {
$this->_delegate = $result;
}
function __call($name, $args) {
if (is_callable($this->_delegate, $name)) {
return call_user_func_array(array($this->_delegate, $name), $args);
} else {
$delegateClass = get_class($this->_delegate);
$myClass = get_class($this);
throw new BadMethodCallException("Method {$delegateClass}::{$name} (called as {$myClass}::{$name}) doesn't exist.");
}
}
/* Here's where we get the column names */
function fields() {
if (is_null($this->_fields)) {
$columnCount = $this->_delegate->columnCount();
for ($i=0; $i < $columnCount; ++$i) {
$this->_fields[] = $this->_delegate->getColumnMeta($i);
// perhaps massage data into some other format
}
}
return $this->_fields;
}
}
abstract class View {
public $data;
abstract function display();
...
}
class TableView extends View {
function display() {
?>
<table>
<?php
$this->_display_head();
$this->_display_body();
?>
</table>
<?php
}
/* Here's the first place we use the column names */
protected function _display_head() {
?>
<thead>
<tr>
<?php foreach ($this->data->fields as $fields) { ?>
<th><?php echo $fields['name'] ?></th>
<?php } ?>
</tr>
</thead>
<?php
}
protected function _display_body() {
?>
<tbody>
<?php
foreach ($this->data as $row) {
$this->_display_row($row);
}
?>
</tbody>
<?php
}
/* Here's the second place we use the column names */
protected function _display_row(&$row) {
?>
<tr>
<?php foreach ($this->data->fields as $fields) { ?>
<th><?php echo $row[$fields['name']] ?></th>
<?php } ?>
</tr>
<?php
}
}