I am trying to create very simple table with data from database in it. The table has two rows with three pieces of data in each (three columns).. I was playing around for couple hours now but can't get my head round it..I understand that $row['BandName'] needs to be indexed and increased each time the loop get round, but don't know how to attach index to it. I tried something like this: $row['BandName'][$i]
but it displayed only one letter of full name (obviously it's not right) Any help would be appreciated..
The table looks like this :
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";}
}
?>
Table structure is
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>
you want your database data to a table?:
<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
}
?>
Finally done. and tested
<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>";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.