Rendering 2 queries into 2 separate tables in pdf in php - 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/>
'
;

Related

Render table data to pdf in 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.

Create SQL query for Wordpress Multisite

I'm trying to create a SQL query for a Wordpress multisite. I just don't get an answer.
The multisite has an id of 2 and the table is also named 2. Can someone help my with it?
If I change wp_2_posts, wp_2_postmeta to wp_posts, wp_postmeta, then it will work but only for the multisite with id = 1. I try a lot but not correct result.
<table class="tg">
<tr>
<th colspan="4" class="tg-dqty"> <b>Vandaag: <?=$date?> <!–– automatic date ––> </b>
</th>
</tr>
<tr>
<td class="tg-apa9">Datum/Tijd</td>
<td class="tg-apa9">De klant</td>
<td class="tg-apa9">Kapper</td>
<td class="tg-apa9">Betaling</td>
</tr>
<?php
$query_res = "SELECT * FROM wp_2_posts as wp,wp_2_postmeta as wpm,wp_blogs as blg WHERE wp.ID=wpm.post_id and wpm.meta_key='_sln_booking_date' and wpm.meta_value='$date' and blg.blog_id='2'";
$results_d = $wpdb->get_results($query_res);
foreach($results_d as $resultsd)
{
$post_id = $resultsd->ID;
$first_name = get_post_meta($post_id, '_sln_booking_firstname', true);
$last_name = get_post_meta($post_id, '_sln_booking_lastname', true);
$date = get_post_meta($post_id, '_sln_booking_date', true);
$time = get_post_meta($post_id, '_sln_booking_time', true);
$services = get_post_meta($post_id, '_sln_booking_services', true);
$attendants = array();
foreach($services as $service)
{
$attendants[] = $service['attendant'];
}
$attendants = array_unique($attendants);
$attendant_names = '';
if(count($attendants) > 0)
{
$attendants = implode(',', $attendants);
$query_res = "SELECT post_title FROM `wp_2_posts`, `wp_blogs`
WHERE post_type='sln_attendant' AND ID IN ($attendants) AND blg.blog_id = '2'";
$results_a = $wpdb->get_results($query_res);
$disattendants = array();
foreach($results_a as $result)
{
$disattendants[] = $result->post_title;
}
$attendant_names = implode(',', $disattendants);
//print_r($results);
}
$status = $resultsd->post_status;
$statuses = array('sln-b-paid'=>'Betaald','sln-b-paylater'=>'Betaal later','sln-b-canceled'=>'Geannuleerd','sln-b-confirmed'=>'Bevestigd');
$classes = array('sln-b-paid'=>'tg-jsol','sln-b-paylater'=>'tg-8a9r','sln-b-canceled'=>'tg-jsolc','sln-b-confirmed'=>'tg-jsol');
?>
<tr>
<td class="<?=$classes[$status]?>">Om <?=$time?> uur<br><?=$date?></td>
<td class="<?=$classes[$status]?>"><?=$first_name?> <?=$last_name?></td>
<td class="<?=$classes[$status]?>"><?=$attendant_names?></td>
<td class="<?=$classes[$status]?>"><?=$statuses[$status]?></td>
</tr>
<?php
}
?>
</table>

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>";

Pulling Data in PHP from SQL Server

I have an issue with my script somewhere but do not know exactly where it is incorrect. Here is the code:
<title>Log</title>
</head>
<h1> Log </h1>
<form method="get" action="getlog.php">
<table width="300" border="0">
<tr>
<td> Forte ID:</td>
<td><select id="ForteID" name="ForteID">
<option value="nc4682">nc4682</option>
<option value="bs1441">bs1441</option>
<option value="sp3212">sp3212</option>
</select></td>
</tr>
</table>
<input type="submit" name="getLog" value="Get Log">
</form>
</head>
<body>
</body>
</html>
<?php
/*print_r($_POST);*/
$serverName = 'SRB-Nick_Desktop\SQLEXPRESS';
$connectionInfo = array('Database'=>'cslogs', 'UID'=>'cslogslogin', 'PWD'=>'123456');
$connection = sqlsrv_connect($serverName, $connectionInfo);
$query = 'SELECT ForteID, Disposition, appNumber, Finance_Num, Num_Payments, ACH_CC, Notes from logs';
$result = sqlsrv_query($connection,$query);
if (!$result)
{
$message = 'ERROR: ' . sqlsrv_errors();
return $message;
}
else
{
$i = 0;
echo '<html><body><table><tr>';
while ($i < sqlsrv_num_rows($result))
{
$meta = sqlsrv_fetch($result, $i);
echo '<td>' . $meta->name . '</td>';
$i = $i + 1;
}
echo '</tr>';
while ( ($row = sqlsrv_fetch_array($result)))
{
$count = count($row);
$y = 0;
echo '<tr>';
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
}
sqlsrv_free_stmt ($result);
echo '</table></body></html>';
}
sqlsrv_close( $connection);
?>
And this is what I get in result, no matter what is selected in the drop down box and hit the button get log.
nc4682 nc4682 Save Save asdf asdf fdas fdas 1 1 cc cc asdf asdf
bs1441 bs1441 LOC LOC AN00336862 AN00336862 None None 0 0 ach ach Backout Backout
nc4682 nc4682 Save Save AN00336862 AN00336862 easdf easdf 2 2 ach ach asdf asdf
sp3212 sp3212 Sale Sale NCXXXXXX1 NCXXXXXX1 none none 1 1 cc cc asdfasdfasdf asdfasdfasdf
Each column is duplicated. There are supposed to be 7 columns.
To only show the results from the drop down list you would append something like
WHERE ForteID = {$_GET['ForteID']}
to the end of your SQL statement.
sqlsrv_fetch_array() returns both a numeric array and an associative array by default.
Try specifying only one type of array
while ( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC ))
To use your dropdpwn in the query.
$query = 'SELECT ForteID, Disposition, appNumber, Finance_Num, Num_Payments, ACH_CC, Notes FROM logs WHERE ForteID =' . $_GET['ForteID'];
Please be aware that without sanitizing the $_GET variable this query is vulnerable to sql injection.

Categories