limit data with same amount per page on library fpdf - php

I am using PHP and fpdf to generate my view in pdf. My problem is how to limit amount of data per page?
If I have 100 rows data, the result is 10 page. So I must Limit 10 rows per page.
I have this code :
<?php
define('FPDF_FONTPATH', 'font/');
require_once("../../includes/initialize.php");
class PDF extends FPDF {
function Header() {
$this->Image("../icon/banner.jpg", 40, 10, 15);
}
function Footer() {
$this->SetY(-15);
$this->SetFont("Arial", "I", 10);
$this->Cell(0, 10, "Page " . $this->PageNo() . "/{nb}", 0, 0, "C");
}
}
$filter = $_GET["filter"];
$value = $_GET["value"];
if (!empty($_GET["filter"]) && !empty($_GET["value"])) {
$query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang
where ".$filter." = '".$value."'";
} else {
$query = "Select id_detail_ruang, id_ruang, id_barang, no_seri from detail_ruang";
}
$sql = mysql_query($query);
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
array_push($data, $row);
}
$judul = "LAPORAN KERUSAKAN";
$header = array(
array("label" => "No. Detail Kerusakan", "length" => 25, "align" => "C"),
array("label" => "Kode Barang", "length" => 25, "align" => "C"),
array("label" => "Nama Barang", "length" => 50, "align" => "C"),
array("label" => "No Ruang", "length" => 50, "align" => "C")
);
$pdf = new PDF("L");
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 7);
$pdf->Cell(0, 20, $judul, 0, 1, "C");
$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetDrawColor(255, 0, 25);
$pdf->SetAutoPageBreak(true, 30);
foreach ($header as $kolom) {
$pdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
}
$pdf->Ln();
$pdf->SetFillColor(87, 190, 224);
$pdf->SetTextColor(33, 71, 84);
$pdf->SetFont('');
$fill = false;
foreach ($data as $baris) {
$i = 0;
foreach ($baris as $cell) {
$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
$i++;
}
$fill = !$fill;
$pdf->Ln();
}
$pdf->Output();
?>
I'm try using SetAutoPageBreak()
Is it Possible to do it..?
How to do it..?
Thanks

Since you've already go the data in a PHP array, the simplest solution would be to use array_chunk():
define('TABLE_SIZE', 100);
$pages=array_chunk($data, TABLE_SIZE, true);
foreach ($page as $table) {
foreach ($table as $baris) {
$i = 0;
foreach ($baris as $cell) {
$pdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
$i++;
}
}
if (count($table)==TABLE_SIZE) {
// do page break
}
}

Related

Output pdf not working on new server with TCPDF

