PHP TCPDF heredoc - php

I got this in my heredoc in my TCPDF.
I basically want to create a dynamic pdf with the data of my database.
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname<th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
</tr>
</tbody>
</table>
EOD;
I want to make this dynamic like this, to create the data from my database dynamically.
<?php
foreach($result_set as $result) {
?>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
<?php
}
?>
I tried this so far but I cannot find a suitable solution:
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
</tbody>
</table>
EOD;
can anyone help please.

Ok I somehow did it, but still if there is any better solution feel free to post:)
Code:
$loopHereFirstname = '';
$loopHereLastname = '';
foreach($result_set_random_01 as $result_dish_usr_01) {
$tr_start = '<tr>';
$tr_end = '</tr>';
$td_start = '<td>';
$td_end = '</td>';
$loopHereFirstname .= $result_dish_usr_01['firstname']."\n";
$loopHereLastname .= $result_dish_usr_01['lastname']."\n";
}
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname</th>
</tr>
</thead>
<tbody>
$tr_start
$td_start
$loopHereFirstname
$td_end
$td_start
$loopHereLastname
$td_end
$tr_end
</tbody>
</table>
EOD;

You've to break your code into two part if you want to use a loop with <<
<?php
$html =<<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
EOD;
foreach($result_set as $result) {
$html.=<<<EOD
<tr>
<td>
{$result['firstname']}
</td>
<td>
{$result['lastname']}
</td>
</tr>
EOD;
}
$html .= '</tbody></table>';

Related

Generate new table after 10 iteration of tr tags [duplicate]

This question already has an answer here:
How can I get two items in a foreach loop in PHP? [duplicate]
(1 answer)
Closed 2 years ago.
I have a table with looping tr tags I am looking to break after every second tr tag.
like
My table looks like this after the for loop.
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
after modulo logic, I want to show'em like this
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</tbody>
</table>
And here is my PHP script so far.
I have tried all sort of arrangement b
ut unable to achieve those layout
<?php $num = 1; ?>
<table class="Table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<?php
for ( $x = 1; $x <= 12; $x++ ) {
if($num%2 == 0) {
?>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<?php
}
?>
<?php
if($num %2 == 1) {
?>
<table class="Table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<?php
}
$num++;
}
?>
</tbody>
</table>
I know I am doing a silly mistake but can't figure it out.
I appreciate your help.
Create a function that returns you a table. For every dataset of size 6, call the function and get your table. You can use array_chunk to chunk the datasets into smaller chunks and use heredoc syntax for better readability.
getTable() function:
<?php
function getTable($data){
$table = <<<EOD
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
EOD;
$rows = "";
foreach(array_chunk($data,3) as $values){
$rows .= "<tr>";
foreach($values as $value){
$rows .= "<td>" . $value . "</td>";
}
$rows .= "/<tr>";
}
$table .= $rows;
$table .= <<<EOD
</tbody>
</table>
EOD;
return $table;
}
Driver code:
<?php
$arr = [1,2,3,4,5,6,7,8,9,10,11,12];
foreach(array_chunk($arr,6) as $set_for_table){
echo getTable($set_for_table);
}
for dynamic tables you can iterate your entire table and set the two tr values.
using your example:
<?php $total = 100; ?>
<?php for ($i=0; $i < $total; $i+=6):?>
<table class="table">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td><?=$i+1?></td>
<td><?=$i+2?></td>
<td><?=$i+3?></td>
</tr>
<tr>
<td><?=$i+4?></td>
<td><?=$i+5?></td>
<td><?=$i+6?></td>
</tr>
</tbody>
</table>
<?php endfor; ?>
will generate $total/6 tables each with two <tr>

Goutte - Get table column

