Php 在表中显示查询结果
我有一个MySQL查询,返回结果超过50个。现在我需要在一个有3行3列的表中显示结果 大概是这样的:Php 在表中显示查询结果,php,mysql,Php,Mysql,我有一个MySQL查询,返回结果超过50个。现在我需要在一个有3行3列的表中显示结果 大概是这样的: <table> <tr> <td>Content</td> <td>Content</td> <td>Content</td>
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
我用PHP做了如下尝试:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
然后我在我的表中打印内容数组,如下所示:
<table>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
//Create new output
$output = "<p>$name</p>";
$output .= "<p>$address</p>";
$output .= "<p>$content</p>";
//Add output to array
$mycontent[] = $output;
}
<tr>
<td><?php echo $mycontent[0]; ?></td>
<td><?php echo $mycontent[1]; ?></td>
<td><?php echo $mycontent[2]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[3]; ?></td>
<td><?php echo $mycontent[4]; ?></td>
<td><?php echo $mycontent[5]; ?></td>
</tr>
<tr>
<td><?php echo $mycontent[6]; ?></td>
<td><?php echo $mycontent[7]; ?></td>
<td><?php echo $mycontent[8]; ?></td>
</tr>
使用此代码,我只能显示9个内容。我的问题是我想显示更多的内容。我将使用它来显示内容;比如0-9,10-18,19-27等等
注意:我可以做分页部分
我希望有人能给我正确的方向
多谢各位 试试以下方法:
echo "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
}
echo "</table>";
要获取TD中的每个内容、名称、地址,以及TR中MyContent、name、address的3个,请尝试以下操作:
$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";
尝试以下方法:
echo "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
}
echo "</table>";
要获取TD中的每个内容、名称、地址,以及TR中MyContent、name、address的3个,请尝试以下操作:
$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";
使用for循环迭代$mycontent数组:
编辑:我意识到$mycontent的构建方式与我最初设想的不同:
$h = "<table>"; // we are going to build the table in $h
$maxcount = count($mycontent); // get the number of values within the array
for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
$h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
}
$h.= "</table>"; // close the table
echo $h; // echo it
-->实时演示:使用for循环迭代$mycontent数组:
编辑:我意识到$mycontent的构建方式与我最初设想的不同:
$h = "<table>"; // we are going to build the table in $h
$maxcount = count($mycontent); // get the number of values within the array
for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...
$h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row
}
$h.= "</table>"; // close the table
echo $h; // echo it
-->现场演示:如果您计划在函数中使用变量,您可以将其存储在变量中,也可以打印它。不管怎样
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
$str = "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$str .= "<tr>";
$str .= "<td>" . $row['name'] . "</td>";
$str .= "<td>" . $row['address'] . "</td>";
$str .= "<td>" . $row['content'] . "</td>";
$str .= "</tr>";
}
$str .= "</table>"
echo $str;
好了 如果您计划在函数中使用变量,可以将其存储在变量中,也可以直接打印它。不管怎样
$q = "SELECT name, address, content FROM mytable";
$r = mysqli_query($dbc, $q);
$str = "<table>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$str .= "<tr>";
$str .= "<td>" . $row['name'] . "</td>";
$str .= "<td>" . $row['address'] . "</td>";
$str .= "<td>" . $row['content'] . "</td>";
$str .= "</tr>";
}
$str .= "</table>"
echo $str;
好了 试试这样的东西。它将值放置在易于使用的关联数组中。然后你只需循环通过
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$values[] = array(
'name' => $row['name'],
'address' => $row['address'],
'content' => $row['content']
);
}
?>
<table>
<?php
foreach($values as $v){
echo '
<tr>
<td>'.$v['name'].'</td>
<td>'.$v['address'].'</td>
<td>'.$v['content'].'</td>
</tr>
';
}
?>
</table>
试试这样的。它将值放置在易于使用的关联数组中。然后你只需循环通过
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$values[] = array(
'name' => $row['name'],
'address' => $row['address'],
'content' => $row['content']
);
}
?>
<table>
<?php
foreach($values as $v){
echo '
<tr>
<td>'.$v['name'].'</td>
<td>'.$v['address'].'</td>
<td>'.$v['content'].'</td>
</tr>
';
}
?>
</table>
我对你的代码有点困惑。你能解释一下吗?当你可以使用while迭代结果时,为什么要使用for循环?这行不通,因为你错过了他是如何构建$mycontent的。。。它不是一个对象数组。。。它是一个文本字符串。i、 e.$mycontent[0]=John Doe101 Lost st什么东西@AarolamaBluenk:我想继续使用$mycontent。当然,您可以在一段时间内编写表。使用您编辑的代码,我的输出是1行和1个td。这不是我期望的结果我对你的代码有点困惑。你能解释一下吗?当你可以使用while迭代结果时,为什么要使用for循环?这行不通,因为你错过了他是如何构建$mycontent的。。。它不是一个对象数组。。。它是一个文本字符串。i、 e.$mycontent[0]=John Doe101 Lost st什么东西@AarolamaBluenk:我想继续使用$mycontent。当然,您可以在一段时间内编写表。使用您编辑的代码,我的输出是1行和1个td。这不是我期望的结果,请调用这段代码。它将为您创建整个表。名称、地址和内容应放在一个td中,而不是放在3个td中。好的,您可以自己调整它。。发挥创造力。。这一点非常简单。你不应该再需要帮助了。只要调用这段代码就行了。它将为您创建整个表。名称、地址和内容应放在一个td中,而不是放在3个td中。好的,您可以自己调整它。。发挥创造力。。这一点非常简单。你不应该再需要帮助了。姓名、地址和内容应该放在一个td中,而不是放在3个td中。你的意思是你在表格中是这样的:每个客户都在,每个客户都有3个?@TNK我编辑了我的答案,将姓名、地址和内容放在1个td中,地址和内容应该放在一个td而不是3个td中。你是说你在表格中是这样的:每个客户在,每个客户在3个?@TNK i编辑我的答案以获得姓名,地址和内容在1 TD请考虑接受一个答案点击蜱标记左边如果它实际上回答了你的问题请考虑接受答案点击蜱标记左边如果它实际上回答了你的问题