有人可以帮我用mysql组织我在php中显示的数据吗?
我的代码
<center>
<?php
$db_host = '';
$db_user = '';
$db_pwd = '';
$database = '';
$table = 'pkers';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table} ORDER BY points DESC");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo '<br>';
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "{$field->points}";
}
// printing table rows
while($row = mysql_fetch_row($result))
{
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
// printing table headers
for($i=0; $i<$fields_num; $i++)
// printing table rows
while($row = mysql_fetch_row($result))
{
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell";
}
}
?></center>
我的结果显示了这个
我希望在显示下一个单元格之前暂停一下,
TOP PKERS:
DANNY 1
USERNAME 0
如果有人能提供帮助就会很棒!
答案 0 :(得分:2)
只需在外部循环中添加<br />
。
while($row = mysql_fetch_row($result))
{
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo " $cell ";
echo "<br />";
}
或者,您可以使用<table>
布局来获得更好的视图。
答案 1 :(得分:1)
更改以下代码:
foreach($row as $cell)
echo "$cell";
带
foreach($row as $cell)
echo "$cell ";
echo "<br/>";
它会根据需要输出。