sort in cakephp / fpdf - php

I have a small script created in CakePHP where I can generate quotes, however the quote items seem to be sorted in a non specific way on my PDF. What is the best way to have this sorted by ID in an ascending order ?
Find below my related code snippet which will list all items on my quote:
foreach ($items as $item) {
$itemSubTotal = $item['quantity'] * $item['unit_price'];
$discount_rate=$item['discount_rate'];
$unit_price=$item['unit_price'];
$subTotal += $itemSubTotal;
$itemDiscount=$itemSubTotal*$discount_rate/100;
$discount+=$itemDiscount;
//$itemTax = $itemSubTotal * ($item['tax_rate'] / 100);
$itemTax = ($itemSubTotal - $itemDiscount) * ($item['tax_rate'] / 100);
$tax += $itemTax;
$itemSubTotal = $itemSubTotal*((100-$discount_rate) / 100);
$itemSubTotal = number_format($itemSubTotal, 2, '.', ',');
$y+=5;
$pdf->setXY(5, $y);
$pdf->MultiCell(10, 5, $i++, 0, 'L');
$pdf->setXY(15, $y);
//$pdf->MultiCell(30, 5, $item['title'], 0, 'L');
$pdf->Cell(30, 5, $item['title'], 0, 2, 'L');
$pdf->setXY(45, $y);
//$pdf->MultiCell(80, 5, $item['details'], 0, 'L');
$pdf->Cell(30, 5, $item['details'], 0, 2, 'L');
$pdf->setXY(125, $y);
$pdf->MultiCell(20, 5, $item['quantity'], 0, 'R');
$pdf->setXY(145, $y);
$pdf->MultiCell(15, 5, number_format($unit_price, 2, '.', ','), 0, 'R');
$pdf->setXY(160, $y);
$pdf->MultiCell(20, 5, number_format($discount_rate, 2, '.', ','), 0, 'R');
$pdf->setXY(180, $y);
$pdf->MultiCell(25, 5, $itemSubTotal, 0, 'R');
}
Some expert help would be greatly appreciated, thank you

I have not tried this, but it should work. Have a look at cake 3 collections and specifically sortBy
If you wrap your $items in a collection, then sort them, then iterate through them you will achieve your goal.
Something like:
use Cake\Collection\Collection;
$collection = new Collection($items);
$sortedByField = $collection->sortBy('field')
If this doesn't work have a play with the other methods like groupBy

Related

Word wrap in a cell in FPDF using PHP?

I am trying to print some words in a cell but wordwrap in not working in a cell, what should i do?
$pdf->setXY(124, 36 + ($i * 5.1));
$pdf->SetFillColor(238, 236, 225);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(40, 6, $resultArrayIndex['pubtitle'], 0, 0, 'L', True);
Here is my output -
and here what i am trying to achieve - trying to wrap word within a cell. (Any help or hint is appreciated)
Edited after comments and sugggestions
After all u suggesting me use Multicell, I used it but it's still not helpful
not equal gap with each multicell and even sometimes it's uneven size text and gap
My code after your suggestion and i am using it in for-loop
$pdf->setXY(17, 36+($i * 6.9));
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(19, 4.6, formatPubDate($resultArrayIndex['pubdate']), 1);
$pdf->setXY(42, 36 + ($i * 6.9));
$pdf->SetFont('Arial', '', 8);
$pdf->MultiCell(50.5, 4.6, ($resultArrayIndex['title']), 1);
$pdf->setXY(124, 33.7 + ($i * 9.0));
$pdf->SetFont('Arial', '', 8);
$pdf-> MultiCell(27, 2.9 , $resultArrayIndex['pubtitle'],1);

CakePHP calculation not correct

