I am using phpQuery to get the data from elements.
I'm trying to get the values from first td, seconds td and href link from each tr.
<table>
<tr class="A2">
<td> Text 1 </td>
<td> Text 2 </td>
<td> Text 3 </td>
<td> Text 131 </td>
</tr>
<tr class="A2">
<td> Text 4 </td>
<td> Text 5 </td>
<td> Text 6 </td>
<td> Text 123213 </td>
</tr>
<tr class="A2">
<td> Text 7 </td>
<td> Text 8 </td>
<td> Text 9 </td>
<td> Text 213213 </td>
</tr>
</table>
How to do this? I have tried:
<?
require('phpQuery.php');
$file = file_get_contents('test.txt', true);
$html = phpQuery::newDocument($file);
foreach($html->find('.A2') as $tag) {
echo pq('td'); // problem here?
}
?>
I guess you have them switched..
foreach(pq('.A2') as $tag) {
$tds = pq($tag)->find('td');
}
To get a value from each td, you can iterate over it inside:
foreach(pq('.A2') as $tag) {
foreach(pq($tag)->find('td') as $td) {
// stuff
}
}
pq() would return a list of matching nodes (your <td> tags, in this case). You have to iterate over that list:
foreach(pq('td') as $td) {
... do something ...
}
Related
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>
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.
I'm trying to extract a specific link from a table but is not displaying anything. It's the 3rd link in the td. I thought this would work but doesn't.
here the code:
<?php
$site = 'site';
$html = file_get_html($site);
foreach($html->find('td a', 3) as $element)
echo $element->href;
?>
Here is the HTML
<tr class="evenrow team-600-359">
<td>
Aug 17
</td>
<td>
FT
</td>
<td align="right">
Arsenal
</td>
<td align="center">
1-3
</td>
<td>Aston Villa</td>
<td style="text-align:right;">60,003</td>
</td>
<td>
Premier League
</td>
</tr>
You have invalid HTML. It can be the cause.
Check double closing of TD with 60,003 value.
Just use native DomDocument:
$str = <<<STR
<tr class="evenrow team-600-359">
<td>
Aug 17
</td>
<td>
FT
</td>
<td align="right">
Arsenal
</td>
<td align="center">
1-3
</td>
<td>Aston Villa</td>
<td style="text-align:right;">60,003</td>
</td>
<td>
Premier League
</td>
</tr>
STR;
$dom = new DOMDocument();
#$dom->loadHTML($str);
$elements = $dom->getElementsByTagName('td');
echo '<pre>' . print_r($dom->saveXML($elements->item(2)), true) . '</pre>';
OUTPUT
<td align="right">
Arsenal
</td>
This is the output of one of my file
<table>
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
I want to get the content of Description x with php and then I want to read out the Content x. One of my problem is, that before grapping the content I don't know how many description-td's I have.
Should DOM / getElementsBy ... work for my problem?
Best regards,
Susan
Here you have a solution with JQUERY. Try if yourself
In this example we show in an alert dialog the content of the description number 3.
HTML:
<table id="mytable">
<tr>
<td> Description 1 </td> <td> Content 1 </td>
</tr>
<tr>
<td> Description 2 </td> <td> Content 2 </td>
</tr>
<tr>
<td> Description 3 </td> <td> Content 3 </td>
</tr>
</table>
JQUERY:
// Search content of description number 3
$(document).ready(extractContent(3));
function extractContent(num) {
$('td', $('#mytable')).each(function() {
// The text in the first column must match " Description num "
if ($(this).text() == " Description " + num + " ")
alert("Content found: "+$(this).next().text());
});
}
lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';