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.
Related
I want to print a section of a table if the data is available in JSON file for it. And if the data is not there and it shouldn't do anything.
So, far what I am doing is as below. Checking is the account_type is parent and then generate the table rows with corresponding data.
<?php
$str = file_get_contents('test.json');
$json = json_decode($str, true);
$parent= strtr('<tr>
<td colspan="2" style="color:#263a80;font-size:19px;padding-bottom:20px;padding-top:20px">Parent Information</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">First Name</td>
<td>#fname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Last Name</td>
<td>#lname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Mobile Number</td>
<td>#mobile</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Email</td>
<td><a #email target="_blank" rel="noreferrer">#email</a></td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Country</td>
<td>#country</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">State</td>
<td>#state</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">City</td>
<td>#city</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Postal Code</td>
<td>#pcode</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Full Home Address</td>
<td>#address</td>
</tr>', ["#fname"=>$json["json"]["register_session_1"]["first_name"],
"#lname"=>$json["json"]["register_session_1"]["last_name"],
"#mobile"=>$json["json"]["register_session_1"]["mobile"],
"#email"=>$json["json"]["register_session_1"]["email"],
"#country"=>$json["json"]["register_session_1"]["country"],
"#state"=>$json["json"]["register_session_1"]["state_online"],
"#city"=>$json["json"]["register_session_1"]["city"],
"#pcode"=>$json["json"]["register_session_1"]["postal_code"],
"#address"=>$json["json"]["register_session_1"]["address"]
]);
if ($json["json"]["register_session_1"]["account_type"]==="parent"){
echo $parent;
}
else {
echo "hahahah";
}
?>
Problem is that it printing the table data anyway. I don't know what I am doing wrong. The if-else condition is not working.
I have this code and I can't seem to figure out why. I have added this code in between the <table></table> tag.
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>
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
I have an html table like this
<table>
<tbody>
<tr>
<td><table>
<tbody>
<tr class="prdLi">
<td rowspan="2" class="prdNo"><span>310.</span></td>
<td colspan="2" class="prdDe" rowspan="2"><span>Pepsi</span></td>
</tr>
<tr class="prdLi">
<td class="prdAc"><span> 1.5L</span></td>
<td><span> </span></td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
the table is saved as $html
I want to select the child elements of the class .prdLi
I tried like this:
foreach($html->find('tr.prdLi') as $foo){
echo $foo;
}
the output that i get is like this
<span>310.</span>
<span>Pepsi</span
.
.
.
but what i actually want to get is the code with the parent element td.like this:
<td rowspan="2" class="prdNo"><span>310.</span></td>
<td colspan="2" class="prdDe" rowspan="2"><span>Pepsi</span></td>
.
.
.
please help me
Since Simple HTML DOM Parser supports CSS like selectors, you can use 'tr.prdLi td' to specify all td elements which are children of tr with class prdLi. The following should provide what you are looking for:
$htmlstr = <<<EOD
<table>
<tbody>
<tr>
<td><table>
<tbody>
<tr class="prdLi">
<td rowspan="2" class="prdNo"><span>310.</span></td>
<td colspan="2" class="prdDe" rowspan="2"><span>Pepsi</span></td>
</tr>
<tr class="prdLi">
<td class="prdAc"><span> 1.5L</span></td>
<td><span> </span></td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
EOD;
$html = str_get_html($htmlstr);
foreach ($html->find('tr.prdLi td') as $foo) {
echo $foo . "\n";
}
Note that find() is called on the main simple_html_dom-element. In your example, the result was already limited by a previous find().
What andy says is correct, but the css for direct child is > *, therefore:
foreach($html->find('tr.prdLi > *') as $foo){
echo $foo . "\n";
}
I have two HTML tables. Each has three rows and one column only. I'd like to join them programmatically such that I get one table with two columns and three rows.
Is there some function or hack to achieve this? How can I achieve this?
For example
First Table:
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Second Table:
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
This is what I want from the above two tables:
<table id="table_three">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
Well, this is a way to proceed (check in-code comments):
$table1 = <<<'_DATA_'
<table id="table_one">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
$table2 = <<<'_DATA_'
<table id="table_two">
<tbody>
<tr>
<td>
<label>data one</label>
</td>
</tr>
<tr>
<td>
<label>data two</label>
</td>
</tr>
<tr>
<td>
<label>data three</label>
</td>
</tr>
</tbody>
</table>
_DATA_;
// Load teh 1st table
// Create a DOM object
$html1 = new simple_html_dom();
// Load HTML from a string
$html1->load($table1);
// Load teh 2nd table
// Create a DOM object
$html2 = new simple_html_dom();
// Load HTML from a string
$html2->load($table2);
// Find all rows from both tables
$rows1 = $html1->find('tr');
$rows2 = $html2->find('tr');
// Build teh 3rd table
$table3 = '<table id="table_three">
<tbody>';
// Insert rows cells from both initial tables
for ($i=0; $i < count($rows1); $i++) {
$table3 .= '<tr>';
// get row's innerhtml
$table3 .= $rows1[$i]->innertext;
$table3 .= $rows2[$i]->innertext;
$table3 .= '</tr>';
};
// finish the table
$table3 .= '</tbody>
</table>';
// Clear DOM objects
$html1->clear();
unset($html1);
$html2->clear();
unset($html2);
It creates this:
<table id="table_three">
<tbody>
</tbody>
</table>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#table_one tr').each(function(index){
var row = $(this);
var row2 = $('#table_two tr').get(index);
$(row).append($(row2).find('td'));
$('#table_three').append($(row));
});
});
</script>
Here's how I'd do it:
// assume we have the tables in three strings:
// table_one in $str_1, table_two in $str_2, and table_three in $str_3.
// create $str_3 by copying $str_1 and replacing the table ID
$str_3 = $str_1;
$str_3 = str_replace('table_one', 'table_three', $str_3);
// load table_two and table_three in simple-html-dom and collect the TD elements
$tab_2 = str_get_html($str_2);
$tab_3 = str_get_html($str_3);
$tds_3 = $tab_3->find("td");
$tds_2 = $tab_2->find("td");
// go through the TDs in table_three and append the corresponding table_two TD
$acc = 0;
foreach ($tds_3 as &$td) {
$td->outertext = $td->outertext . $tds_2[$acc]->outertext;
$acc++;
}
// done!
echo $tab_3;
Not sure I understood the question, but:
<?php
// first and second table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_one_or_two">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>
// 3TH table
//Select data
$reponse = $bdd->prepare('SELECT data1, data2 FROM '.RESEARCH.' WHERE X=X');
$reponse->execute(array(X));
?>
//Begin table
<table id="table_three">
<tbody>
//Loop the rows
while ($data = $reponse->fetch())
{
<tr>
<td>
<label>$data['data1']</label>
</td>
<td>
<label>$data['data2']</label>
</td>
</tr>
}
//end of loop, end of table
</tbody>
</table>