How to follow the condition to underline in the table? - php

I have a question how to underline in the table according the column data. Below is example coding to explain what I am facing the problem:
I want to detect if column underline is 1 the first name data will draw the underline, if 0 the first name data no show the underline. Below the sample is hardcode, if real situation, I have too many row to show the data, I cannot 1 by 1 to add text-decoration: underline; in the td. So that, hope someone can guide me how to solve this problem. I am using the php code to make the variable to define the underline.
<!--Below the php code I just write the logic, because I don't know how to write to detect the column underline value-->
<?php
if ( <th>Underline</th> == 1) {
$add_underline = "text-decoration: underline;";
}
if ( <th>Underline</th> == 0) {
$add_underline = "text-decoration: underline;";
}
?>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td style="<?php echo $add_underline;?> ">Jill</td>
<td>Smith</td>
<td>1</td>
</tr>
<tr>
<td style="<?php echo $add_underline;?>">Eve</td>
<td>Jackson</td>
<td>0</td>
</tr>
<tr>
<td style="<?php echo $add_underline;?>">John</td>
<td>Doe</td>
<td>1</td>
</tr>
</table>
My output like below the picture:
My expected result like below the picture, Jill and John can underline:

Why not use javascript to achieve this? No matter what the server sends it will evaluate the condition if 1 is set and then underline accordingly... You would have to use classes to get the appropriate table data tags holding the values, I added class='name' to the names <td> tag and class='underline' tot he underline <td> tag.
// get the values of the elements with a class of 'name'
let names = document.getElementsByClassName('name');
// get the values of the elements with a class of 'underline'
let underline = document.getElementsByClassName('underline');
// loop over elements using for and use the keys to get and set values
// `i` will iterate until it reaches the length of the list of elements with class of underline
for(let i = 0; i < underline.length; i++){
// use the key to get the text content and check if 1 is set use Number to change string to number for strict evaluation
if(Number(underline[i].textContent) === 1){
// set values set to 1 to underline in css style
names[i].style.textDecoration = "underline";
}
}
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td class="name">Jill</td>
<td>Smith</td>
<td class='underline'>1</td>
</tr>
<tr>
<td class="name">Eve</td>
<td>Jackson</td>
<td class='underline'>0</td>
</tr>
<tr>
<td class="name">John</td>
<td>Doe</td>
<td class='underline'>1</td>
</tr>
</table>
Or using the td child values...
let tr = document.querySelectorAll("tr");
last = null;
for(let i = 1; i < tr.length; i++){
if(Number(tr[i].lastElementChild.innerHTML) === 1){
tr[i].firstElementChild.style.textDecoration = "underline";
}
}
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>1</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>0</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
</table>

Related

What's the best way of attaching an on-click event to a table cell?