I have a CRM for a couple of years working perfect with the TCPDF and I put in a new server and now I have some problems (the code is exactly the same in both of servers).
This is the code generating the PDF :
<?php
//Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
$id_facture = $_POST['id_facture'];
$id_client = $_POST['id_client'];
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Logo
$image_file = K_PATH_IMAGES.'logo.jpg';
$this->Image($image_file, 18, 5, 80, '', 'JPG', '', 'T', true, 300, '', false, false, 0, false, false, false);
}
// Page footer
public function Footer() {
$total = $this->getAliasNbPages();
//if($this->getAliasNumPage() == $total){
$info_bank = GET_bank_account();
if($bank['clearing'] != ''){$clearing = '<br>Clearing : '.$bank['clearing'];}
// Set font
$this->SetFont('helvetica', '', 8);
$txt_footer = '<table border="0" width="100%" cellpadding="2"><tr>
<td>'.NOM_SOCIETE.'<br>
'.ADRESSE_SOCIETE.' - '.CODE_POSTAL_SOCIETE.'-'.VILLE_SOCIETE.'</td>';
while($bank = mysqli_fetch_array($info_bank)){
$txt_footer .= '
<td>'.$bank['nom'].'<br>IBAN : '.$bank['iban'].'<br>BIC : '.$bank['bic'].'<br>'.$bank['type'].' : '.$bank['compte'].' '.$clearing.'
</td></tr>';
}
$txt_footer .= '<tr>
<td colspan="2" align="center"> '.MORE_INFO_SOCIETE.'</td>';
$txt_footer .= '</tr></table><hr>';
$txt_footer = ($txt_footer);
if($this->getNumPages() == 1){
$this->SetY(-30);
$this->SetFont('helvetica', 'B', 9);
$this->writeHTMLCell(0, 1, 15, $y_start, 'Informations bancaires', array('B' => array('width' => 0.4, 'cap' => 'butt', 'join' => 'round', 'dash' => 0, 'phase' => 1, 'color' => array(0, 0, 0))), 0, 0, true, 'J', true);
$this->Ln(5);
$this->SetFont('helvetica', '', 8);
$this->writeHTML($txt_footer, true, false, true, false, '');
}else{
if ($this->getPage() > 1){
$this->SetY(-30);
$this->SetFont('helvetica', 'B', 9);
$this->writeHTMLCell(0, 1, 15, $y_start, 'Informations bancaires', array('B' => array('width' => 0.4, 'cap' => 'butt', 'join' => 'round', 'dash' => 0, 'phase' => 1, 'color' => array(0, 0, 0))), 0, 0, true, 'J', true);
$this->Ln(5);
$this->SetFont('helvetica', '', 8);
$this->writeHTML($txt_footer, true, false, true, false, '');
//$this->Ln(5);
//$this->Cell(0, 5, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), '', false, 'R', 0, '', 0, false, 'T', 'M');
}else{
$this->SetY(-10);
$this->Cell(0, 5, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 'T', false, 'C', 0, '', 0, false, 'T', 'M');
}
}
//}
}
public function MultiRowIntervention($firstC, $secondC, $thirdC, $fourthC) {
$page_start = $this->getPage();
$y_start = $this->GetY();
// write the 1 cell
$this->MultiCell(25, 0, $firstC, 0, 'L', 0, 1, '', '', true, 'B', 0);
$page_end_1 = $this->getPage();
$y_end_1 = $this->GetY();
$this->setPage($page_start);
// write the 2 cell
$this->writeHTMLCell(130, 0, 35, $y_start, ($secondC), 0, 1, 0, 'L' );
$page_end_2 = $this->getPage();
$y_end_2 = $this->GetY();
$this->setPage($page_start);
// write the 3 cell
$this->MultiCell(25, 0, $thirdC, 0, 'L', 0, 1, 165 ,$y_start, true, 'B', 0);
$page_end_3 = $this->getPage();
$y_end_3 = $this->GetY();
$this->setPage($page_start);
// write the 4 cell
$this->MultiCell(20, 0, $fourthC, 0, 'R', 0, 1, 175 ,$y_start, true, 'B', 0);
$page_end_4 = $this->getPage();
$y_end_4 = $this->GetY();
// set the new row position by case
if (max($page_end_1,$page_end_2,$page_end_3,$page_end_4) == $page_start) {
$ynew = max($y_end_1, $y_end_2, $page_end_3, $page_end_4);
} elseif ($page_end_1 == $page_end_2) {
$ynew = max($y_end_1, $y_end_2);
} elseif ($page_end_2 == $page_end_3) {
$ynew = max($y_end_2, $y_end_3);
}elseif ($page_end_3 == $page_end_4) {
$ynew = max($y_end_3, $y_end_4);
} elseif ($page_end_1 > $page_end_2) {
$ynew = $y_end_1;
} elseif($page_end_2 > $page_end_3) {
$ynew = $y_end_2;
} elseif($page_end_3 > $page_end_4) {
$ynew = $y_end_3;
}else{
$ynew = $y_end_4;
}
$this->setPage(max($page_end_1,$page_end_2));
$this->SetXY($this->GetX(),$ynew);
}
public function MultiRowMateriel($firstC, $secondC, $thirdC, $fourthC, $fifthC) {
$page_start = $this->getPage();
$y_start = $this->GetY();
// write the 1 cell
$this->MultiCell(25, 0, $firstC, 0, 'L', 0, 1, '', '', true, 'B', 0);
$page_end_1 = $this->getPage();
$y_end_1 = $this->GetY();
$this->setPage($page_start);
// write the 2 cell
$this->writeHTMLCell(115, 0, 35, $y_start, ($secondC), 0, 1, 0, 'L' );
//$this->MultiCell(115, 0, ($secondC), 0, 'L', 0, 1, 35,$y_start, true, 'B', 0);
$page_end_2 = $this->getPage();
$y_end_2 = $this->GetY();
$this->setPage($page_start);
// write the 3 cell
$this->MultiCell(15, 0, $thirdC, 0, 'C', 0, 1, 150 ,$y_start, true, 'B', 0);
$page_end_3 = $this->getPage();
$y_end_3 = $this->GetY();
$this->setPage($page_start);
// write the 4 cell
$this->MultiCell(25, 0, $fourthC, 0, 'R', 0, 1, 153 ,$y_start, true, 'B', 0);
$page_end_4 = $this->getPage();
$y_end_4 = $this->GetY();
$this->setPage($page_start);
// write the 5 cell
$this->MultiCell(20, 0, $fifthC, 0, 'R', 0, 1, 175 ,$y_start, true, 'B', 0);
$page_end_5 = $this->getPage();
$y_end_5 = $this->GetY();
// set the new row position by case
if (max($page_end_1,$page_end_2,$page_end_3,$page_end_4,$page_end_5) == $page_start) {
$ynew = max($y_end_1, $y_end_2, $page_end_3, $page_end_4, $page_end_5);
} elseif ($page_end_1 == $page_end_2) {
$ynew = max($y_end_1, $y_end_2);
} elseif ($page_end_2 == $page_end_3) {
$ynew = max($y_end_2, $y_end_3);
}elseif ($page_end_3 == $page_end_4) {
$ynew = max($y_end_3, $y_end_4);
}elseif ($page_end_4 == $page_end_5) {
$ynew = max($y_end_4, $y_end_5);
} elseif ($page_end_1 > $page_end_2) {
$ynew = $y_end_1;
} elseif($page_end_2 > $page_end_3) {
$ynew = $y_end_2;
} elseif($page_end_3 > $page_end_4) {
$ynew = $y_end_3;
}elseif($page_end_4 > $page_end_5) {
$ynew = $y_end_4;
}else{
$ynew = $y_end_5;
}
$this->setPage(max($page_end_1,$page_end_2,$page_end_3,$page_end_4,$page_end_5));
$this->SetXY($this->GetX(),$ynew);
}
public function MultiRow($left, $right) {
// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0)
$page_start = $this->getPage();
$y_start = $this->GetY();
// write the left cell
$this->MultiCell(150, 0, $left, 0, 'R', 0, 2, '', '', true,true,true, 0);
$page_end_1 = $this->getPage();
$y_end_1 = $this->GetY();
$this->setPage($page_start);
// write the right cell
$this->MultiCell(0, 0, $right, 0, 'R', 0, 1, $this->GetX() ,$y_start, true,true,true, 0);
$page_end_2 = $this->getPage();
$y_end_2 = $this->GetY();
// set the new row position by case
if (max($page_end_1,$page_end_2) == $page_start) {
$ynew = max($y_end_1, $y_end_2);
} elseif ($page_end_1 == $page_end_2) {
$ynew = max($y_end_1, $y_end_2);
} elseif ($page_end_1 > $page_end_2) {
$ynew = $y_end_1;
} else {
$ynew = $y_end_2;
}
$this->setPage(max($page_end_1,$page_end_2));
$this->SetXY($this->GetX(),$ynew);
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(NOM_SOCIETE);
$pdf->SetTitle('Facture '.$id_facture);
//$pdf->SetSubject('TCPDF Tutorial');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData('logo.jpg', PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont('arial');
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 35);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------------------------------------------------------------------------
//require_once('../../../gestion_user_fns.php');
$no_client = no_client($id_facture);
$nom_client = nom_client($no_client);
$no_tel_client = no_tel_client($no_client);
$email_client = email_client($no_client);
$date_facture = inverse_date(date_facture($id_facture));
$entreprise_client = entreprise_client($no_client);
$date = date('d.m.Y');
$adresse_client = adresse_client($no_client);
$npa_client = npa_client($no_client);
$total_inter_ht = total_inter($id_facture);
$total_inter_TVA = total_inter_TVA($id_facture);
$total_mat_HT = total_mat($id_facture);
$total_mat_TVA = total_mat_TVA($id_facture);
$rabais = rabais($id_facture);
$nombre = GET_liste_mat_fact($id_facture);
$nombre = mysqli_fetch_array($nombre);
$total_HT = arrondi(total($id_facture));
$total_TVA = arrondi(total_TVA($id_facture));
if(mode_paiement($id_facture) != ""){
$mode_paiement = mode_paiement($id_facture);
}else{
$mode_paiement = "30 jours";
}
$total_TTC = arrondi(total($id_facture)+total_TVA($id_facture) - $rabais);
if ($entreprise_client != '-') {$info_client[0] = $entreprise_client.'<br>';}
if ($nom_client != ' ') {$info_client[1] = $nom_client."<br>";}
$info_client[2] = $adresse_client.'<br>';
$info_client[3] = $npa_client.'<br>';
$list_acompte = GET_acompte_PAR_id_facture($id_facture);
//echo $id_facture;
if($list_acompte){
$montant_acompte = 0;
$taxe_acompte = 0;
while($acom = mysqli_fetch_array($list_acompte)){
$montant_acompte += $acom['montant'];
$taxe_acompte += $acom['taxe'];
}
$totalAcompte = arrondi($montant_acompte + $taxe_acompte);
$total_HT = arrondi(total($id_facture) + $montant_acompte);
$total_TVA = arrondi(total_TVA($id_facture) + $taxe_acompte);
$total_TTC = arrondi(total($id_facture)+total_TVA($id_facture) + $montant_acompte + $taxe_acompte - $rabais);
$totalPayer = arrondi($total_TTC - $totalAcompte);
}
//----------------------------------------------------------------------------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 9);
// add a page
$pdf->AddPage();
ob_start();
// set some text to print
if(FAX_SOCIETE != ''){ $fax_societe = 'Téléfax '.FAX_SOCIETE.'<br>';}
if(NUMERO_TVA_SOCIETE != ''){ $numero_tva_societe = 'N° de TVA : '.NUMERO_TVA_SOCIETE.'<br>';}
// create some HTML content
$html = '<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td> </td>
<td> </td>
<td rowspan="2">'.$info_client[0].$info_client[1].$info_client[2].($info_client[3]).'</td>
</tr>
<tr>
<td><div style="">'.NOM_SOCIETE.'<br>
'.ADRESSE_SOCIETE.' '.NUMERO_SOCIETE.'<br>
'.PAYS_SOCIETE.'-'.CODE_POSTAL_SOCIETE.' '.VILLE_SOCIETE.'
<br>Téléphone '.TELEPHONE_1_SOCIETE.'<br>
'.$fax_societe.EMAIL_SOCIETE.'<br>
'.LIEN_SOCIETE.'<br>'.$numero_tva_societe.'</div></td>
<td> </td>
</tr>
<tr>
<td>
<b>Date : '.$date_facture.'</b><br>
<b>Facture N° : '.$id_facture.'</b><br>
Paiement : '.strip_tags($mode_paiement).'
</td>
<td> </td>
<td><span style="font-size:20px;"><b>FACTURE</b></span></td>
</tr>
</table>';
// output the HTML content
$pdf->Ln(1);
$html = ($html);
$pdf->writeHTML($html, true, false, true, false, '');
$borderDashedBottom = array('T' => array('width' => 0.1, 'cap' => 'square', 'join' => 'round', 'dash' => 2, 'phase' => 0, 'color' => array(0, 0, 0)));
$borderSolidBottom = array('B' => array('width' => 0.4, 'cap' => 'butt', 'join' => 'round', 'dash' => 0, 'phase' => 1, 'color' => array(0, 0, 0)));
$pdf->SetFont('helvetica', '', 8);
//=========================================================================================================
//---------------------------------------------------------- INTERVENTIONS
//=========================================================================================================
$list_intervention_facture = GET_liste_inter_fact($id_facture);
if ($intervention = mysqli_num_rows($list_intervention_facture) != 0){
if(tva_societe($no_client) || tva_facture($id_facture)) $txt_prix = 'Prix HT';else $txt_prix = 'Prix TTC';
$pdf->Multicell(0, 5, 'Intervention(s)', $borderSolidBottom, 'L', 0, 1, '', '', true);
$pdf->Ln(1);
$pdf->MultiRowIntervention('Date', 'Description', 'Durée', $txt_prix);
$pdf->Ln(-3);
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderSolidBottom, 0, 0, true, 'J', true);
$pdf->Ln(5);
while ($inter = mysqli_fetch_array($list_intervention_facture))
{
$description = $inter[1];
if ($inter[2] == 0)
$temps = '-';
elseif ($inter[2] >= 60)
{
$heure = $inter[2] / 60;
$minute = $inter[2] % 60;
if ($minute == 0)
$temps = (int)$heure . 'h';
else
$temps = (int)$heure . 'h ' . $minute . 'm';
}
else
$temps = $inter[2].'m';
$date = inverse_date($inter[0]);
if(tva_societe($no_client) || tva_facture($id_facture))
$prix = arrondi($inter[3]);
else
$prix = arrondi($inter[3]+$inter[5]);
$pdf->MultiRowIntervention($date, ($description)."\n", $temps, $prix);
$pdf->Ln(0.5);
// write the line bottom
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderDashedBottom, 0, 0, true, 'J', true);
$pdf->Ln(0.5);
$pdf->checkPageBreak(1, '', true);
}
$pdf->Ln(10);
}
//=========================================================================================================
//---------------------------------------------------------- MATERIELS
//=========================================================================================================
if($intervention){$nL = $nL + 10;}
$list_materiel_facture = GET_liste_mat_fact($id_facture);
if ($materiel = mysqli_num_rows($list_materiel_facture) != 0)
{
if(tva_societe($no_client) || tva_facture($id_facture)) $txt_prix = 'Prix HT';else $txt_prix = 'Prix TTC';
$pdf->Multicell(0, 5, 'Materiel(s)', $borderSolidBottom, 'L', 0, 1, '', '', true);
$pdf->Ln(1);
$pdf->MultiRowMateriel('Date', 'Description', 'Nombre', 'Unitaire', $txt_prix);
$pdf->Ln(-3);
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderSolidBottom, 0, 0, true, 'J', true);
$pdf->Ln(5);
while ($mat = mysqli_fetch_array($list_materiel_facture))
{
$date = inverse_date($mat[0]);
$desc = strip_tags($mat[3]." - ".$mat[4]);
if(tva_societe($no_client) || tva_facture($id_facture)){
$prix_total = $mat[1] * $mat[5];
$prix = $mat[1];
}else{
$prix_total = ($mat[1] + $mat[2]) * $mat[5];
$prix = $mat[1] + $mat[2];
}
$pdf->MultiRowMateriel($date, ($desc), $mat[5], arrondi($prix), arrondi($prix_total));
$pdf->Ln(0.5);
// write the line bottom
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderDashedBottom, 0, 0, true, 'J', true);
$pdf->Ln(0.5);
}
}
//------------------------------------------------ LES TOTAUX
$pdf->checkPageBreak(20, '', true);
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderSolidBottom, 0, 0, true, 'J', true);
if(tva_societe($no_client)) {
$pdf->Ln(5);
$pdf->MultiRow('Total HT CHF', $total_HT);
if(tva_facture($id_facture)){
$txt_tva = 'Vente hors Taxe (TVA 0%) CHF';
}else{
$txt_tva = 'TVA (7.7%) CHF';
}
$pdf->MultiRow($txt_tva, $total_TVA);
}
if ($rabais){
$pdf->MultiRow('Rabais CHF', '- '.arrondi($rabais));
}
$pdf->MultiRow('<b>Prix Total TTC CHF</b>', '<b>'.$total_TTC.'</b>');
if($montant_acompte != 0){
$pdf->MultiRow('Acomptes CHF', '- '.$totalAcompte);
$pdf->MultiRow('<b>Total à payer</b>', '<b>'.$totalPayer.'</b>');
}
$pdf->Ln(-3);
$pdf->writeHTMLCell(0, 1, 15, $y_start, '', $borderSolidBottom, 0, 0, true, 'J', true);
//Close and output PDF document
$pdf->Output('pdf/factures_pdf/facture_'.$id_facture.'_'.$date_facture.'.pdf', 'F');
//============================================================+
// END OF FILE
//============================================================+
So, now I'm having the followins errors :
Warning: array_push() expects parameter 1 to be array, null given in
and
Warning: Invalid argument supplied for foreach() in
I read somewere that's a problem with the HTML tags and I need to clean up with a tool like HTML tidy but, the code is almost in PHP and I've look for something missing and I can't found it.
There's someone who can help me with this?
Thanks

