[英]Printing an array in php from mysql
Just trying to do a tutorial from a few weeks ago and run into a bit of trouble. 仅仅从几周前开始尝试做一个教程就遇到了麻烦。
I'm trying to retrieve all the data from an array inside of mysql. 我正在尝试从mysql内部的数组中检索所有数据。 Here's my code so far, which kinda works.
到目前为止,这是我的代码,可以工作。
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
while ($row = mysqli_fetch_array($result) )
{
printf("%s %s %d %s\n", $row[0], $row[1],$row[2], $row[3]);
}
The table is meant to look like this 该表应该看起来像这样
While the code does work (it prints out every value), How do I organize it so it looks like that in the picture? 虽然代码可以正常工作(它会打印出每个值),但如何组织它,使其看起来像图片中的那样? I'm guessing I need to print into a table somehow?
我猜我需要以某种方式打印到表中?
Googling hasn't really gotten me anywhere. 谷歌搜索并没有真正吸引我。 Only really shown be how to print one line of the array.
仅真正显示出如何打印阵列的一行。
You could do it in a table by looping your results inside the <tr>s
您可以在表中通过将结果循环到
<tr>s
<?php
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
?>
<h1>Search Product Details</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($result) )
{
?>
<tr>
<?php
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
?>
</tr>
<?php
}
?>
</tbody>
</table>
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Price</th>";
echo "<th>Description</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}
echo "</table>";
Design your table with CSS 使用CSS设计表格
<?php
include('db_conn.php'); //db connection
$query = "SELECT * FROM kit202_product";
$result = $mysqli->query($query);
?>
<TABLE BORDER="5" WIDTH="50%" CELLPADDING="4" CELLSPACING="3">
<TR>
<TH COLSPAN="2"><BR><H3>Search Product Detail</H3>
</TH>
</TR>
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>Price</TH>
<TH>Description</TH>
</TR>
<?php
while ($row = mysqli_fetch_array($result) )
{
echo "<TR ALIGN='CENTER'>
<TD>$row[0]</TD>
<TD>$row[1]</TD>
<TD>$row[2]</TD>
<TD>$row[3]</TD>
</TR>";
}
?>
</TABLE>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.