Add td to specific column in table using php - php

I'm still new to php and im trying to make a scheduler script with php and trying to add a tag to a specific column
My code:
<table>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<tr>
<td>3</td> /* I'm trying to get this td in column 3 */
<td>1</td> /* I'm trying to get this td in column 1 */
<td>2</td> /* I'm trying to get this td in column 2 */
</tr>
</table
Any help would be appreciated
EDIT
To make things clear i simplified my code.

I could not understand your Question clearly that you want to generate dynamic code to add td by php if yes, below code may useful for you
<?php foreach($td_array as $key=>$val){
echo "<tr>";
echo "<td>".$key."</td><td>".$val."</td>";
echo "</tr>";

Related

Split mysql retrieved data into headers and rows

I retrieve data from DB and I need to insert all of them in a table.
I have some constant headers and some that depends on the query results.
Below you can see a table; how i want it to look like:
I did prepare the HTML structure:
<table class="table-fill">
<thead>
<tr>
<th>ID</th>
<th>CODE</th>
<th colspan=2>TREND A</th>
<th colspan=2>TREND B</th>
<th colspan=2>TREND C</th>
</tr>
<tr>
<th></th>
<th></th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td>1</td>
<td>A</td>
<td>10</td>
<tD>150</td>
<td>5</td>
<tD>200</td>
<td>8</td>
<tD>120</td>
</tr>
</tbody>
No problem so far. The hard part is to use actual data from DB and insert them to the table.
Headers "trend a", "trend b" etc are not constants. The below tables will be displayed on different countries. Some countries have specific trend names. Let's say England has "Trend A", "Trend B" and "Trend D". Italy has only "Trend A" etc.
Is it possible to write php code and in some way auto generate the table headers based on the query results?
And then split the results by trend so that correct values will go to correct trend?
I know how to do a simple table like:
$sql_r = "SELECT o.id, o.code, tl.name, ts.items, ts.revenue
FROM overview o, trend_stats ts, trend_lang tl
WHERE o.id_shop = '1'
AND o.id = ts.id
AND ts.id_trend = tl.id_trend";
$data_to_output = Db::getInstance()->ExecuteS($sql_r);
foreach ($data_to_output as $data) {
echo '
<tr>
<td>'.$data['id'].'</td>
<td>'.$data['code'].'</td>
<td>'.$data['trend'].'</td>
<td>'.$data['items'].'</td>
<td>'.$data['revenue'].'</td>
</tr>
';
}
But I'm not sure how to split and structure them the way I want them to be. If there's any tutorial or anything online related to what I'm asking I'm happy to check it out. Thank you
I don't think you can do that other than rewrite your headers. The fact that you want to retrieve your informations from your database tells that you'll have to work with any kind of loop. So you first have to display your headers and then display your informations. The only way is to store somewhere in your loop the value that will change your headers when it changes, and add another line of header when the value actually changes. You can do that with just a simple if condition.

Simple HTML DOM: Notice->Trying to get property of non-object

I am getting an php notice when using simple html dom to scrape a website. There are 2 notices displayed and everything rendered underneath looks perfect when using the print_r function to display it.
The website table structure is as follows:
<table class=data schedTbl>
<thead>
<tr>
<th>DATA</th>
<th>DATA</th>
<th>DATA</th>
etc....
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="class1">DATA</div>
<div class="class2">SAME DATA AS PREVIOUS DIV</div>
</td>
<td>DATA</td>
<td>DATA</td>
etc....
</tr>
<tr>
<td>
<div class="class1">DATA</div>
<div class="class2">SAME DATA AS PREVIOUS DIV</div>
</td>
<td>DATA</td>
<td>DATA</td>
etc....
</tr>
<tr>
<td>
<div class="class1">DATA</div>
<div class="class2">SAME DATA AS PREVIOUS DIV</div>
</td>
<td>DATA</td>
<td>DATA</td>
etc....
</tr>
etc....
</tbody>
</table>
The code below is used to find all tr in table[class=data schedTbl]. I have a tbody selector in there, but it seems to pay no attention to this selector as it still selects the tr in the thead.
include('simple_html_dom.php');
$articles = array();
getArticles('www.somesite.com');
function getArticles($page) {
global $articles;
$html = new simple_html_dom();
$html->load_file($page);
$items = $html->find('table[class=data schedTbl] tbody tr');
foreach($items as $post) {
$articles[] = array($post->children(0)->first_child(0)->plaintext,//0 -- GAME DATE
$post->children(1)->plaintext,//1 -- AWAY TEAM
$post->children(2)->plaintext);//2 -- HOME TEAM
}
}
So, I believe notices come from the tr in the thead because I am calling on the first child of the first td which only has one record. The reason for two is there is actually two tables with the same data structure in the body.
Again, I believe there are 2 ways of solving this:
1) PROBABLY THE EASIEST (fix the find selector so the TBODY works and only selects the tds within the tbodies)
2) Figure out a way to not do the first_child filter when it is not needed?
Please let me know if you would like a snapshot of the print_r($articles) output I am receiving.
Thanks in advance for any help provided!
Sincerely,
Bill C.
Just comment out line #695 in the simple_html_dom.php
if ($m[1]==='tbody') continue;
Then it should read the tbody.