I have a small script that generates quotes. I can add items with related discount or tax values, however the calculation does not seem to be correct as it's not subtracting the discount value. Based upon the screenshot I will attach the item subtotal should amount to 155,584.00
Please refer to my script below as well as a screenshot
foreach ($items as $item) {
$itemSubTotal = $item['quantity'] * $item['unit_price'];
$discount_rate=$item['discount_rate'];
$unit_price=$item['unit_price'];
$subTotal += $itemSubTotal;
$itemDiscount=$itemSubTotal*$discount_rate/100;
$discount+=$itemDiscount;
$itemTax = $itemSubTotal * ($item['tax_rate'] / 100);
$tax += $itemTax;
$itemSubTotal = number_format($itemSubTotal, 2, '.', ',');
$y+=5;
$pdf->setXY(5, $y);
$pdf->MultiCell(10, 5, $i++, 0, 'L');
$pdf->setXY(15, $y);
$pdf->Cell(30, 5, $item['title'], 0, 2, 'L');
$pdf->setXY(45, $y);
$pdf->Cell(30, 5, $item['details'], 0, 2, 'L');
$pdf->setXY(125, $y);
$pdf->MultiCell(20, 5, $item['quantity'], 0, 'R');
$pdf->setXY(145, $y);
$pdf->MultiCell(15, 5, number_format($unit_price, 2, '.', ','), 0, 'R');
$pdf->setXY(160, $y);
$pdf->MultiCell(20, 5, number_format($discount_rate, 2, '.', ','), 0, 'R');
$pdf->setXY(180, $y);
$pdf->MultiCell(25, 5, $itemSubTotal, 0, 'R');
How can I get this fixed? Some expert advise would be greatly appreciated
You haven't modified the $itemSubTotal variable with the discount. you should do this before printing it:
$itemSubTotal = $itemSubTotal*($discount_rate/100);
also, according to my calculation, the item subtotal would be 83,776.00

how to increase width according to value count

First of all i am really sorry for my english. I try to explain my wanted. I use fpdf to make dynamic invoice in pdf. I have more than one product to add one invoice and i must list them sub-bottom. But products counts change in all invoices. Some invoice has 5 products but some invoice has 2 products. My problem is i use code below but all lines overrided.
$invoices_query = mysql_query("SELECT * FROM invoice_bookings WHERE invoice_code = '$invoice_code'");
while ($invoices = mysql_fetch_array($invoices_query)) {
$customers_name = $invoices['customer_name'];
$pdf->Ln(0);
$pdf->Cell(21,123,$invoice_number,0,0,'L',0); // empty cell with left,top, and right borders
$pdf->Cell(30,123,$customers_name,0,0,'L',0);
$pdf->Cell(20,123,$date,0,0,'L',0);
$pdf->Cell(20,123,$time,0,0,'L',0);
$pdf->Cell(30,123,$suburb_from,0,0,'L',0);
$pdf->Cell(34,123,$suburb_to,0,0,'L',0);
$pdf->Cell(10,123,'% ' . $tax_percent,0,0,'L',0);
$pdf->Cell(25,123,'$ ' . $invoice_price,0,0,'R',0);
}
You can see value of margin. It's 123 as you can see. I must increase this value to 132 for example. And for other product 142 i must make. For example there are 5 products in this invoice i must make like this ;
first 123
second 133
third 143
fourth 153
fifth 163
but i really don't know how can i do this.
As this. Add a counter, initialize it, and always incrase with 10.
$cnt = 123; //Init the counter
while ($invoices = mysql_fetch_array($invoices_query)) {
$customers_name = $invoices['customer_name'];
$pdf->Ln(0);
$pdf->Cell(21, $cnt, $invoice_number, 0, 0, 'L', 0); // empty cell with left,top, and right borders
$pdf->Cell(30, $cnt, $customers_name, 0, 0, 'L', 0);
$pdf->Cell(20, $cnt, $date, 0, 0, 'L', 0);
$pdf->Cell(20, $cnt, $time, 0, 0, 'L', 0);
$pdf->Cell(30, $cnt, $suburb_from, 0, 0, 'L', 0);
$pdf->Cell(34, $cnt, $suburb_to, 0, 0, 'L', 0);
$pdf->Cell(10, $cnt, '% ' . $tax_percent, 0, 0, 'L', 0);
$pdf->Cell(25, $cnt, '$ ' . $invoice_price, 0, 0, 'R', 0);
$cnt = $cnt + 10; //Incrase it in the loop.
}
NOTE
Do not use mysql functions, they are deprecated. Use mysqli or PDO instead.
Avoid sql injections by escaping your variables comes from outside, or use prepared statements.

php gd - multicolor imagettftext

I'm trying to put multicolor text through imagettftext.
I tried drawing letter by letter but the spacing is horrible.
Here's my code:
$usrname_split = str_split("MarioErmando");
$onecharwidth = imagefontwidth((int)$font)*(12/8);
foreach($usrname_split as $key=>$letter){
if($key == 0){
// first letter
imagettftext($im, 12, 0, $xusrname, 15, $blue, $font, $letter);
$oldletters = "$letter";
}else{
$posarr=imageftbbox(12, 0 ,$font, $oldletters);
$posx = $posarr["2"];
imagefttext($im, 12, 0, $posx, 15, $red, $font, $letter);
$oldletters .= "$letter";
}
}
The output:
Note that the text is dynamic.
Is it possible to achieve multicolor text through imagettftext without horrible spacing?
Regards, MarioErmando.
in order to test, replace your code by this one:
$string="Mario Ermando";
$last_string= substr( $string, 1 );
imagettftext($im, 12, 0, $xusrname, 20, $blue, $font, $string[0]); //first letter "M"
imagefttext($im, 12, 0, 12, 20, $red, $font, $last_string);
i think the problem is when you write every letter separately

Split a string according to First substring and 2nd another substring

I have a HTML file code in a string that is like given below :
--- few lines of code ---------
<SCRIPT language="javascript" type="text/javascript">
var statList = new Array(
0, "192.168.1.179", "88-53-95-28-2B-AF", 194280, 101141053, 0, 0, 0, 0, 0, 0, 0, 0,
1, "192.168.1.170", "60-C5-47-10-37-FD", 132316, 65860791, 0, 0, 0, 0, 0, 0, 0, 0,
2, "192.168.1.151", "68-5D-43-21-76-95", 9887, 3898646, 0, 0, 0, 0, 0, 0, 0, 0,
3, "192.168.1.134", "6C-3E-6D-8C-FF-62", 26405, 7521875, 0, 0, 0, 0, 0, 0, 0, 0,
4, "192.168.1.124", "04-F7-E4-78-D3-D0", 6189, 1791672, 0, 0, 0, 0, 0, 0, 0, 0,
5, "192.168.1.121", "60-C5-47-10-40-92", 122657, 91113301, 0, 0, 0, 0, 0, 0, 0, 0);
</SCRIPT>
---- Few other lines of code ---------
I want to extract the <script> block containing the Array declaration.
Can I do this with any PHP function?
If you can find a clear delimiter that appears only once at the beginning of your string and another one that appears only once at the end, you can easily use preg_match. Otherwise, you can still use preg_match, but will have a much harder time writing a good RegExp. If you want to extract the IP/MAC data, you may want to start with a RegExp directly.
What about this piece of code ?
$content = file_get_contents('____URL_of_your_html____');
if(preg_match('/(var statList = new Array\(.*?\);)/si',$content,$matches)){
echo $matches[1];
}

Categories