echo <tr> with a href inside not working - php

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>

Related

Format table row based on value

Im would like to style a table row based on the value in the first cell.
order_status has 3 options. 'Done' 'Optie' 'Def'
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['order_status'] . "</td>";
echo "<td>" . $row['planning'] . "</td>";
echo "<td>" . $row['order_name'] . "</td>";
echo "<td>" . $row['order_number'] . "</td>";
echo "<td>" . $row['order_pl'] . "</td>";
echo "<td>" . $row['order_date_in'] . "</td>";
echo "<td>" . $row['order_date_out'] . "</td>";
echo "<td>" . $row['order_location'] . "</td>";
echo "<td>" . $row['order_customer'] . "</td>";
echo "<td>" . $row['order_contact'] . "</td>";
echo "<td>" . $row['order_content'] . "</td>";
echo "</tr>";
}
echo "</table>";
If you add a class to the row you can easily do this by updating your CSS file.
PHP
Update your PHP to look like this:
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr class=\"order_status_{$row['order_status']}\">
<td> {$row['order_status']} </td>
<td> {$row['planning']} </td>
<td> {$row['order_name']} </td>
<td> {$row['order_number']} </td>
<td> {$row['order_pl']} </td>
<td> {$row['order_date_in']} </td>
<td> {$row['order_date_out']} </td>
<td> {$row['order_location']} </td>
<td> {$row['order_customer']} </td>
<td> {$row['order_contact']} </td>
<td> {$row['order_content']} </td>
</tr> ";
}
echo "</table>";
The following line...
class=\"order_status_{$row['order_status']}\"#
Will give class names in the format of order_status_*; for example:
order_Status_confirmed
order_Status_received
CSS
Then you can add something similar to the following to your CSS and control exactly how it looks.
.order_status_pending{
background: #faa;
}
.order_status_confirmed{
background: #afa;
}

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>";
?>

embedded PHP preventing scrolling

so I am using an HTML template, and have a dynamic table generated by PHP. For whatever reason, when I add the php table in, I can no longer scroll on the page. I am wondering if there is some work around, or there is something I should be looking for in the template CSS to fix this.
Here is my HTML with the embedded PHP:
<div class="block-head">
<h2>Datatable All Features</h2>
</div>
<div class="block-content np">
<table cellpadding="0" cellspacing="0" width="100%" class="table table-bordered table-striped sortable">
<thead>
<tr>
<th><input type="checkbox" class="checkall"/></th>
<th width="25%">Name</th>
<th width="25%">Gender</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row[username] . "</td>";
echo "<td>" . $row[gender] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
Here is my query, which I have above my HTML, for whatever it's worth. I know it's deprecated (just not a top priority right now):
$query = mysql_query("SELECT * FROM markerfollowing WHERE markerID = '$markerid'"); //query
When I remove the php in the table, it scrolls fine. When I put it back, no dice.
Sincere thanks for any help!
Here is my solution:
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . '<input type="checkbox" name="checkbox"/>' . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
?>
It's probably an error 500 which you cannot see because of your PHP.ini configuration.
change the $row[username] to $row['username'] and do the same with the gender.
This should do the job:
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
?>

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>";
}

Apply Table Style in Dynamic Table Creation with 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>

Categories