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>
Related
I need to count how many of these items are open, and there are four types of them: Easy, Medium, Difficult and Not-Wanted. All of these types are values inside the div's. I need to exclude the 'Not-Wanted' types from the count. Notice the 'Open' and 'Close' values have different number of spaces around them. This is the html structure:
<table>
<tbody>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Closed </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td>Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td> Closed</td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td>Closed </td>
</tr>
<tr>
<td>
<div>Not-wanted</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td>Open</td>
</tr>
............
This is one of my attempts to solve the problem. It is obviously wrong, but I don't know how to get it right.
$doc = new DOMDocument();
$doc->loadHtmlFile('http://www.nameofsite.com');
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath($doc);
$elements = $xpath->query("/html/body/div[1]/div/section/div/section/article/div/div[1]/div/div/div[2]/div[1]/div[2]/div/section/div/div/table/tbody/tr");
$count = 0;
foreach ($elements as $element) {
if ($element->childNodes->nodeValue != 'Not-wanted') {
if ($element->childNodes->nodeValue === 'open') {
$count++;
}
}
}
echo $count;
I have a very rudimental knowledge of DOMXPath, so it is too complex for me, since I'm only able to create simple queries.
Can anybody help?
Thanks in advance.
Based on the data in your example, I think you can adjust the xpath expression to this to get all the <tr>'s that match your conditions:
//table/tbody/tr[normalize-space(td[3]/text()) = 'Open' and
td[1]/div/text() != 'Not-wanted']
$elements is then of type DOMNodeList and you can then get the length property to get the number of nodes in the list.
For example:
$source = <<<SOURCE
<table>
<tbody>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Closed </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td>Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td> Closed</td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td>Closed </td>
</tr>
<tr>
<td>
<div>Not-wanted</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td>Open</td>
</tr>
</tbody>
</table>
SOURCE;
$doc = new DOMDocument();
$doc->loadHTML($source);
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//table/tbody/tr[normalize-space(td[3]/text()) = 'Open' and td[1]/div/text() != 'Not-wanted']");
echo $elements->length;
Which will result in:
5
Demo
I need some help with how I can soft query from sql.
I have a php script with an html page who is showing the result from the query.
I have this query:
$ready_orders = DB::query( 'SELECT * FROM ordertable where orderid is NOT null ORDER by id DESC')->fetchAll( DB::FETCH_ASSOC );
From the HTML I have this;
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<td> want counter here</td>
<td>result 1</td>
<td>result 2</td>
<td>result 3</td>
<td>result 4</td>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</tr>
</table>
The query is doing all I want, but need the result to be in a listing.
Like a counter 1,2,3,4,5 etc
Is it possible?
include a
select count * from ordertable
Then insert this result in the first
SELECT COUNT(orderid) AS Order_Count
FROM ordertable
WHERE orderid IS NOT NULL
using foreach loop
<tal:block tal:condition="exists:orders">
<table>
<tr>
<td>amount</td>
<?php
$i = 1;
foreach($ready_orders as $result){ ?>
<td>result <?php echo $i++; ?></td> //your counter
<?php } ?>
</tr>
<tr tal:repeat="order orders">
<td tal:content="">
....
</td>
<td tal:content="order/id">
....
</td>
<td tal:content="order/orderid">
....
</td>
<td tal:content="order/productid">
....
</td>
<td tal:content="order/processed">
....
</td>
<td>
</td>
</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.
I get that error in php..And I have no idea why...
Here is how my connection is established:
<?php
$hostname_QASite = "localhost";
$database_QASite = "qasite";
$username_QASite = "root";
$password_QASite = "";
$QASite = mysql_pconnect($hostname_QASite, $username_QASite, $password_QASite) or trigger_error(mysql_error(),E_USER_ERROR);
?>
my query is the following:
mysql_select_db($database_QASite, $QASite) or die(mysql_error());
$query_get_all_topics = "SELECT topic_id, title FROM topic";
$get_all_topics = mysql_query($query_get_all_topics, $QASite) or die(mysql_error());
$row_get_all_topics = mysql_fetch_assoc($get_all_topics);
$totalRows_get_all_topics = mysql_num_rows($get_all_topics);
And then I iterate over the row_get_all_topics ...
What is wrong in the code ?
Edit:
I get that error, when I try to loop 2 times over different results in the database.
UPDATE:
<body>
<br/><br/><br/><br/><br/><br/><br/>
<div align="center">
<ul id="navlist">
<li> צור נושא</li>
<li> ראה קשרים</li>
</ul>
<?php do { ?>
<table border="1">
<tr>
<td>
<table width="100%" border="1" >
<tr>
<td width="90%" align="right">
<?php echo $row_get_all_topics['title']; ?>
</td>
<td width="10%">
:שם נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<?php
//$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());
$result="";
if($row=mysql_fetch_array($result))
{
do
{
?>
<table >
<tr>
<td>
<?php echo $row['title']; ?>
</td>
<td>
:תת נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
<tr>
<td>
עדכן
</td>
<td>
מחק
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
**}while($row=mysql_fetch_assoc($result));** 1 FIRST LOOP
}//end suptopic search
?>
<?php **} while ($row_get_all_topics = mysql_fetch_assoc($get_all_topics)); ?>** 2ND LOOP
AS soon as I add this line, to query teh database inside the loop, the page shows the error..
$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());
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.