Render table data to pdf in php - php

I have a an html form with a data table which has a pdf button on every row. I want to click that button and data for that particular row from mysql database is fetched and rendered into a table in pdf i.e. I want to render data array fetched from a table with a set id into a table in pdf. The code I wrote is rendering the first row incorrectly outside the table in pdf but all other rows correctly under their respective column (see below image)
Here is my code in view_payment_request.php;
<?php
if
(
isset($_GET["pdf"]) &&
isset($_GET['payment_request_id'])
)
{
require_once 'pdf.php';
include('database_connection.php');
include('function.php');
//-------------------------------------------------------------------------------------------------------------------------------
$output = '';
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_payment_request
WHERE
payment_request_id = :payment_request_id
LIMIT 1
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$result = $statement->fetchAll();
foreach($result as $row)
{
$output .=
'
<table width="100%" border="1" cellpadding="5" cellspacing="0">
<tr>
<th rowspan="2">Count</th>
<th rowspan="2">CRS/Site</th>
<th rowspan="2">Protocol</th>
<th rowspan="2">Budget Class</th>
<th rowspan="2">Amount</th>
</tr>
'
;
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_cost_sharing
WHERE
payment_request_id = :payment_request_id
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$cost_sharing_result = $statement->fetchAll();
$count = 0;
foreach($cost_sharing_result as $sub_row)
{
$count = $count + 1;
$output .=
'
<tr>
<td>'.$count.'</td>
<td>'.$sub_row["crs_site_id"].'</td>
<td>'.$sub_row["protocol_id"].'</td>
<td>'.$sub_row["budget_class_id"].'</td>
<td>'.$sub_row["cost_sharing_amount"].'</td>
</tr>
'
;
}
// Added Table Closing Tag
$output .=
'
</table>
'
;
}
//-------------------------------------------------------------------------------------------------------------------------------
$pdf = new Pdf();
$file_name = 'PRF#'.$row["payment_request_id"].'.pdf';
$pdf->loadHtml($output);
$pdf->render();
$pdf->stream($file_name, array("Attachment" => false));
}
?>

I have solved it by removing rowspans from the tags.

Related

Rendering 2 queries into 2 separate tables in pdf in php

I have code which is supposed to render my query data from mysql database into pdf. I want the data to be displayed in 2 tables in the same pdf. My code is doing all that but its rendering the tables as one without space between the 2 tables. How can I separate these tables?
Here is how the tables are being rendered:
Here is my code:
<?php
if
(
isset($_GET["pdf"]) &&
isset($_GET['payment_request_id'])
)
{
require_once 'pdf.php';
include('database_connection.php');
include('function.php');
//-------------------------------------------------------------------------------------------------------------------------------
$output = '';
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_payment_request
WHERE
payment_request_id = :payment_request_id
LIMIT 1
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$result = $statement->fetchAll();
foreach($result as $row)
{
//Payment Details Table Rendering ------------------------------------------------------------------------------------------------
$output .=
'
<table width="100%" border="1" cellpadding="5" cellspacing="0">
<tr>
<th>Payment Description</th>
<th>Vehicle Reg</th>
<th>Ref Number</th>
<th>Amount</th>
</tr>
'
;
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_payment_details
WHERE
payment_request_id = :payment_request_id
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$payment_details_result = $statement->fetchAll();
$count = 0;
foreach($payment_details_result as $sub_row)
{
$count = $count + 1;
$output .=
'
<tr>
<td>'.$sub_row["payment_description"].'</td>
<td>'.$sub_row["reg_number_id"].'</td>
<td>'.$sub_row["ref_number"].'</td>
<td>'.$sub_row["ref_amount"].'</td>
</tr>
'
;
}
$output .=
'
<br/>
<br/>
<br/>
'
;
//Cost Sharing Table Rendering ------------------------------------------------------------------------------------------------
$output .=
'
<table width="100%" border="1" cellpadding="5" cellspacing="0">
<tr>
<th>CRS/Site</th>
<th>Protocol</th>
<th>Budget Class</th>
<th>Amount</th>
</tr>
'
;
$statement = $connect->prepare
(
"
SELECT
*
FROM
tbl_cost_sharing
INNER JOIN
tbl_crs_site ON tbl_crs_site.crs_site_id = tbl_cost_sharing.crs_site_id
INNER JOIN
tbl_protocol ON tbl_protocol.protocol_id = tbl_cost_sharing.protocol_id
LEFT JOIN
tbl_funding_source ON tbl_funding_source.funding_source_id = tbl_protocol.funding_source_id
INNER JOIN
tbl_budget_class ON tbl_budget_class.budget_class_id = tbl_cost_sharing.budget_class_id
WHERE
payment_request_id = :payment_request_id
"
)
;
$statement->execute
(
array
(
':payment_request_id' => $_GET["payment_request_id"]
)
)
;
$cost_sharing_result = $statement->fetchAll();
$count = 0;
foreach($cost_sharing_result as $sub_row)
{
$count = $count + 1;
$output .=
'
<tr>
<td>'.$sub_row["crs_site_name"].'</td>
<td>'.$sub_row["protocol_name"].'</td>
<td>'.$sub_row["budget_class_name"].'</td>
<td>'.$sub_row["cost_sharing_amount"].'</td>
</tr>
'
;
}
}
//-------------------------------------------------------------------------------------------------------------------------------
$pdf = new Pdf();
$file_name = 'PRF#'.$row["payment_request_id"].'.pdf';
$pdf->loadHtml($output);
$pdf->render();
$pdf->stream($file_name, array("Attachment" => false));
}
?>
I have finally resolved it by replacing this part of the code;
$output .=
'
<br/>
<br/>
<br/>
'
;
with
$output .=
'
</table>
<br/>
'
;

