我正在尝试使用数据库中的数据创建非常简单的表。该表有两行,每行有三个数据(三列)。我现在玩了几个小时但是不能让我的头围绕它...我明白$ row ['BandName']需要是每次循环循环时索引和增加,但不知道如何将索引附加到它。我试过这样的事情:$row['BandName'][$i]
但它只显示了一个全名字母(显然它不对)任何帮助都会受到赞赏..
表格如下:
include 'db_constants.php';
$conn = sqlsrv_connect( $host, $cnct);
if( $conn == false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
else
$tsql = "SELECT * FROM bands";
$stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
else
{
$recordcount = sqlsrv_num_rows( $stmt );//count the records
if($recordcount >0)
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{ ?>
<TR>
<TABLE border="3" cellspacing="5" cellpadding="5">
<?php for($i=0; $i<3; $i++) {?>
<TD align="center" valign="top">
<table border="0" cellspacing="2" cellpadding="0">
<tr align="center">
<td valign="bottom"><a href="details.php" target="_self"><img src="band/1.jpg" width="140" height="110" border="0"></a></td>
</tr>
<tr align="center">
<td class="greytablesm"> <?php echo $row['BandName'];?></td>
</tr>
</table>
</TD>
<?php }?>
<TR>
<?php }
else {echo "not found";}
}
?>
表结构是
BandID - int autoincrement
BandName - nvarchar(50)
BandBio - nvarchar(max)
Date - date
START TABLE <table>
loop from 1 to 2
<tr>
loop from 1 to 2
<td>
Show Details
</td>
end loop
</tr>
end loop
END TABLE </table>
答案 0 :(得分:3)
您希望将数据库数据添加到表中吗?:
<TABLE border="3" cellspacing="5" cellpadding="5">
<?php
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) //loop through every result set
{ ?>
<TR>
<?php foreach($row as $item) { //loop trough every cell in the row ?>
<TD><?= $item ?> </TD>
<?php } ?>
<TD><?php //you can add none column spefic data here like actions(links) or images ?> </TD>
<TR>
<?php
}
?>
答案 1 :(得分:1)
终于完成了。并经过测试
<table border="3" cellspacing="5" cellpadding="5">
<?php
$sql = "select * from deals";
$res = mysql_query($sql);
$num_rows = mysql_num_rows($res);
if($num_rows > 0)
{
$i = 1;
echo "<tr>";
while($row = mysql_fetch_object($res))
{
?>
<td><?php echo $row->deal_name ?> </td>
<?php
if( $i++%3 == 0 )
echo "</tr><tr>";
}
echo "</tr>";
}
?>