[英]PHP: Function to plot any query result in a table
I'm relatively new to using PHP and have been working on using it to fetch data from a database. 我对使用PHP相对较新,并且一直致力于使用它来从数据库中获取数据。 I have been able to plot queries in tables quite easily when I know the number of columns that need to be plotted, like below: 当我知道需要绘制的列数时,我能够很容易地在表格中绘制查询,如下所示:
echo "<table border='1'>";
echo "<tr><td align='center' colspan='6'><b>All ".$catDesc." CD's</b></td></tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr bgcolor='#fff'>
<td>".$row['catDesc']."</td>
<td>".$row['CDTitle']."</td>
<td>".$row['CDYear']."</td>
<td>".$row['artistName']."</td>
<td>£".$row['CDPrice']."</td>
<td>".$row['PubName'].", ".$row['location']."</td>
</tr>";
}
echo "</table>";
I was wondering if there is any way of writing some code in a separate function that would plot the result of any query that it was given regardless of the number of field/ columns the query result held. 我想知道是否有任何方法可以在单独的函数中编写一些代码,无论查询结果保存的字段/列的数量如何,都会绘制给出的任何查询的结果。
Thank you for any advice or code you'd like to share. 感谢您提供任何您想要分享的建议或代码。
Jonny 强尼
Within the while-loop you could iterate over the $row-variable and create a new td-element each time: 在while循环中,您可以迭代$ row-variable并每次创建一个新的td元素:
while($row = mysql_fetch_assoc($result))
{
echo "<tr bgcolor='#fff'>";
foreach($row AS $key => $value)
{
echo "<td>$value</td>";
}
echo "</tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.