data not exist after saved in pdf format (fpdf)

I use fpdf to create pdf documents in php(codeigniter). the data to be printed has appeared in the pdf preview of google chrome. but when the data has been saved in the pdf document format, the data does not appear
this is my code:
public function laporan()
{
$bulan1 = $this->input->post('bulan1');
$bulan2 = $this->input->post('bulan2');
$tanggal = array(
'tanggal1'=>$bulan1,
'tanggal2'=>$bulan2
);
$query = "SELECT costumers.nama AS costumer, alats.nama AS alat, DATE(transaksis.tanggal_ambil) AS tanggal_ambil,DATE(transaksis.tanggal_kembali) AS tanggal_kembali FROM costumers,transaksis,alats WHERE costumers.id=transaksis.costumer_id AND transaksis.alat_id=alats.id AND DATE(transaksis.created_at) BETWEEN '$bulan1' AND '$bulan2' ORDER BY transaksis.created_at DESC";
$data = $this->UserModel->query_umum($query)->result();
$header = array(
array("label"=>"No", "length"=>8, "align"=>"L"),
array("label"=>"Peminjam", "length"=>50, "align"=>"L"),
array("label"=>"Nama Alat", "length"=>60, "align"=>"L"),
array("label"=>"Tanggal Kirim", "length"=>30, "align"=>"L"),
array("label"=>"Tanggal Pengembalian", "length"=>40, "align"=>"L")
);
$this->cetak_laporan($header,$data,$tanggal);
}
public function cetak_laporan($header,$data,$tanggal)
{
$laporan = new FPDF();
$laporan->AddPage();
$laporan->SetFont('Arial','B',12);
$laporan->Image(base_url('asset/img/logo-dewiratih.png'),5,4,30,10);
$laporan->SetY(4);
$laporan->SetX(37);
$laporan->Cell(100,5, "CV. DEWI RATIH", 0, 1, 'L');
$laporan->SetFont('Arial','',6);
$laporan->SetX(37);
$laporan->Cell(100,2, "CONTRACTOR-SUPLIER-HEAVY EQUIPMENT RENTAL-STONES CRUISER", 0, 1, 'L');
$laporan->SetX(37);
$laporan->Cell(100,2, "Jl. Laut Mororejo Kaliwungu Kendal-Jateng 51372", 0, 1, 'L');
$laporan->SetX(37);
$laporan->Cell(100,2, "Telp/Fax (024) 8666225, Email : dewiratih_99#yahoo.com", 0, 1, 'L');
$laporan->SetLineWidth(0.5);
$laporan->Line(3,16,205,16);
// $pdf->Line(1,3.2,28.5,3.2);
$laporan->SetLineWidth(0);
$laporan->SetFont('Arial','B',14);
$laporan->Cell(0,15, "LAPORAN PEMINJAMAN ALAT", 0, 1, 'C');
// $pdf->SetFont('Arial','',12);
// $pdf->Cell(168,5,"Penyewa : ",0,1,'L');
$laporan->SetFont('Arial','',9);
// foreach ($tanggal as $tgl) {
$laporan->Cell(168,5,"Laporan dari tanggal : ".$tanggal['tanggal1']." Sampai ".$tanggal['tanggal2'],0,1,'L');
// }
$laporan->SetFont('Arial','B',10);
foreach ($header as $kolom) {
$laporan->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], false);
}
$laporan->Ln();
// $fill=false;
$laporan->SetFont('Arial','',10);
$no= 0;
foreach ($data as $baris) {
$i = 0;
$laporan->Cell(8, 5, $no+1, 1, '0', $kolom['align'], false);
foreach ($baris as $cell) {
$laporan->Cell($header[$i+1]['length'], 5, $cell, 1, '0', $kolom['align'], false);
$i++;
}
$no++;
$laporan->Ln();
}
// $pdf->Ln();
$laporan->Cell(25,5,"Keterangan : ",0,0,'L');
$laporan->Output("Laporan Peminjaman.pdf","I");
}
If you want to display the document first before the user can optionally download it, then you should consider using GET as a query string instead of POST method for $bulan1 & $bulan2.
When you are previewing your pdf document from previous page, it actually sending your POST variable ($bulan1 & $bulan2), but when you save it, the browser just making a request without the POST data on it, it can be seen on your saved document that it misses the $bulan1 & $bulan2 value.
I checked your and all is well :) (google chrome, adobbe reader, foxit reader).
Check my code:
public function laporan()
{
$bulan1 = $this->input->post('bulan1');
$bulan2 = $this->input->post('bulan2');
$tanggal = array(
'tanggal1' => '1111',
'tanggal2' => '2222',
);
$data = [];
for ($i = 0; $i <= 3; $i++) {
for ($i2 = 0; $i2 <= 3; $i2++) {
$data[$i][$i2] = random_string('alnum', 5);
}
}
$header = array(
array("label" => "No", "length" => 8, "align" => "L"),
array("label" => "Peminjam", "length" => 50, "align" => "L"),
array("label" => "Nama Alat", "length" => 60, "align" => "L"),
array("label" => "Tanggal Kirim", "length" => 30, "align" => "L"),
array("label" => "Tanggal Pengembalian", "length" => 40, "align" => "L"),
);
$this->cetak_laporan($header, $data, $tanggal);
}
public function cetak_laporan($header, $data, $tanggal)
{
$laporan = new FPDF();
$laporan->AddPage();
$laporan->SetFont('Arial', 'B', 12);
$laporan->Image('https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png', 5, 4, 30, 10);
$laporan->SetY(4);
$laporan->SetX(37);
$laporan->Cell(100, 5, "CV. DEWI RATIH", 0, 1, 'L');
$laporan->SetFont('Arial', '', 6);
$laporan->SetX(37);
$laporan->Cell(100, 2, "CONTRACTOR-SUPLIER-HEAVY EQUIPMENT RENTAL-STONES CRUISER", 0, 1, 'L');
$laporan->SetX(37);
$laporan->Cell(100, 2, "Jl. Laut Mororejo Kaliwungu Kendal-Jateng 51372", 0, 1, 'L');
$laporan->SetX(37);
$laporan->Cell(100, 2, "Telp/Fax (024) 8666225, Email : dewiratih_99#yahoo.com", 0, 1, 'L');
$laporan->SetLineWidth(0.5);
$laporan->Line(3, 16, 205, 16);
// $pdf->Line(1,3.2,28.5,3.2);
$laporan->SetLineWidth(0);
$laporan->SetFont('Arial', 'B', 14);
$laporan->Cell(0, 15, "LAPORAN PEMINJAMAN ALAT", 0, 1, 'C');
// $pdf->SetFont('Arial','',12);
// $pdf->Cell(168,5,"Penyewa : ",0,1,'L');
$laporan->SetFont('Arial', '', 9);
// foreach ($tanggal as $tgl) {
$laporan->Cell(168, 5, "Laporan dari tanggal : " . $tanggal['tanggal1'] . " Sampai " . $tanggal['tanggal2'], 0, 1, 'L');
// }
$laporan->SetFont('Arial', 'B', 10);
foreach ($header as $kolom) {
$laporan->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], false);
}
$laporan->Ln();
// $fill=false;
$laporan->SetFont('Arial', '', 10);
$no = 0;
foreach ($data as $baris) {
$i = 0;
$laporan->Cell(8, 5, $no + 1, 1, '0', $kolom['align'], false);
foreach ($baris as $cell) {
$laporan->Cell($header[$i + 1]['length'], 5, $cell, 1, '0', $kolom['align'], false);
$i++;
}
$no++;
$laporan->Ln();
}
// $pdf->Ln();
$laporan->Cell(25, 5, "Keterangan : ", 0, 0, 'L');
$laporan->Output("Laporan Peminjaman.pdf", "I");
}

