While Loop in FPDF only prints 1 result - php

i'm having a problem with FPDF, i want to create a While Loop that returns every result that my SQL query does when i use Phpmyadmin, the problem here is that it only returns one. If i use
$pdf->Cell(190,10,''.$pdf_info2['format'].'',1,1,0);
it does print the result i want, but i need to return them in the table as i show below.
Ps: This is my firts question so i appolagise if i wasn't that clear o my problem.
Thanks in advance
$html='<table border="0">
<tr>
<td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
<td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
<td width="150" height="40" bgcolor="#e6e6e6"> </td>
<td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
<td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
</tr>';
while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
$html2 = '<tr>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
<td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
<td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
<td width="150" height="40" bgcolor="#e6e6e6">.$pdf_info['format'].</td>
</tr>';
}
$pdf->WriteHTML($html);
$pdf->WriteHTML($html2);

Use this code:
First you must need to define before loop: $html2 = ''; and concatenate with $html2 .= in while loop see below code for updated code:
$html2 ='';
$html ='<table border="0">
<tr>
<td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
<td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
<td width="150" height="40" bgcolor="#e6e6e6"> </td>
<td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
<td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
</tr>';
while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
$html2 .='<tr>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
</tr>';
}
$pdf->WriteHTML($html);
$pdf->WriteHTML($html2);

**Option1.**first you need to merger data with previous data so use $html .= $html;
Option2 update $pdf_info
to
$pdf_info2
and no need to use 2 $pdf->WriteHTML();
so total code will be
$html='<table border="0">
<tr>
<td width="150" height="40" bgcolor="#e6e6e6">Tipo</td>
<td width="150" height="40" bgcolor="#e6e6e6">Formato</td>
<td width="150" height="40" bgcolor="#e6e6e6"> </td>
<td width="150" height="40" bgcolor="#e6e6e6">Pago</td>
<td width="150" height="40" bgcolor="#e6e6e6">Editar</td>
</tr>';
while($pdf_info2 = $smth->fetch(PDO::FETCH_ASSOC)) {
$html .= '<tr>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
<td width="150" height="40" bgcolor="#e6e6e6">'.$pdf_info2['format'].'</td>
</tr>';
}
$pdf->WriteHTML($html);

Related

Resize width and height of images

I have content stored in mysql, as following:
<table width="450" cellspacing="1" cellpadding="1" border="1">
<tbody>
<tr>
<td><img width="513" height="680" align="left" alt=" src="/userfiles/image/pic.jpg" /></td>
<td><img width="315" height="700" align="left" alt=" src="/userfiles/image/DSC_0389.JPG" /></td>
<td><img width="580" height="320" align="left" alt=" src="/userfiles/image/ktxh.jpg" /></td>
</tr>
</tbody>
</table>
When I load from db, PHP and display in html by PHP, there is no problem.
Now, I want all images, be displayed by fixed width and height as 200 X 200 AND TABLE BORDER = '0'
<table width="200" cellspacing="1" cellpadding="1" border="0">
<tbody>
<tr>
<td><img width="200" height="200" align="left" alt=" src="/userfiles/image/pic.jpg" /></td>
<td><img width="200" height="200" align="left" alt=" src="/userfiles/image/DSC_0389.JPG" /></td>
<td><img width="200" height="200" align="left" alt=" src="/userfiles/image/ktxh.jpg" /></td>
</tr>
</tbody>
</table>
How do I solve this problem?
Replace with this code and css
<style>
.cstm
{
width:200px;
height:200px
}
table
{
border:0
}
</style>
<table width="200" cellspacing="1" cellpadding="1" border="0">
<tbody>
<tr>
<td><img class="cstm" align="left" alt=" src="/userfiles/image/pic.jpg" /></td>
<td><img class="cstm" align="left" alt=" src="/userfiles/image/DSC_0389.JPG" /></td>
<td><img class="cstm" align="left" alt=" src="/userfiles/image/ktxh.jpg" /></td>
</tr>
</tbody>
</table>
As you mention the $content is dynamic, can you try adding the style to the table as such:
<style>
table td>img
{
width:200px;
height:200px
}
</style>