I am trying to attach an on-click event to a table cell so when its clicked its background changes colour and text saying 'Yes' appears in the middle. I would also like it to fire a PHP method in a controller class.
I have code such as the following:
// index.html
```
<table>
<thead>
<tr>
<th>Heading</th>
</tr>
<thead>
<tbody>
<tr class="clickme">
<td><a onclick="reserve();return false;" href={{route('submitaction',
$colour->colour_id) }}</a></td> // On click, change its background
colour.
<td></td>
</tr>
</tbody>
// scripts.js
function reserve(){
$(this).addClass('newColour');
}
//flow.live.css
.newColour {
background-color: gray;
content: 'Yes';
text-align: center;
}
I am going to pad the link to fill the entire table cell and have the event fire when the user clicks the link. Is what I am trying to do sensible and are there any other clever work-arounds?
Thanks,
Rob
I have used a different markup, because you did not provide the CSS.
What you need is to delegate the event to the parent element.
I have used this markup from another website:
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>UK</td>
</tr>
<tr>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
Here is the JS:
$( "#customers" ).on( "click", "td", function( event ) { // target the outermost parent element, this will delegate the event to the element specified as second arg on the callback function
$(this).addClass('newColour'); // Here you just add the class, no inline JS needed.
});
That is it, read about event delegation and why it is useful and try the working code here, there is no PHP code, but your biggest issue was the JS.:
https://codepen.io/damPop/pen/JwZXya

how would i remove some portion of <tr><td> from a table in php?

this is my table -
<table>
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
and I want to remove this one table row:
<tr>
<td> </td>
</tr>
my expected output is:
<table>
<tr>
<td>ABC</td>
</tr>
</table>
is it possible??please help me
As you have tagged your question with the tag php i would recommend using a regular expression.
The pattern \s*<tr>\s*<td> <\/td>\s*<\/tr> will find the tr with an empty ( ) td.
To test and look into the regex you can have a look here: https://regex101.com/r/ax6Xdg/1
Put together this will look something like this:
$table = "<table>
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>";
$pattern = "/\s*<tr>\s*<td> <\/td>\s*<\/tr>/";
var_dump( preg_replace( $pattern , "" , $table ) );
This will output something very simmilar to this:
string '<table>
<tr>
<td>ABC</td>
</tr>
</table>' (length=60)
You can do this by using JQuery function .remove(). You can look it up here
Edit: If you want to locate that specific tag, you can do that by using .next()read here, .find() read here,.parent()read here, .children read here
Just add id to your table :
<table id="tableid">
<tr>
<td>ABC</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
This script find , if found then remove !
$('#tableid tr').each(function() {
if ($(this).find('td').html()==' ') $(this).remove();
});
If you want to find some text and then remove then replace html() with text()
$('#tableid tr').each(function() {
if ($(this).find('td').text()=='ABC') $(this).remove();
});
You should try this:
<table>
<tr id="abc>
<td>ABC</td>
</tr>
<tr id="remove">
<td> </td>
</tr>
<script>
$('#remove').remove();
</script>
When rendering the table, add a unique class for the rows you wish to delete. Lets say the class is: _rowToDelete, and then using jQuery, remove all the rows that have this class.
In the below example, when you click on the button the rows are being removed, so you can see the changes. But you can do the same on page load if you wish so.
$(function() {
$("#removeBtn").click(function() {
$("._rowToDelete").remove() ;
});
}) ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>ABC 1</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
<tr>
<td>ABC 2</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
<tr>
<td>ABC 3</td>
</tr>
<tr class="_rowToDelete">
<td> </td>
</tr>
</table>
Remove

Get the value from Second td from each TR

I had a table that used for user to input their value.
Table Structure:
<table id="tblAddProduct">
<tbody class='A2'>
<tr>
<td>Product Code :</td>
<td> <input/> </td>
</tr>
<tr>
<td>Product Description :</td>
<td> <input/> </td>
</tr>
</tbody>
<tbody class='A2'>
<tr>
<td>Product Code :</td>
<td> <input/> </td>
</tr>
<tr>
<td>Product Description :</td>
<td> <input/> </td>
</tr>
</tbody>
</table>
I had reference other solution and modified to my question. Here is my draft solution:
foreach(pq('.A2') as $tag) {
foreach(pq($tag)->find('tr') as $tr) {
foreach(pq($tr)->find('td') as $td) {
echo $td;
}
}
}
How to get the INPUT from the each second TD from each TR in each TBODY
Try this:
foreach ( $html->find('#tblAddProduct tbody.A2 tr') as $tr ) {
echo $tr->find('td', 1)->plaintext;
echo '<br/>';
}
I used the plaintext in an example and it worked just fine...
The full working example HERE if needed
Try doing a print_r($td); This will show whats inside. Then you can access it depending on how it is stored.

Alternate multiple columns in an html table with php

How can I output an alternate multiple column like this
<table border="1">
<tr>
<td> userid 1 </td>
</tr>
<tr>
<td> userid 2</td> <td>userid 3 </td>
</tr>
<tr>
<td> userid 4 </td>
</tr>
<tr>
<td> userid5 </td> <td>userid6 </td>
</tr>
<tr>
<td> userid7 </td>
<td>userid8 </td>
<td> userid9 </td>
<td>userid10</td>
</tr>
</table>
<br>
<table border=1>
My table query is like this:
$result = mysql_query("SELECT id,name FROM `tbl-record`") or die(mysql_error());
Classic example in action is like this:
http://www.cashcashpinoy.com
A table with five rows and an alternate 1x2x1x2x4 columns (TD ) on each rows ( TR )
Since this is in a table, you can use the colspan attribute on the td elements, like this:
<table>
<tr>
<td colspan="4">Full width data.</td>
</tr>
<tr>
<td colspan="2">Half width data.</td>
<td colspan="2">Half width data.</td>
</tr>
<tr>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
</tr>
</table>
You can do this with any number of columns.
If you want to assign a number of columns dynamically, you'll want to have a set number of results to provide some consistency, which you could do using a LIMIT along with your query.
$results; // This is all of the results of your query
$colOps = array(1,2,4); // Different colspan values
$numCols = 4; // Maximum columns to allow per line
echo '<table border="1">';
while(count($results) > 0) {
$ind = mt_rand(0, 2); // Generate a random index number
if(count($results) >= $colOps[$ind]) {
echo '<tr>';
for($i = 0; $i < $cols; $i++)
echo '<td colspan="'.$colOps[$ind].'">'.array_shift($results).'</td>';
echo '</tr>';
}
}
echo '</table>';
Note that this is untested and may need some modification to work properly.

How can i get the entire HTML of an element using regex?

i'm learning Regex but can't figure it out.... i want to get the entire HTML from a DIV, how to procced?
already tried this;
/\< td class=\"desc1\"\>(.+)/i
it returns;
Array
(
[0] => < td class="desc1">
[1] =>
)
the code that i'm matching is this;
<table id="profile" cellpadding="1" cellspacing="1">
<thead>
<tr>
<th colspan="2">Jogador TheInFEcT </th>
</tr>
<tr>
<td>Detalhes</td>
<td>Descrição:</td>
</tr>
</thead><tbody>
<tr>
<td class="empty"></td><td class="empty"></td>
</tr>
<tr>
<td class="details">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<th>Classificação</th>
<td>11056</td>
</tr>
<tr>
<th>Tribo:</th>
<td>Teutões</td>
</tr>
<tr>
<th>Aliança:</th>
<td>-</td>
</tr>
<tr>
<th>Aldeias:</th>
<td>1</td>
</tr>
<tr>
<th>População:</th>
<td>2</td>
</tr><tr>
<td colspan="2" class="empty"></td>
</tr>
<tr>
<td colspan="2"> » Alterar perfil</td>
</tr>
</tbody></table>
</td>
<td class="desc1">
<div>STATUS: OFNAaaaAA</div>
</td>
</tr>
</tbody>
</table>
i need to get the entire code inside the < td class="desc1">, like that;
<div >STATUS: OFNAaaaAA< /div>
</td>
</tr>
</tbody>
</table>
Could someone help me out?
Thanks in advance.
I usually use
$dom = DOMDocument::load($htmldata);
for converting HTML code to XML DOM. And then you can use
$node = $dom->getElementsById($id);
/* or */
$nodes = $dom->getElementsByTagName($tag);
to get your HTML/XML node.
Now, use
$node->textContent
to get data inside node.
try this, it does not cover all possible cases but it should work:
/<td\s+class=['"]\s*desc1\s*['"]\s*>((.|\n)*)<\/td>/i
tested with: http://www.pagecolumn.com/tool/pregtest.htm
edit: improved solution suggested by Alan Moore
/<td\s+class=['"]\s*desc1\s*['"]\s*>(.*?)<\/td>/s

Categories