how can I get just one column not whole table?
<table cellspacing="0" cellpadding="0" align="Center" rules="all" border="1">
<tbody>
<tr>
<td>Entity Name</td>
<td>NV Business ID</td>
<td>Status</td>
<td>Type</td>
</tr>
<tr>
<td>GOOGLE</td>
<td></td>
<td>Expired</td>
<td>Reserved Name</td>
</tr>
<tr>
<td>GOOGLE INC.</td>
<td>NV20161275322</td>
<td>Active</td>
<td>Foreign Corporation
</td>
</tr>
</tbody>
</table>
Here is my try:
$client = new Client();
$crawler = $client->request('GET', 'url');
$form = $crawler->selectButton('Search')->form();
$crawler = $client->submit($form, array(
...
));
$crawler->filter('table tr')->each(function ($node) {
print $node->text()."\n \n";
// print $node->filter('td')->text() . '<br />';
});
It's always returning whole table.
Also tried stn like tr[1], etc..
Can someone help please?
Thanks
I found a solution:
$node->filter('td')->eq(2)->text();
2 means third column, because it's [0,1,2,...]
You can use DOMDocument for getting data from HTML.
PHP code demo
<?php
ini_set("display_errors", 1);
$html = '<table cellspacing="0" cellpadding="0" align="Center" rules="all" border="1">
<tbody>
<tr>
<td>Entity Name</td>
<td>NV Business ID</td>
<td>Status</td>
<td>Type</td>
</tr>
<tr>
<td>GOOGLE</td>
<td></td>
<td>Expired</td>
<td>Reserved Name</td>
</tr>
<tr>
<td>GOOGLE INC.</td>
<td>NV20161275322</td>
<td>Active</td>
<td>Foreign Corporation
</td>
</tr>
</tbody>
</table>';
$result=array();
$object= new DOMDocument();
$object->loadHTML($html);
$requiredColumn=3;
$requiredColumn--;
foreach($object->getElementsByTagName("tr") as $value)
{
$nodelistObject=$value->getElementsByTagName("td");
$columnCounter=0;
foreach($nodelistObject as $tdNode)
{
if($columnCounter==$requiredColumn)
{
if($tdNode->getElementsByTagName("a")->length==0)
{
$result[]=$tdNode->textContent;
}
foreach($tdNode->getElementsByTagName("a") as $aElement)
{
$result[]=$aElement->textContent;
}
}
$columnCounter++;
}
}
print_r($result);
Try the below code:
$content = $crawler->filter( 'table' )->extract( array( '_text' ) );

use div inside give table

Can I use a div inside the table given here. I can't use the div tag inside foreach.
here is my code
<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td></td>
<?php foreach($tests as $test){?>
<td><?php echo $test->name;?></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<?php echo $test![enter image description here][1]_attribute->attribute_name;?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td> </td>
<?php foreach($tests as $test){?>
<td><div><?php echo $test->name;?></div></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<div><?php echo test_attribute->attribute_name;?></div>
</td>
</tr>
<?php } ?>
</tbody>
</table>

foreach loop not generating the right html table

I'm trying to get this output from returned data fetched from mysql:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data.
$featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores");
$table_head ="<table><thead><tr><th></th>";
$table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>";
$table_end ="</tr></tbody></table>";
foreach($rows as $row)
{
$special_features = explode(",",$row->special_features);
$header_image .= "<th><img src='".$row->image_url."'></th>";
foreach($featured_tests as $featured_test)
{
$featured .= "<tr><td>".$featured_test."</td>";
if(in_array($featured_test,$special_features))
{
$featured .= "<td>Yes</td>";
}
else
{
$featured .= "<td>No</td>";
}
}
}
$table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end;
But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output?
Here's the result:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
you are creating new rows <tr> inside on most deep foreach...
take this example to make what you want:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>

How to retrieve information from database and display in table?

This is my php code.
<?php
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid=$_POST['cnetid'];
$cpassword=$_POST['cpassword'];
$cnetid = stripslashes($cnetid);
$cpassword = stripslashes($cpassword);
$cnetid = mysql_real_escape_string($cnetid);
$cpassword = mysql_real_escape_string($cpassword);
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$result=mysql_query($sql) OR die(mysql_error());
$row=mysql_fetch_assoc($result);
$rentalid=$row['RentalID'];
$bikeid=$row['BikeID'];
?>
This is the html code. It should be displayihg the query that's been retrieved from the database but for some reason when I run it the table comes up blank. I know I'm only echoing two variables but even those come up empty.
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<table class="table table-striped">
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $rentalid ?></td>
<td><?php echo $bikeid ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table><br>
</div><!-- end span -->
Try this:
---------------------------EDITED-----------------------------------------
<?php
session_start();
if(isset($_POST['cnetid'])){
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid = mysql_real_escape_string($_POST['cnetid']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
$table = "<table class='table table-striped' >
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
";
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$body = "";
$result=mysql_query($sql) OR die(mysql_error());
while ($row=mysql_fetch_assoc($result))
{
$body = $body." <tr><td>".$row['RentalID']."</td>
<td>".$row['BikeID']."</td>
<td>".$row['RentalStartDate']."</td>
<td>".$row['RentalEndDate']."</td></tr> ";
}
$table = $table.$body."
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>" ;
}
else
{
$table = "No data..";
}
?>
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<?php echo $table; ?>
<br>
</div>
PS: Error found, corrected, and code tested xD
Saludos ;)

Categories