Sort images based on order number using laravel

I have 5 images. I need to show the images based on the order.
Now I have the order in the array like
2,3,5,1,4
Now my images are in $new->image1,$new->image2,$new->image3,$new->image4,$new->image5
My result should be like if order is as like above,
order is like $new->image2,$new->image3,$new->image5,$new->image5,$new->image1,$new->image4
How can I achieve this
<table style="text-align:justify" align="center" border="1" cellspacing="0" >
{{$new->img_order}}
<?php
$order_array = explode(",", $new->img_order);
$img1 = $new->image2;
dd($new->image2);
?>
<tr>
<td height="600" rowspan="3"><img width="350" height="910px" src="{{asset('collage/'.$new->image1)}}" class="img-responsive" alt=""></td>
<td height="300" colspan="2"><img width="500" height="300px" src="{{asset('collage/'.$new->image2)}}" class="img-responsive" alt=""></td>
</tr>
<tr>
<td width="250" ><img width="250" height="350px" src="{{asset('collage/'.$new->image3)}}" class="img-responsive" alt=""></td>
<td width="250"><img width="250" height="350px" src="{{asset('collage/'.$new->image4)}}" class="img-responsive" alt=""></td>
</tr>
<tr>
<td colspan="2"><img width="500" height="250px" src="{{asset('collage/'.$new->image5)}}" class="img-responsive" alt=""></td>
</tr>
</table>
I just wonder why you not save the image path in array, but with many image1, image2, .....
Without sorting the image, you can echo the image by the order array with
$new->{'image' . $order[0]}
$new->{'image' . $order[1]}
$new->{'image' . $order[2]}
...

DataTable plugin not working with dynamically generated php data?

DataTable plugin not working with dynamically generated php data ?
I am using data table in to display a dynamic table. But its not working.
I tried to use static table its working I don't understand the problem
Here is my HTML Table and js script.
Can anyone solve the issue.
$(document).ready(function () {
$('#myTable').dataTable();
});
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" id="myTable">
<thead>
<tr>
<th width="100" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3">Client Name</th>
<th width="100" height="30"align="left" bordercolor="#999999" bgcolor="#d3d3d3">EPIC ID</th>
<th width="100" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3">Project Name</th>
<th width="100" height="30"align="left" bordercolor="#999999" bgcolor="#d3d3d3">Category</th>
<th width="100" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3">Epic Status</th>
<th width="150" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3">Total Estimated Hours</th>
<th width="150" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3">Total Time Spent</th>
<th width="150" height="30" align="left" bordercolor="#999999" bgcolor="#d3d3d3" >% Hours Burned</th>
<th width="150" height="30" align="left" bordercolor="#000000" bgcolor="#d3d3d3">% Project Complete</th>
<th width="150" height="30" align="left" bordercolor="#000000" bgcolor="#d3d3d3">Action Status</th>
</tr>
</thead>
<tbody>
<tr <?php if($cnt%2==0){echo "bordercolor='#FFFFFF' bgcolor='#FFFFFF'";}else{echo "bordercolor='#999999' bgcolor='#FFFFFF'";}?>>
<td align="left" width="100" height="30"><?php echo "$ClientName";?></td>
<td align="left" width="100" height="30">
<?php
echo form_open('Riskreport',array('name'=>'s2frm'.$cnt,'target'=>'_blank'));?>
<?php echo "$EPICID";?>
<input type="hidden" name="epickeyid" value="<?php echo "$EPICID";?>" />
<?php echo $formbuild;?>
</form>
</td>
<td align="left" width="100" height="30"><?php echo "$ProjectName";?></td>
<td align="left" width="100" height="30"><?php echo "$Category";?></td>
<td align="left" width="100" height="30"><?php echo "$epic_status";?></td>
<td align="left" width="150" height="30"><?php echo "$TotalEstimatedHours";?></td>
<td align="left" width="150" height="30"><?php echo "$TotalTimeSpentinHours";?></td>
<td align="left" width="150" height="30" <?php if(($variance2>0)&&($variance2<=100)){echo "bgcolor='#92d050'";}else if(($variance2>100)&&($variance2<=120)){echo "bgcolor='#FFC200'";}else if($variance2>120){echo "bgcolor='#ff0000'";}else{echo "bgcolor='#FFFFFF'";}?>><?php echo $variance2prcnt;?></td>
<td align="left" width="150" height="30">
<?php
$PrjtCompleteprcnt=$ops1->getprjctdone($EPICID);
echo $PrjtCompleteprcnt;
?>
</td>
<td align="CENTER" width="150" height="30"><?php echo "$ActionStatus";?></td>
</tr>
</tbody>
</table>

