Apply Table Style in Dynamic Table Creation with PHP - php

I am having trouble applying CSS styles to my dynamic table. I am connecting to a database and I know the data is populated correctly and displaying. My issue is that there are 900 records and I want to limit the table size, utilizing a scrollbar. I have read elseware that the proper CSS style nodes to accomplish this are: How to specify table's height such that a vertical scroll bar appears?
overflow: auto;
height: 700px;
display: block;
overflow-y: scroll;
At first I attempted this with inline styling (a no-no.. I realize), but it didn't work. I have read about adding a 'class' to the table and/or individual rows, which would then be reflected in my CSS style sheet, but I can't seem to figure out how to accomplish this. I get syntax errors when I add 'span' or 'class' tag designators to the PHP (I imagine from utilizing 'ECHO' - which both require double quotes).
Good example of what I'm trying to accomplish: http://www.timrivera.com/tests/csstables.html#markupIE
The PHP code snippet below has good syntax, but I don't know where to add the class or span designators appropriately. One thing to note - I need to have different styles for different tables, so changing the global 'table' CSS isn't going to work.
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";

I think the best way would be to wrap a div around the table and give the div a height.
http://phpfiddle.org/main/code/i7h-9b1
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>
<div id="scroll">
table
</div>
Using your code:
echo '
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>';
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div id='scroll'><table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table></div>";

Edit your PHP code to...
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div class=\"table-container\">";
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
Then just style using
div.table-container table {}

Using Calum's style format you could do this:
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<style>#size{height:100px;width:340px;overflow-y:scroll;}</style>";
echo "<table id='size' border='1'>
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
I tested it and works fine. And for the different table styles you could:
<style>
#table1
{
style code here
}
#table2
{
style code here
}
</style>
and so on... then you could apply them to the tables:
<table id="table1">
...
</table>
<table id="table2">
...
</table>

Related

PHP MYSQL display table with action row clickable to open specific file

