我有一个非常标准的数据库,如:
id | name | last_name | gender
-----------------------------------------------
1 | John | Doe | Male
2 | Jane | Smith Dolores Clayborne | Female
3 | Paul | Paulson | Male
我希望在表格中显示它,但数据库中的每一行都需要是HTML表格中的一列:
id | 1 | 2 | 3
---------------------------------------------
name | John | Jane | Paul
---------------------------------------------
last_name | Doe | Smith | Paulson
| | Dolores |
| | Clayborne |
---------------------------------------------
gender | Male | Female | Male
如果我选择:
foreach($data as $row) {
echo "<tr><td>" . $row->id . "</td></tr>";
echo "<tr><td>" . $row->name . "</td></tr>";
echo "<tr><td>" . $row->last_name . "</td></tr>";
echo "<tr><td>" . $row->gender . "</td></tr>";
}
我在一个长列中获取所有数据。如何在每个SQL行之后中断列?
注意:$data
是一个对象数组,其中包含带有字段值的属性(您可以从示例中看出来)。
修改
我找到了解决方案,看到我的答案,它简单而优雅。
答案 0 :(得分:4)
我想通了,原来这是一个非常简单的解决方案,几个周期并在编写行和单元格之前预先填充数组。
我测试了它,就像一个魅力。 ;)它可能对某人有用,所以这里是:
foreach($records as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
//This creates a new array composed/transposed with the field names as keys and
//the "rowed" values as sub-arrays.
echo "<table>\n";
foreach ($recNew as $key => $values) // For every field name (id, name, last_name, gender)
{
echo "<tr>\n"; // start the row
echo "\t<td>" . $key . "</td>\n" ; // create a table cell with the field name
foreach ($values as $cell) // for every sub-array iterate through all values
{
echo "\t<td>" . $cell . "</td>\n"; // write cells next to each other
}
echo "</tr>\n"; // end row
}
echo "</table>";
答案 1 :(得分:2)
已针对调整后的原始帖子要求更新了答案,如下所示:
这是获得你想要的东西的一种奇怪的(或优雅的)方式,它可能需要调整以使它看起来漂亮,但它似乎可以工作,你可以看到以下网址
http://www.bluevineconsulting.com/table_test.php
它有顶部的php div方法,确实显示div是不同的大小,因为你希望不会发生。然而,我提出了一个优雅/丑陋的解决方案,适用于桌子。
// i populated a fake object so i could test with it
$data = new stdClass();
$data->student1->id = "1";
$data->student1->name = "Test1";
$data->student1->last_name = "User1";
$data->student1->gender = "Male";
$data->student2->id = "2";
$data->student2->name = "Test";
$data->student2->last_name = "User<br>\nMaiden<br>\nOther\n";
$data->student2->gender = "Female";
$data->student3->id = "3";
$data->student3->name = "Test3";
$data->student3->last_name = "User3";
$data->student3->gender = "Male";
$data->student4->id = "4";
$data->student4->name = "Test4";
$data->student4->last_name = "User4";
$data->student4->gender = "Female";
$data->student5->id = "5";
$data->student5->name = "Test5";
$data->student5->last_name = "User5";
$data->student5->gender = "Female";
$data->student6->id = "6";
$data->student6->name = "Test6";
$data->student6->last_name = "User6";
$data->student6->gender = "Female";
$datacount = count($data);
foreach($data as $row) {
$datacount++;
if (empty($colcount)) {
foreach ($row as $key => $val) {
$colcount++;
$keyname[$colcount] = $key;
}
echo "Columns = ".$colcount."<br>\n";
}
}
echo "records = ".$datacount."<br>\n<br>\n";
echo ("<b>PHP Table method:</b><br>\n<br>\n");
echo ("<table border='1px' cellspacing='0'>\n");
foreach ($keyname as $key) {
echo " <tr style='vertical-align:top;'>\n";
for ($d = 1; $d < $datacount; $d++) {
$id = "student".$d;
echo " <td>".$data->$id->$key."</td>\n";
}
echo " </tr>\n";
}
echo ("</table>\n");
我不得不承认它很奇怪,但它会让你的桌子保持正确的高度..下一个更复杂的部分是设置显示多少列,因为这个方法使得在5列之后破坏更加复杂我之前的div方法。
答案 2 :(得分:0)
你应该在新行的每一列的末尾使用“\ n”,在标签中使用“\ t”:
foreach($data as $row) {
echo "\t<tr><td>" . $row->id . "</td></tr>\n";
echo "\t<tr><td>" . $row->name . "</td></tr>\n";
echo "\t<tr><td>" . $row->last_name . "</td></tr>\n";
echo "\t<tr><td>" . $row->gender . "</td></tr>\n";
}
答案 3 :(得分:0)
这是一种低技术解决方案,但可能就足够了。
$fmt="<tr><td>%s<br/>%s<br/>%s<br/>%s</td></tr>\n";
foreach($data as $row) {
printf($fmt, $row->id, $row->name, $row->lastname, $row->gender);
}
答案 4 :(得分:0)
最简单的方法是在div和一些样式中运行该查询,因此代码将是
<div align="center" style="float:left; width:100px; ">
<?
$query = "select Something from your database";
$result = mysql_query($query);
$results = mysql_num_rows($result);
//Now run the loop
for($i=0; $i<$results; $i++)
{
echo'<div align="center" style="float:left; width:100px;">';
$records=mysql_fetch_assoc($result);
foreach($records as $item)
{
echo'<div style="float:left;">'.$item.'</div>';
}
echo '</div>';
}
?>
</div>
答案 5 :(得分:0)
我希望这有效,我们可以避免不必要的循环。
foreach($ data as $ row){
$idRow .= '<td>' . $row->id . '</td>';
$nameRow .= '<td>' . $row->name . '</td>';
$lastNmaeRow .= '<td>' . $row->last_name . '</td>';
$genderRow .= '<td>' . $row->gender . '</td>';
}
<tr><td>Id</td>'.$idRow.'</tr>
<tr><td>Name</td>'.$nameRow.'</tr>
<tr><td>Last Name</td>'.$lastNmaeRow.'</tr>
<tr><td>Gender</td>'.$genderRow.'</tr>