Find to closest higher and lower number in an array in loop

I have an array. Given a number X (that must not be contained in the array), I want to search for both the next greater and next lower number of X within the array in a single loop. My code is:
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
sort($a);
$nearestGreater = null;
$nearestLower = null;
foreach ($a as $key => $val) {
if ( $v<=$val) {
$nearestGreater = (isset($a[$key + 1])) ? $a[$key + 1]: $nearestGreater;
$nearestLower = (isset($a[$key - 1])) ? $a[$key - 1]: $nearestLower;
break;
}
}
var_dump($nearestLower);
echo "<br/>".$v."<br/>";
var_dump($nearestGreater);
unset($a);
?>
Write your code in clean and readable way using Code Block
$nearestGreater=null;
$nearestLower = null;
$a = array(1, 8, 23, 25, 40,41,22,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
$v = 104;
foreach( $a as $val)
{
if($val < $v)
{
if(isset($nearestLower))
{ if($nearestLower < $val)
$nearestLower=$val;
}
else
{
$nearestLower=$val;
}
}
if($val > $v)
{
if(isset($nearestGreater))
{ if($nearestGreater > $val)
$nearestGreater=$val;
}
else
{
$nearestGreater=$val;
}
}
}
Try this:
?>
<?php
$a = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($a);
$v = 58;
$lesser = null;
$greater = null;
foreach($a as $key => $current){
if($current <= $v){
$lesser = $current;
$greater = $a[($key+1)];
}else{
}
}
echo "<pre>";
print_r(array(
"lesser" => $lesser,
"greater" => $greater,
));
echo "</pre>";
?>
Edited TO HAVE TWO LOWER AND TWO HIGHER EVEN WHEN MATCH
<?php
$allNumbers = array(1, 8, 23, 25, 40,41,42,47, 52, 55, 66, 74,75, 76,77,78, 95, 102,103, 104, 105,106, 126, 128, 140, );
sort($allNumbers);
$input = 55;
$index = array_search($input, $allNumbers);
if(empty($index)){
foreach($allNumbers as $key => $value){
if($value < $input){
$index = ($key + 0.5);
}else{
break;
}
}
}
$below = array(
$allNumbers[ceil($index - 2)],
$allNumbers[ceil($index - 1)],
);
$above = array(
$allNumbers[floor($index + 1)],
$allNumbers[floor($index + 2)],
);
echo "<pre>";
print_r(array(
"input" => $input,
"index" => $index,
"below" => $below,
"above" => $above,
));
echo "</pre>";
?>

Foreach loop + recursion

I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
$combinations = array();
foreach($sets[0] as $s1)
foreach($sets[1] as $s2)
foreach($sets[2] as $s3)
foreach($sets[3] as $s4)
$combinations[] = array($s1, $s2, $s3, $s4);
print_r($combinations);
You can do it in recursion like this. The output is identical to your loops
<?php
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
function get_combinations($sets, &$combinations = array(), &$row = array()) {
if (count($sets) == 0) {
$combinations[] = $row;
return $combinations;
}
foreach ($sets[0] as $s) {
$row[] = $s;
get_combinations(array_slice($sets, 1), $combinations, $row);
array_pop($row);
}
return $combinations;
}
$combinations = get_combinations($sets);
print_r($combinations);

How to add queried rows with same values?

I am new to PHP. I have a table that stores the transactions bought. I am to summarize the transactions that happens in between two dates. For example: 2014-03-21 to 2014-03-23
I'm placing this in a fpdf :)
Here's my query:
<?php
$query = "SELECT * FROM tbl_items INNER JOIN tbl_receipts ON tbl_items.item_id=tbl_receipts.item_id WHERE receiptdate >= '$sdate' && receiptdate <='$ldate' ORDER BY tbl_items.item_id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$addtotal = 0;
$counter = 1;
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_assoc($result)) {
$totaladd = $row['quantity_bought'] * $row['itemquantity_price'];
$item = $row['item_description'];
$num = $row['quantity_bought'];
$pdf->Cell(15, 13, '', 0, 0, 'C');
$pdf->Cell(15, 6, $counter, 1, 0, 'R');
$pdf->Cell(60, 6, $row['item_description'], 1, 0, 'L');
$pdf->Cell(20, 6, $row['quantity_bought'], 1, 0, 'R');
$pdf->Cell(30, 6, $row['itemquantity_price'], 1, 0, 'R');
$pdf->Cell(30, 6, "$totaladd.00", 1, 0, 'R');
$pdf->Ln(6);
$addtotal = $addtotal + $totaladd;
$counter++;
}
}
$pdf->Cell(15, 15, '', 0, 0, 'C');
$pdf->Cell(15, 6, '', 1, 0, 'R');
$pdf->Cell(60, 6, 'SUB-TOTAL', 1, 0, 'L');
$pdf->Cell(20, 6, '', 1, 0, 'R');
$pdf->Cell(30, 6, '', 1, 0, 'R');
$pdf->Cell(30, 6, "$addtotal.00", 1, 0, 'R');
$pdf->Ln(4);
?>
However, after summarizing, I have multiple rows for items with same item_description. I want to add those item quantities to avoid repetition. How can I do it?
Personally, I pass by the array, or I accumulate on the key and then I browse the array.
for example:
$array = array();
while($row = mysql_fetch_assoc($result)){
$array[$row['item_description']]['quantity_bought'] = $row['quantity_bought'];
$array[$row['item_description']]['itemquantity_price'] = $row['itemquantity_price'];
...
}
foreach($array as $item_description => $array_detail){
$item = $item_description;
foreach($array_detail as $key => $value){
if ($key == 'quantity_bought') $num = $value;
...
}
}

Categories