TCPDF - HTML table align right inside writeHTMLCell does not work

I create a table with HTML to be inserted into TCPDF::writeHTMLCell with this format. The table only contains image.
$html_table ='<table align="right" border="1">
<tr>
<td width="10%" align="center">
<img src="http://localhost/img1.png" width="40" height="40" />
</td>
<td width="10%" align="center">
<img src="http://localhost/img2.png" width="40" height="40" />
</td>
</tr>
<tr>
<td width="10%" align="center">
<img src="http://localhost/img3.png" width="40" height="40" />
</td>
<td width="10%" align="center">
<img src="http://localhost/img4.png" width="40" height="40" />
</td>
</tr>
</table>';
Here's the TCPDF code to create HTMLCell
$pdf->writeHTMLCell(100, '', '', $pdf->getY(), $html_table, 1, 0, 0, false, 'R', false);
Even thought the HTML can show the table at the right side of page, TCPDF only shows this table at the left side inside the cell.
What is the right code to put the table in align right, put in the right side of the HTMLCell ?

Regex to read Table Rows contents in Array - PHP

Is there any Regex to Read the <td> contents from the following Table, note that there are many similar tables so i want only to read the following table contents.
I want to READ data from 3,4,5 TD from all <TR>
My regex looks like following but doesnt work
$match = preg_replace('~<td width="80" bgcolor="#F3F3E4" align="left"> <a onmouseout="ChangeImage(AE1,1)" onmouseover="ChangeImage(AE1,0)" href="/charts/livegold.html">GOLD</a></td>#[a-z0-9]{6}~i','',$match[3]);
echo '<table><tr>' . $match . '</tr></table>';
the table is as follows
<table width="540" cellspacing="1" cellpadding="0" border="0" align="center">
<tbody><tr>
<td width="16" bgcolor="#000000" align="center"> </td>
<td width="80" bgcolor="#000000" align="center"><font size="1" face="Arial, Helvetica, sans-serif" color="#FFFFFF">www.kitco.com</font></td>
<td width="369" bgcolor="#000000" align="center" colspan="5"><p class="white">The World Spot Price - Asia/Europe/NY markets</p></td>
<td width="73" bgcolor="#000000" align="right"><img width="39" vspace="0" hspace="0" height="17" border="0" alt="light" src="/images/lightgreen.gif"></td>
</tr>
<tr>
<td width="16" bgcolor="#000000" align="center"> </td>
<td width="522" bgcolor="#F3F3E4" align="center" colspan="7"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font color="GREEN">MARKET IS OPEN</font><br>(Will close in 17 hrs. 41 mins.)<!-- 1486.00--></b></font></td>
</tr>
<tr bgcolor="#F3F3E4">
<td width="16" bgcolor="#000000" align="center"> </td>
<td width="80" bgcolor="#CCCC99" align="center">Metals</td>
<td width="80" bgcolor="#CCCC99" align="center">Date</td>
<td width="80" bgcolor="#CCCC99" align="center">Time (EST)</td>
<td width="68" bgcolor="#CCCC99" align="center">Bid</td>
<td width="68" bgcolor="#CCCC99" align="center">Ask</td>
<td width="146" bgcolor="#CCCC99" align="center" colspan="2">Change from NY Close</td>
</tr>
<tr bgcolor="#F3F3E4">
<td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE1','1')" onmouseover="ChangeImage('AE1','0')" href="/charts/livegold.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Gold Charts" name="AE1" src="http://www.kitco.com/images/graph_down.gif"></a></td>
<td width="80" bgcolor="#F3F3E4" align="left"> <a onmouseout="ChangeImage('AE1','1')" onmouseover="ChangeImage('AE1','0')" href="/charts/livegold.html">GOLD</a></td>
<td width="80" align="center">06/04/2013</td>
<td width="80" align="center">23:34</td>
<td width="68" align="center">1405.50</td>
<td width="68" align="center">1406.50</td>
<td width="73" align="center"><p class="spotgreen">+5.50</p></td>
<td width="73" align="center"><p class="spotgreen">+0.39%</p></td>
</tr>
<tr bgcolor="#F3F3E4">
<td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE2','1')" onmouseover="ChangeImage('AE2','0')" href="/charts/livesilver.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Silver Charts" name="AE2" src="http://www.kitco.com/images/graph_down.gif"></a></td>
<td width="80" align="left"> <a onmouseout="ChangeImage('AE2','1')" onmouseover="ChangeImage('AE2','0')" href="/charts/livesilver.html">SILVER</a></td>
<td width="80" align="center">06/04/2013</td>
<td width="80" align="center">23:34</td>
<td width="68" align="center">22.59</td>
<td width="68" align="center">22.69</td>
<td width="73" align="center"><p class="spotgreen">+0.05</p></td>
<td width="73" align="center"><p class="spotgreen">+0.20%</p></td>
</tr>
<tr bgcolor="#F3F3E4">
<td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE3','1')" onmouseover="ChangeImage('AE3','0')" href="/charts/liveplatinum.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Platinum Charts" name="AE3" src="http://www.kitco.com/images/graph_down.gif"></a></td>
<td width="80" align="left"><p> <a onmouseout="ChangeImage('AE3','1')" onmouseover="ChangeImage('AE3','0')" href="/charts/liveplatinum.html">PLATINUM</a></p></td>
<td width="80" align="center">06/04/2013</td>
<td width="80" align="center">23:34</td>
<td width="68" align="center">1501.00</td>
<td width="68" align="center">1509.00</td>
<td width="73" align="center"><p class="spotgreen">+9.00</p></td>
<td width="73" align="center"><p class="spotgreen">+0.60%</p></td>
</tr>
<tr bgcolor="#F3F3E4">
<td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE4','1')" onmouseover="ChangeImage('AE4','0')" href="/charts/livepalladium.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Palladium Charts" name="AE4" src="http://www.kitco.com/images/graph_down.gif"></a></td>
<td width="80" align="left"> <a onmouseout="ChangeImage('AE4','1')" onmouseover="ChangeImage('AE4','0')" href="/charts/livepalladium.html">PALLADIUM</a></td>
<td width="80" align="center">06/04/2013</td>
<td width="80" align="center">23:25</td>
<td width="68" align="center">755.00</td>
<td width="68" align="center">761.00</td>
<td width="73" align="center"><p class="spotgreen">+6.00</p></td>
<td width="73" align="center"><p class="spotgreen">+0.80%</p></td>
</tr>
</tbody></table>
Here is the data i want to Extract
ok here is the solution with Regex:
$patt = "/<td[^>]*width=['\"]68['\"][^>]*>([0-9\.]+)<\/td>\s*<td[^>]*width=['\"]68['\"][^>]*>([0-9\.]+)<\/td>/i";
if(preg_match_all($patt, $html, $matches))
{
//print all records
//print_r($matches);
for($i=0; $i<count($matches[1]); $i++)
{
echo "Bid: ".$matches[1][$i].", Ask: ".$matches[2][$i]."\n";
}
}
You can use some HTML parser. For PHP there is one http://simplehtmldom.sourceforge.net/
Once the DOM has been loaded into library, get through the TABLE element and iterate through each TR element.

Categories