How to convert json value database to html

I have notification table on my database where the value is using like json.
Here's my table
id | touserid | data
1 2 a:1:{i:0;s:10:"INV-000001";}
2 2 a:1:{i:0;s:10:"INV-000003";}
3 2 a:1:{i:0;s:15:"The Mej Hotel";}
4 1 a:5:{i:0;s:28:"Total Goalsi:1;s:7:"6250000";}
5 1 a:1:{i:0;s:10:"INV-000007";}
I want to use that value in html table, but I don't know how to convert the value to html table in codeigniter
Here's my view code
<table class="table table-dark">
<tbody>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Data</th>
<
</tr>
</thead>
<tbody>
<?php foreach($notifications as $notif){ ?>
<tr>
<td><?php echo $notif['id'] ?></td>
<td><?php echo $notif['data'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Here's controller code
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$data['notifications'] = $this->db->get(db_prefix() . 'notifications')->result_array();
$this->load->view('admin/sales/sales', $data);
But I don't see the data value get into html table like I want, in table it's show the error message " not_goal_message_failedArray"
I'm trying to encode the json, but I still don't know how to pass the json encode in controller to view in codeigniter
Here's the json encode
$page = $this->input->post('page');
$offset = ($page * $this->misc_model->get_notifications_limit());
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$notifications = $this->db->get(db_prefix() . 'notifications')->result_array();
$i = 0;
foreach ($notifications as $notification) {
if (($notification['fromcompany'] == null && $notification['fromuserid'] != 0) || ($notification['fromcompany'] == null && $notification['fromclientid'] != 0)) {
if ($notification['fromuserid'] != 0) {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('staff/profile/' . $notification['fromuserid']) . '">' . staff_profile_image($notification['fromuserid'], [
'staff-profile-image-small',
'img-circle',
'pull-left',
]) . '</a>';
} else {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('clients/client/' . $notification['fromclientid']) . '">
<img class="client-profile-image-small img-circle pull-left" src="' . contact_profile_image_url($notification['fromclientid']) . '"></a>';
}
} else {
$notifications[$i]['profile_image'] = '';
$notifications[$i]['full_name'] = '';
}
$data = '';
if (!empty($notification['data'])) {
$data = unserialize($notification['data']);
$x = 0;
foreach ($data as $dt) {
if (strpos($dt, '<lang>') !== false) {
$lang = get_string_between($dt, '<lang>', '</lang>');
$temp = _l($lang);
if (strpos($temp, 'project_status_') !== false) {
$status = get_project_status_by_id(strafter($temp, 'project_status_'));
$temp = $status['name'];
}
$dt[$x] = $temp;
}
$x++;
}
}
$notifications[$i]['description'] = _l($notification['description'], $dt);
$notifications[$i]['date'] = time_ago($notification['date']);
$notifications[$i]['full_date'] = $notification['date'];
$i++;
}
echo json_encode($notifications);
Do you know where's my error when tried convert the json value in the table to html table code ?
Thank you
Your data in your table is looking like a serialised array
You will not get the data via echo, should use
$this->load->view("notification_view", $notifications);
instead of
echo json_encode($notifications);

How to insert html input to the returned table from the backend?

I want to append or insert a html input to the table's td. Do you know how?
This is my html form index.php:
<table class="stack">
<thead class="tblthead">
<tr>
<th>Filename</th>
<th width="400">Date/Time</th>
<th width="300">Action</th>
</tr>
</thead>
<tbody id="importable">
</tbody>
</table>
This is my backend.php, this is where i returned the data going to the frontend which is my index.php.
case 'filerec':
$arr =[
":userID" => $_SESSION['loggedIn_PH'][0]['user_id'],
];
$query = "SELECT * FROM file_rec_tbl WHERE user_id=:userID ORDER BY file_id DESC";
$stmt = $con -> prepare( $query );
$stmt -> execute( $arr );
$data = $stmt -> fetchAll();
$table = '';
for( $x = 0; $x < count($data); $x++ ) {
$table .= '<tr fileID="'.$data[$x]['file_id'].'">
<td>'.$data[$x]['file_name'].'</td>
<td>'.$data[$x]['file_datetime'].'</td>
<td>'.html('<input type="text"></input>')
</tr>';
}
exit ($table);
break;
I got an error to the last td. Can you please help me or suggest what might be a good solution to my problem.
Try may be your problem
$table = '';
for( $x = 0; $x < count($data); $x++ ) {
$table .= '<tr fileID="'.$data[$x]["file_id"].'">
<td>'.$data[$x]["file_name"].'</td>
<td>'.$data[$x]["file_datetime"].'</td>
<td><input type="text"></input></td>
</tr>';
}

PHP MySQL page, additional HTML to show count of results

Ok so I have this PHP page.
Anyone able to help me add in a count of rows?
<h2>All Open Incidents</h2>
<table class="table table-striped table-bordered table-head-bordered-bottom table-condensed">
<thead>
<tr>
<th class=span1>Ticket ID</th>
<th class=span2>Title</th>
<th class=span2>Submitter</th>
<th class=span2>Owner</th>
<th class=span2>Status</th>
<th class=span1>Created</th>
<th class=span1>Modified</th>
<th class=span1>SLA</th>
</tr>
</thead>
<tbody>
<?php
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
HD_TICKET.CUSTOM_FIELD_VALUE10 as SLA,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID) AND
(HD_STATUS.NAME like '%Open%')
ORDER BY ID DESC
";
$result1 = mysql_query($query1);
$num = mysql_num_rows($result1);
$i = 0;
while ($i < $num)
{
$ID = mysql_result($result1,$i,"ID");
$Title = mysql_result($result1,$i,"Title");
$Status = mysql_result($result1,$i,"Status");
$Type = mysql_result($result1,$i,"Type");
$Created = mysql_result($result1,$i,"Created");
$Modified = mysql_result($result1,$i,"Modified");
$Priority = mysql_result($result1,$i,"Priority");
$Owner = mysql_result($result1,$i,"Owner");
$Submitter = mysql_result($result1,$i,"Submitter");
$SLA= mysql_result($result1,$i,"SLA");
$ID = stripslashes($ID);
$Title = stripslashes($Title);
$Status = stripslashes($Status);
$Type = stripslashes($Type);
$Created = stripslashes($Created);
$Modified = stripslashes($Modified);
$Priority = stripslashes($Priority);
$Owner = stripslashes($Owner);
$Submitter = stripslashes($Submitter);
$SLA = stripslashes($SLA);
$StatusSpan="";
if ($Status=="Stalled")
{
$StatusSpan="<span class='label label-warning'>$Status</span>";
}
$PriortySpan="";
if ($Priority=="High")
{
$PriortySpan="<span class='label label-important'><i class='icon-exclamation-sign icon-white'></i>High</span>";
}
if ($Priority=="Low")
{
$PriortySpan="<span class='label'>Low</span>";
}
if ($Priority=="Medium")
{
$PriortySpan="<span class='label'>Medium</span>";
}
if ($Priority=="Critical")
{
$PriortySpan="<span class='label'><i class='icon-exclamation-sign icon-white'></i>Critical</span>";
}
echo "<tr><td><a href='http://$KaceBoxDNS/adminui/ticket.php?ID=$ID' target='_blank'>$ID</a> $StatusSpan $PriortySpan</td> \n";
echo "<td>$Title</td> \n";
echo "<td>$Submitter</td> \n";
echo "<td>$Owner</td> \n";
echo "<td>$Status</td> \n";
echo "<td>$Created</td> \n";
echo "<td>$Modified</td> \n";
echo "<td>$SLA</td> \n";
echo "</tr> \n";
$i++;
}
echo "</tbody></table> \n";
?>
So basically, I want to count the number of results (8 for instance) and display
8 Results Found
At the moment I can get it to count, but it loops and puts "8 results found" 8 times, any help would be greatly apprecaited.
:)
Assuming that the number of results is in $num, you just need to add the line outside the while ($i < $num) loop, e.g. after the table close tag:
echo "</tbody></table>\n";
echo "<p>$num results found! Woohoo!</p>";

MySQL result data as link

I'm not really a good web programmer but I wanted to try.
I want to make my result from database as a link for example
I want "click here" as click would redirect to a new page or something.
and I think the idea is like.. a href"this.webpage.com">click here /a
my code is.
add_filter( 'the_content', 'get_scrapy_scraped' );
function get_scrapy_scraped( $content )
{
global $wpdb;
if ( ! is_page( 'jobs' ) )
return $content;
$table_name = $wpdb->prefix . "careers";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
if ( ! $retrieve_data )
return $content;
$table = '<table><tr>
<th>Job Name</th>
<th>Location/Dept</th>
<th>Complete Info</th>
<th>Application Link<th>
</tr>';
foreach ( $retrieve_data as $row )
{
$table .= "<tr>
<td>{$row->Job_Name}</td>
<td>{$row->Job_Department}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>{$row->Job_Link_Info}</td>
<td>{$row->Job_Link_Apply}</td>
</tr>";
}
$table .= "</table>";
return $table . $content;
}
Assuming that $row->Job_Link_Info and $row->Job_Link_Apply are destinations for some sort you can just add an a tag and use that as the href attribute.
For example:
$table .= '<tr>
<td>{' . $row->Job_Name . '}</td>
<td>{' . $row->Job_Department . '}</td>
// I want this Job_Link_Info and Job_Link_Apply to be links naming Click for Details and Click to Apply respectively
<td>Click for info</td>
<td>Click to apply</td>
</tr>';

Categories