I am having this type of problem. I have a table displayed on my webpage. Wherein I have an Action column. I am trying to make my (a href) code open different files depending on what was indicated on my table whether it is pdf or an excel file.
<?php
$con=mysqli_connect("localhost","root","","annualdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM aviation_report");
echo "<head><style>
table {
border: 2px solid black;
width: 100%;
height: 100px;
text-align: center;
}
</style></head>";
echo "<table>
<tr>
<th>Agency</th>
<th>FileName</th>
<th>FileType</th>
<th>Date</th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['agency'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "<td>" . $row['filetype'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='sample.pdf'>OPEN FILE</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You would have this code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['agency'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "<td>" . $row['filetype'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a target='_blank' href='" . $row['filename'] . "'>OPEN FILE</a></td>";
echo "</tr>";
}

Highlight or color the values of a specific row in table fetched using PHP and Mysql

I am a beginner to PHP. I am looking to highlight or color the values of first and last row in a table fetched using PHP and Mysql.
Below is the code.
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
while($row = mysql_fetch_array($result_trend))
{
echo "<tr>";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>
I am not really sure how to proceed further. Any suggestion is much appreciated. Thanks in advance.
You can define a CSS style for the first and last rows as below:
.my-table tbody tr:first-child {
background-color: yellow;
}
<table class="my-table">
<thead>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>row</td>
</tr>
<tr>
<td>Third</td>
<td>row</td>
</tr>
</tbody>
</table>
You can simply obtain number of results and then count iterations. And if this is 1. row or last, add the style you need or css class that you'll style in your css file. Like this:
<?php
$result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets,
Backlogged, Resolution_Rate, Transactions from table");
$rows_count = mysql_num_rows($result_trend);
$i = 0;
while($row = mysql_fetch_array($result_trend))
{
$i++;
$style = "";
if($i == 1 || $i == $rows_count) {
// $style .= "bgcolor=\"#FF0000\"" //for inline style, bad practise
$style .= "class=\"colorful\""
}
echo "<tr " . $style . ">";
echo "<td>" . $row['Week'] . "</td>";
echo "<td>" . $row['New_Tickets'] . "</td>";
echo "<td>" . $row['Closed_Tickets'] . "</td>";
echo "<td>" . $row['Backlogged'] . "</td>";
echo "<td>" . $row['Resolution_Rate'] . "</td>";
echo "<td>" . $row['Transactions'] . "</td>";
}
echo "</tr>";
?>

echo <tr> with a href inside not working

I'm pretty confused about the fact that I have this code:
while($row = mysql_fetch_array($result)) {
echo "<tr href='http://google.com'>";
echo "<td>" . $row['rowname'] . "</td>";
echo "<td>" . $row['rowname2'] . "</td>";
echo "<td>" . $row['rowname3'] . "</td>";
echo "<td>" . $row['rowname4'] . "</td>";
echo "</tr>";
}
and for some reason my link doesn't work. I've tried putting a tag around the tr but with no success. Somebody to have a clue?
Guess you are looking for link to entire row. This can be achieved by having onClick function on the row, instead of using href.
<tr onClick="location.href='target url'">
<td></td>
<td></td>
</tr>
while($row = mysql_fetch_array($result)) {
echo "<tr onclick='window.location.href = \"http://google.com\";'>";
echo "<td>" . $row['rowname'] . "</td>";
echo "<td>" . $row['rowname2'] . "</td>";
echo "<td>" . $row['rowname3'] . "</td>";
echo "<td>" . $row['rowname4'] . "</td>";
echo "</tr>";
}
If you want to make entire row clickable and work like an a tag you should use javascript for this:
echo "<tr onclick='location.href = \"http://google.com\"'>";
And then it would be nice to add CSS cursor pointer to clickable table cells:
tr td {
cursor: pointer;
}
a tr-tag has no href attribute, see here.
you can do something like:
<tr>
<td>
Google
</td>
....
</tr>
Another Solution would be adding javascript if the whole row should be clickable:
<tr onclick="window.location.href = google.com">
<td>
<a href="google.com"</a>
</td>
....
</tr>

PHP, MYSQL, HTML table with tablesorter

Trying to add tablesorter added to a page I am creating. I know very little of jquery, so I'm guessing that's where my fault is. I've added the required code in the <head> area of my page, and made the necessary changes to my table. My table still renders as it would with just HTML. Ideas?
<html>
<head>
<title>Inventory</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://tablesorter.com/__jquery.tablesorter.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ $("table").tablesorter(); });
</script>
</head>
<body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT
products.name,
products.sku,
inventory.quantityfry,
inventory.quantityjuv,
inventory.quantityadult,
inventory.notes,
inventory.location,
inventory.owner
FROM
products
INNER JOIN
inventory
ON
products.sku=inventory.sku";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
echo "<table border='1' id='table' class='tablesorter'>
<thead>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>
</thead>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
echo "</tbody>";
}
mysqli_free_result($result);
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Thanks!
Three things:
Don't link directly to tablesorter at tablesorter.com - make a copy to your own server, or use a copy at a CDN (this is of my fork of tablesorter at cdnjs.com).
Include a <!DOCTYPE html> at the top of your HTML otherwise IE will change into quirks mode and pretty much make your site look bad.
As #MikeB mentioned, the above code wraps every row in a tbody, correct the code as follows (this is just a snippet):
echo "<table border='1' id='table' class='tablesorter'>
<thead>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>
</thead><tbody>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
echo "</tbody></table>";

Highlighting a cell PHP a certain color based on MYSQL value

I have three values that can be displayed from a table/column in mysql, RED, GREEN, YELLOW for the field "ProspectStatus"
Is there anyway I can make a cell change its background color based on the value?
e.g.
echo "<td>" . $row['ProspectStatus'] . "</td>";
PHP Code:
$result = mysql_query("SELECT * FROM customerdetails");
//List the Columns for the Report
echo "<table border='1'>
<tr>
<th>CustomerID</th>
<th>Customer Name</th>
<th>Prospect Status</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
You can do this with this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
if($row['ProspectStatus']=='[val1]') // [val1] can be 'approved'
echo "<td style='background-color: #00FF00;'>".$row['ProspectStatus']."</td>";
else if($row['ProspectStatus']=='[val2]')// [val2]can be 'rejected'
echo "<td style='background-color: #FF0000;'>".$row['ProspectStatus']."</td>";
else if($row['ProspectStatus']=='[val3]') //[val3] can be 'on hold'
echo "<td style='background-color: #FFFF00;'>".$row['ProspectStatus']."</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
The value of the status may depend on the color. I assume that val1, val2 and val3 are the values of the 3 colors.
First, use CSS for this, not messy in-line style definitions
The most maintainable option would probably separate the color codes themselves from the view logic, so in CSS, create some new classes:
td.done { background-color: green; }
td.working { background-color: yellow; }
td.stopped { background-color: red; }
Then, in your loop just do this (you don't need the echo statements, anything outside of <? and ?> tags will be parsed as HTML rather than PHP):
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
....
<td class='<?= $row['ProspectStatus']; ?>'><?= $row['ProspectStatus']; ?></td>
....
</tr>
<?php endwhile; ?>
This way, if ProspectStatus is 'done', the cell will have a green background, if it's 'working', it will have a yellow background and if it's stopped it will have a red background.
If ProspectStatus is a range, or otherwise not this simple
Let's say ProspectStatus doesn't have done, working and stopped or similar simple values, and instead it's a range, for this you can use a simple nested ternary operator to get the color you want instead of <tr class='<?= $row['ProspectStatus']; ?>'>:
<tr class='<?php echo ($row['ProspectStatus'] >= 80) ? "done" : (($row['ProspectStatus'] >= 40) ? "working" : "stopped"); ?>'>
With this one liner, if ProspectStatus is greater than 80, the field will be green, if it's between 40 and 80 it will be yellow and if it's below 40 it will be red.
Note: mysql_ functions are DEPRECATED
By using the mysql_ features, you run the risk of your program not working in future versions of PHP as those functions have been formally deprecated as of 5.5.
The new recommended way to perform your query is this:
$mysqli = new mysqli("username","password","hostname","password");
$result = $mysqli->query("SELECT * FROM customerdetails");
while($row = $result->fetch_assoc())...
Hope this helps.
Create a class of the color you want for that cell and then make an if statement that will check the value. Depending on the value, echo that class.
yes you can add it to you Table data tag like below
The following code will work:
while($row = mysql_fetch_array($result))
{
echo "<tr style='background-color: ". $row['ProspectStatus'] ."'>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "<tr style='background-color: ". $row['ProspectStatus'] ."'>";
or make an if statement checking what color || then displaying the echo
That should work.
Since you said you want to change the color of only one particular cell, this should do the trick:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>";
echo "<td>" . $row['CustomerName'] . "</td>";
echo "<td style='background-color: ". $row['ProspectStatus'] ."'>" . $row['ProspectStatus'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}

Categories