xPath, getting tabular data

I have a HTML thus like:
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<tr>
<td>Joe Bloggs</td>
<td>joe#bloggs.com</td>
<td>40</td>
</tr>
<tr>
<td>John Doe</td>
<td>john#doe.com</td>
<td>40</td>
</tr>
</table>
Is there a way using xPath to get the first 2 columns, i.e the Name and Email fields?
I can get the table data using $data = $xpath->query( '//table'); just unsure how to get only the first 2 columns.
Many thanks
Get the first two td's:
//table/tr/td[position() <= 2]

jQuery DataTables on live tables from PHP script

I'm having a problem with the implementation of DataTables, I simple can't make it work in a returned table from a PHP script. Suppose this simple PHP script named simple_table.php:
<?php
echo '
<table class="table_type">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>';
?>
Now I also have the next very simple jQuery script declared after the </body> tag in the hipotetic html file:
$(document).ready( function () {
$('table.table_type').dataTable();
} );
$('div.push_button li').click(function() {
var content = $(this).closest('div').children('div.content');
$.get('simple_table.php', {}, function(html_table) {
content.html(html_table);
} );
} );
What is needed to be made for this simple example to work and keeping in it simple? The problem is that there is no solution for this useful scenario of a dynamic table made by a so simple script!
Well, maybe you have to have a preexisting <table> and only yhen be able to filled up the respective <tr>...
The problem is because you are running .dataTable() before the page has the element table.table_type. Instead of calling .dataTable() when the page loads, you need to run it once the table has been added to your <div>.
$('div.push_button li').click(function() {
var content = $(this).closest('div').children('div.content');
$.get('simple_table.php', {}, function(html_table) {
content.html(html_table);
content.find('table').dataTable();
} );
} );

add a class to particular row in codeigniter using table class

How can I add a class to particular row in codeigniter using table class?
You can't add a class to a row in the same way you can added extra attributes to other elements CI-style. You can, however, add a class to every td IN A ROW, and then operate on the class as it references the entire row:
$td1 = array(
'data' => '/*actual html you want in the td*/',
'class' => 'myclass'
);
$table->add_row($td1);
It's annoying to have to do every single td like that, and it isn't really what you want to have to do, but it's the best out there from all the solutions that I've seen.
I would stay away from the CI table class if i were you; it is overly messy and doesnt really make things any easier or save you work.
I prefer this:
<table cellspacing="0" cellpadding="4">
<tr>
<th>Col One</th>
<th>Col Two</th>
<th>Col Three</th>
</tr>
<?php if($table_data != FALSE){?>
<?php foreach($table_data->result() as $row){?>
<tr <?php echo (expr to find the row)? 'class="your_class"' : ''; ?>>
<td></td>
<td></td>
<td></td>
</tr>
<?php }?>
<?php }?>
</table>

Categories