PHP表由从SQL数据库加载的3行组成 [英] PHP tables consisting of 3 rows loaded from SQL database
问题描述
我有一个数据库加载了不同的教堂信息,我试图将数据库中的所有信息插入到3行的PHP表中。
I have a database loaded with different churches information, I am trying to insert all the information from the database into a PHP table with 3 rows.
我想要每个单元格的结构为:
I would like the structure of each cell to be:
Church Name
Image
Pastor Name
我可以轻松地将所有数据插入表中,但我无法将其显示为3行。
I can easily insert all data into a table, but I cannot get it to display as 3 rows.
echo("<table>");
while($row = mysql_fetch_array($rs))
{
echo("<tr>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("</tr>");
}
echo("</table>");
这样做会导致我有3行结构正确,但数据重复。我知道我没有更改ID,但我不确定我应该做什么
Doing this causes me to have 3 rows in correct structure, but having duplicate data. I understand that I have not changed the id, but am not sure what I should do
推荐答案
你正在重复数据这一排。如果要每行显示3个项目,则需要添加计数器和语句来绘制表格行中断,如下所示:
You're repeating the data on the row. If you want to show 3 items per row, you need to add a counter and a statement to draw the table row breaks, as below:
$int_Col = 1;
echo("<table>");
while($row = mysql_fetch_array($rs))
{
if ($int_Col == 1) {
echo("<tr>");
}
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
if ($int_Col == 3) {
echo("</tr>");
$int_Col = 1;
} else {
$int_Col++;
}
}
if ($int_Col > 1) {
echo("<td colspan='". (3 - $int_Col) ."'> </td></tr>");
}
echo("</table>");
最后一次检查($ int_Col> 1)应确保使用空单元格正确呈现表格 - 应该跨越未在当前行上绘制的正确数量的单元格。
The last check ($int_Col > 1) should ensure the table is rendered properly with an empty cell - should span the correct amount of cells that were not drawn on the current row.
这篇关于PHP表由从SQL数据库加载的3行组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!