I have finally figured out how to make my SQL table display using PHP however I am having a hell of a time getting the styles to show up. Here's what I have so far, any pointers on how to style the echo? Nothing shows up. Is how I am doing this the improper way to style an echo? I am trying to style it with Bootstrap CSS.
<html>
<head>
<title>Abacus PHP SQL Server Connection</title>
</head>
<body>
<?php
$db_host = '***';
$db_user = '***'; //recommend using a lower privileged user
$db_pwd = '***';
$database = '***';
$table = '***';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT category, data_desc, data_source, update_cycle,ISNULL(datename(mm,last_abadata_update) + + ' ' + DATENAME(dd,last_abadata_update) + ', ' + DATENAME(yyyy, last_abadata_update),'') AS last_abadata_update, ISNULL(datename(mm,last_abadata2_update) + ' ' + DATENAME(dd,last_abadata2_update) + ', ' + DATENAME(yyyy,last_abadata2_update),'') AS last_abadata2_update FROM {$table} ORDER BY category, data_source, data_desc";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
echo "<table >";
echo "<tr>";
// printing table headers with desired column names
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Category";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Description";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Source";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Update Cycle";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Last AbaData Update";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Last AbaData 2.0 Update";
echo "</td>";
echo "</tr>";
while($row = sqlsrv_fetch_array($result))
{
echo "<tr>";
echo "<td style='border=1px solid black'>";
echo $row['category'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['data_desc'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['data_source'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['update_cycle'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['last_abadata_update'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['last_abadata2_update'];
echo "</td>";
echo "</tr>\n";
}
echo "</table>";
sqlsrv_free_stmt( $result);
sqlsrv_close( $conn);
?>
</body>
</html>
CSS accepts : not = so changestyle='border=1px solid black' to style='border:1px solid black'.
Your CSS should not be used with Camel Case, try using lowercase styles.
Also you should be using colons not equals for the styles.
"<td style='border:1px solid black; font-size:18; font-weight:bold'>";
use css like this
<style>
.outside-while{
border:1px solid black;font-size:18;font-weight:bold;
}
.inside-while{
border:1px solid black;
}
</style>
And for all td outside the while loop give class as outside-while
Nd for td inside the loop, give class as inside-while
example
echo "<td class='outside-while'>";
echo "Category";
echo "</td>";
And i feel outside it should be <th> not <td>
Now inside while
echo "<td class='inside-while'>";
echo $row['category'];
echo "</td>";
I don't like when you styling like this html. Use CSS to style. Add id or class to elements. I even don't echo html elements but instead opening and closing php tags and writing elements in plane html and only when i need to present php var I use php. Someone will say maybe its not good but this is how I do.
Here is how i would do your code:
<html><head><title>Abacus PHP SQL Server Connection</title></head><body>
<?php
$db_host = '***';
$db_user = '***'; //recommend using a lower privileged user
$db_pwd = '***';
$database = '***';
$table = '***';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT category, data_desc, data_source, update_cycle,ISNULL(datename(mm,last_abadata_update) + + ' ' + DATENAME(dd,last_abadata_update) + ', ' + DATENAME(yyyy, last_abadata_update),'') AS last_abadata_update, ISNULL(datename(mm,last_abadata2_update) + ' ' + DATENAME(dd,last_abadata2_update) + ', ' + DATENAME(yyyy,last_abadata2_update),'') AS last_abadata2_update FROM {$table} ORDER BY category, data_source, data_desc";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
?>
<table>
<tr>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Category
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Description
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Source
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Update Cycle
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Last AbaData Update
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Last AbaData 2.0 Update
</td>
</tr>
<?php
while($row = sqlsrv_fetch_array($result))
{
?>
<tr>
<td style="border:1px solid black">
<?php
echo $row['category'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['data_desc'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['data_source'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['update_cycle'];
?>
</td>
<td tyle="border:1px solid black">
<?php
echo $row['last_abadata_update'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['last_abadata2_update'];
?>
</td>
</tr>
<?php
}
?>
</table>
sqlsrv_free_stmt( $result);
sqlsrv_close( $conn);
?>
</body></html>
Related
I've seen similar questions on here but the code is different when I try to implement it. I'm using php and html to connect to my db2 database, pull data from a table and display the pulled data in an html table. I'm trying to add functionality to my 'delete' button and would like to be able to mark a check-box that would highlight the selected row in the table. I'm working on my delete.php but for now I would like to implement this row selection. I found a line of code that I put at the start of my table, but how do I highlight the row? Here's what I have so far:
<html>
<head><title>DB Testing</title></head>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>
You'll need some javascript to do the trick, see the snippet:
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td><input type="checkbox" name="checkbox" value="1" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="2" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="3" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
EDIT
<html>
<head>
<title>DB Testing</title>
<script type="text/javascript">
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
</script>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" onClick=\"hl(this)\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>
I want to create a filter which filters my ouput from the database. I have no idea how to create this. I have searched some other topics but they were not so helpful, hopefully someone can help me out.
I have the following code which retrieves the information from my database and ouputs it into a table. (please ignore the table set-up and such, still need to clean the nesting and stuff..)
Code (retrieve.php):
<?php
// Connect to database server
mysql_connect("xx", "xx", "xx") or die (mysql_error ());
// Select database
mysql_select_db("xx") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
//OUDE LOOP SPACE
// Write the value of the column FirstName (which is now in the array $row)
echo "<table width='100%' border='0' cellpadding='10' cellspacing='0'><tr style='background-color:#f04d44; color:#ffffff;'>";
echo "<td style='border-right:1px solid #ff9a88;'><strong>Klant</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Datum</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Eventviewer Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Services Opmerkingen</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Nagekeken</strong></td><td style='border-right:1px solid #ff9a88;'><strong>Backup Opmerkingen</strong></td><td><strong>Check Gedaan Door</strong></td>";
echo "</tr>";
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
echo "<tr style='background-color:#ffffff;'>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['klant'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['datum'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['eventviewer_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['services_opmerkingen'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_nagekeken'];
echo "</td>";
echo "<td style='border-right:1px solid #cccccc; border-bottom:1px solid #cccccc;'>";
echo $row['backup_opmerkingen'];
echo "</td>";
echo "<td style='border-bottom:1px solid #cccccc;'>";
echo $row['check_door'];
echo "</td>";
}
echo "</tr></table>";
// Close the database connection
mysql_close();
?>
Thanks for helping me out!
For filtering, most of the time you can use your database.
Right now your SQL statement is like this:
$strSQL = "SELECT * FROM informatie ORDER BY id DESC;";
To filter on information from the table informatie, you should alter the SQL statment, for example:
$strSQL = "SELECT * FROM informatie WHERE `eventviewer_nagekeken` = 1 ORDER BY id DESC;";
This will return all rows where eventviewer_nagekeken equals 1. This way you will be able to create various outputs from the pool of data you have in the informatie table.
For documentation and examples, see the docs.
you can use where clause generated dynamically as bellow
$where = ' 1=1 ';
if(isset($_REQUEST['param1']))
{
$where .= " and db_field1 = ".mysql_real_escape_string($_REQUEST['param1']);
}
if(isset($_REQUEST['param2']))
{
$where .= " and db_field2 = ".mysql_real_escape_string($_REQUEST['param2']);
}
$strSQL = "SELECT * FROM informatie ".$where." ORDER BY id DESC;";
here mysql_real_escape_string will avoid mysql injection.
Hope this helps you.
The code below is my way on how to print my data in Table.
<style media="screen">
.noPrint{ display: block; }
.yesPrint{ display: block !important; }
</style>
<style media="print">
.noPrint{ display: none; }
.yesPrint{ display: block !important; }
</style>
Here is my code of printing the data from the table
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_Close($con);
?>
</div>
</div>
</div>
To initiate the command
<input TYPE="button" value = "Print Report" onClick="window.print()">
Further Explanation: My code here is to print the data from my table and the table has been populated from my database. Data Transfer to Table then Print but what happens is the whole website has been print. It seems that my code Capture the site as Image and this what it print.
What I want to do is print what only on the table, how can achieve it? TY
try this,
you r using mysql_Close($con); but in real mysql_close($con);
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
</div>
</div>
With the below javascript function, you can print any element by just placing the element in the parentheses.
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />
Here is the entire code:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
mywindow.close();
return true;
}
</script>
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;" class="noPrint">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
?>
<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>
<?php
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
?>
</table>
<?php mysql_close($con); ?>
</div>
</div>
</div>
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />
Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.
This is my table
<?php
$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");
echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th>
<th width='5%'></th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "...";
}
echo"</table>";
?>
This is how it looks like
Any help will be appreciated.
I'm doing this out of my head so forgive me any format issues and such...
Move the string length check to just under the while.
Overwrite $row['bookname'] instead of creating $str.
Remove the line with:
echo '<td style="max-height: 10px;">' . $str . '</td>';
Result:
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "...";
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
I am trying to display the data output in to specific div within a table.
I am able to get the table setup and display the first row of the data within the table, but the rest of the output does not make it inside the table.
Anyone please can point out what am I doing wrong?
Thank you!
<?php include('header.php'); ?>
<?php
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
?>
<?php
while($row = mysqli_fetch_array($result))
{?>
<div class="userlistoutput">
<?php
echo "<td width=120px>" . $row['firstname'] . "</td>";
echo "<td width=120px>" . $row['lastname'] . "</td>";
echo "<td width=120px>" . $row['location'] . "</td>";
echo "</tr>";
?>
</div>
<? echo "</table>" ?>
<?php
}?>
</div>
<?php
mysqli_close ($con);
?>
And this is the CSS for userlist:
#userlist {
list-style:none;
width: auto;
margin:30px auto 1px auto;
height:100%;
padding:0px 20px 0px 20px;
/* Rounded Corners */
border-radius: 10px;
/* Background color and gradients */
background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB); */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
/* Borders */
border: 1px solid #002232;
}
That is happening because you have put the </table> inside the while statement. Try changing it to:
<?php include('header.php');
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr class='userlistoutput'>;
echo "<td width='120px'>" . $row['firstname'] . "</td>";
echo "<td width='120px'>" . $row['lastname'] . "</td>";
echo "<td width='120px'>" . $row['location'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close ($con);
?>