php Foreach 行显示为列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8904731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Foreach row display as column
提问by Viktor
I have a pretty standard database like:
我有一个非常标准的数据库,例如:
id | name | last_name | gender
-----------------------------------------------
1 | John | Doe | Male
2 | Jane | Smith Dolores Clayborne | Female
3 | Paul | Paulson | Male
and I want to display it in a table, but every row in the DB needs to be a column in HTML table:
我想在表格中显示它,但数据库中的每一行都需要是 HTML 表格中的一列:
id | 1 | 2 | 3
---------------------------------------------
name | John | Jane | Paul
---------------------------------------------
last_name | Doe | Smith | Paulson
| | Dolores |
| | Clayborne |
---------------------------------------------
gender | Male | Female | Male
If I go with:
如果我去:
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>";
}
I get all the data in one long column. How do I break the column after every SQL row?
我在一个长列中获取所有数据。如何在每个 SQL 行之后打破列?
Note: $data
is an array of objects that contain properties with the field values (you can probably figure that out from the example).
注意:$data
是一个对象数组,其中包含具有字段值的属性(您可能可以从示例中看出这一点)。
EDIT:
编辑:
I found the solution, see my answer, it's simple and elegant.
我找到了解决方案,请参阅我的答案,它简单而优雅。
回答by Viktor
I figured it out, turns out it's a pretty simple solution with few cycles and pre-populating an array before writing rows and cells.
我想通了,结果证明这是一个非常简单的解决方案,只需很少的周期并在写入行和单元格之前预先填充数组。
I tested it, works like a charm. ;) It might be useful for someone, so here it is:
我测试了它,就像一个魅力。;) 它可能对某人有用,所以这里是:
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>";
回答by Silvertiger
Answer has been updated for adjusted original post requirements as follows:
针对调整后的原始职位要求,答案已更新如下:
this is a wierd (or elegant) way to get what you want done and it will likely take tweaking to get it to look pretty, but it seems to work as you can see at the following url
这是完成您想要完成的事情的一种奇怪(或优雅)的方式,可能需要进行调整才能使其看起来很漂亮,但它似乎可以正常工作,您可以在以下网址中看到
http://www.bluevineconsulting.com/table_test.php
http://www.bluevineconsulting.com/table_test.php
it has the php div method on top and does indeed show that the div's are different sizes as you had hoped would not happen. I have however come up with elegant/ugly solution that works with a table.
它在顶部有 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");
i have to admit it is strange, but it keeps your tables with the correct heights .. the next more complicated part would be to set how many columns to show as this method makes it much more complicated to break after 5 columns like the div method I had earlier.
我不得不承认这很奇怪,但它使您的表格保持正确的高度..下一个更复杂的部分是设置要显示的列数,因为这种方法使得像 div 方法一样在 5 列之后打破变得更加复杂我之前有过。
回答by Tomi
You should use "\n" at the end of every column you want in new line and "\t" for tabs:
您应该在新行中所需的每一列的末尾使用“\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";
}
回答by ghoti
This is a low-tech solution, but it may be sufficient.
这是一种技术含量低的解决方案,但可能就足够了。
$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);
}
回答by Playmen
The easiest way is to run that query within div's and some styling, so the code would be
最简单的方法是在 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>
回答by Sathiya Raj
I hope this working and we can avoid unwanted for loop.
我希望这有效,我们可以避免不必要的 for 循环。
foreach($data as $row) {
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>