Unable to stream pdf: headers already sent codeigniter - php

i had this error when i want to export my file to pdf
Unable to stream pdf: headers already sent
this is my function export_to_pdf inside the controller
function export_to_pdf() {
$this->load->helper('warsito_pdf_helper');
$this->load->model('gedung/gedung_model');
$data['row'] = $this->gedung_model->laporan_perawatan_keseluruhan();
$object = $this->load->view('manage/pdf_report_all', $data);
$filename = 'Report.pdf';
generate_pdf($object, $filename, true);
}
and i have helper with name warsito_pdf_helper like this
function generate_pdf($object, $filename, $direct_download = true) {
require_once 'dompdf/dompdf_config.inc.php';
$dompdf = new DOMPDF();
$dompdf->load_html($object);
$dompdf->render();
if($direct_download == true) {
$dompdf->stream($filename);
} else {
return $dompdf->output();
}
}
and finally the view for the pdf
pdf_report_all.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Favicons-->
<link rel="apple-touch-icon-precomposed" href="<?php echo base_url();?>assets/home/assets/img/favicon/apple-touch-icon-152x152.png">
<meta name="msapplication-TileColor" content="#FFFFFF">
<meta name="msapplication-TileImage" content="<?php echo base_url();?>assets/home/assets/img/favicon/mstile-144x144.png">
<link rel="icon" href="<?php echo base_url();?>assets/home/assets/img/favicon/favicon-32x32.png" sizes="32x32">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Materialize core CSS -->
<link href="<?php echo base_url();?>assets/home/materialize/css/materialize.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="assets/js/html5shiv.js"></script>
<script src="assets/js/respond.min.js"></script>
<![endif]-->
<link href="<?php echo base_url();?>assets/home/template.css" rel="stylesheet" type="text/css">
</head>
<body>
<center><b><h5>Laporan Pembayaran Keseluruhan</h5></b></center>
<!--
<div class="container">
<div class="row">
<div class="col s12 m12"> -->
<table class="bordered" align="center">
<tr>
<th>No</th>
<th>No Ref</th>
<th>Jenis Perawatan</th>
<th>Tanggal Pembayaran Tagihan</th>
<th>Jumlah Pembayaran</th>
</tr>
<?php foreach($row as $row): $no = 1; $total_pembayaran = 0;?>
<tr>
<td><?php echo $no++?></td>
<td><?php echo $row['NO_ID']?></td>
<td><?php echo $row['NAMA_PERAWATAN']?></td>
<td><?php echo $row['TANGGAL_PEMBAYARAN']?></td>
<td><?php echo "Rp. ".number_format($row['BIAYA'])?></td>
</tr>
<?php $total_pembayaran = $row['BIAYA'] + $total_pembayaran?>
<?php endforeach;?>
</table>
<table style="display: inline-block;">
<tr>
<td><b>Total Pembayaran Keseluruhan: </b></td>
<td><b><?php echo "Rp. ".number_format($total_pembayaran)?></b></td>
</tr>
</table>
<!--
</div>
</div>
</div> -->
<script src="<?php echo base_url();?>assets/home/assets/js/jquery.min.js"></script>
<script src="<?php echo base_url();?>assets/home/materialize/js/materialize.js"></script>
<script src="<?php echo base_url();?>assets/home/index.js"></script>
</body>
</html>
i have googling my problem before, mostly says this error occurs because i have an extra lines in my file i already try the solution from this link DOMPDF: Unable to stream pdf: headers already sent
and add the following code after the stream();
$f;
$l;
if(headers_sent($f,$l))
{
echo $f,'<br/>',$l,'<br/>';
die('now detect line');
}
but the error still happens, so anyone can tell me whats wrong with my method or my code, thanks

In your controller change $object = $this->load->view('manage/pdf_report_all', $data);
to $object = $this->load->view('manage/pdf_report_all', $data, TRUE);
And also change your view to
`
<center><b><h5>Laporan Pembayaran Keseluruhan</h5></b></center>
<table class="bordered" align="center">
<tr>
<th>No</th>
<th>No Ref</th>
<th>Jenis Perawatan</th>
<th>Tanggal Pembayaran Tagihan</th>
<th>Jumlah Pembayaran</th>
</tr>
<?php foreach($row as $row): $no = 1; $total_pembayaran = 0;?>
<tr>
<td><?php echo $no++?></td>
<td><?php echo $row['NO_ID']?></td>
<td><?php echo $row['NAMA_PERAWATAN']?></td>
<td><?php echo $row['TANGGAL_PEMBAYARAN']?></td>
<td><?php echo "Rp. ".number_format($row['BIAYA'])?></td>
</tr>
<?php $total_pembayaran = $row['BIAYA'] + $total_pembayaran?>
<?php endforeach;?>
</table>
<table style="display: inline-block;">
<tr>
<td><b>Total Pembayaran Keseluruhan: </b></td>
<td><b><?php echo "Rp. ".number_format($total_pembayaran)?></b></td>
</tr>
</table>

Related

Problem displaying html with bootstrap-table.js

I have created a dynamic list with a bootstrap filter, but there is a problem displaying all the links.
I do not know why the HTML code is displayed
Please help me please.
cordially.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.9.1/extensions/editable/bootstrap-table-editable.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.9.1/extensions/export/bootstrap-table-export.js" type="text/javascript"></script>
<script src="https://rawgit.com/hhurz/tableExport.jquery.plugin/master/tableExport.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.9.1/extensions/filter-control/bootstrap-table-filter-control.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.min.css" />
<link rel="stylesheet" type="text/css" href="https://rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/css/bootstrap-editable.css" />
<link rel="stylesheet" type="text/css" href="https://rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/css/bootstrap-editable.css" />
<table id="table"
data-toggle="table"
data-search="true"
data-filter-control="true"
data-show-export="true"
data-click-to-select="true"
data-toolbar="#toolbar">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="article" data-filter-control="input" data-sortable="true">Article</th>
<th data-field="composant" data-filter-control="select" data-sortable="true">Composant</th>
<th data-field="fournisseur" data-filter-control="input" data-sortable="true">Fournisseur</th>
</tr>
</thead>
<tbody>
<? while($row = $req->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td class="bs-checkbox "><input data-index="0" name="btSelectItem" type="checkbox"></td>
<td><?php echo $row['code_article'] ?></td>
<td><?php echo $row['comp'] ?></td>
<td><?php echo $row['fournisseur'] ?></td>
</tr>
<? } ?>
</tbody>
</table>
Add data-escape="false" to your table tag - then the "plain" HTML will be rendered
<table id="table"
...
data-escape="false"
>
see https://bootstrap-table.com/docs/api/table-options/#escape
Thank you very much
with attribute data-escape="false" it works perfectly.

Parse error: syntax error unexpected end of file in php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
Here is my code, I check all closing & open brace,I also not use its short form of php anywhere & i don't miss ';',
I don't know why Its shows parse error:
<html>
<head>
<script type="text/javascript" src="<?php echo base_url('assests/js/jquery.min.js') ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assests/css/bootstrap.min.css') ?>">
<script type="text/javascript" src="<?php echo base_url('assests/js/bootstrap.min.js') ?>"></script>
</head>
<body>
<div class="container">
<table class="hover hover-table">
<tr>
<th>No</th>
<th>Code</th>
<th>Name</th>
<th>Address</th>
</tr>
<?php if(count($all_user>0))
{ $i=0;
foreach ($all_user as $user)
{
$i++;
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $user->code ?></td>
<td><?php echo $user->name?></td>
<td><?php echo $user->address ?></td>
</tr>
}
}
</table>
</div>
</body>
</html>
You are missing to open and close php
</tr>
<?php } /* endforeach */
} /* endif */ ?>
</table>
That's why you are getting errors.
You're missing the closing php tags before curly braces <?php }}. Becasue of that you're getting Line : 36 -- syntax error, unexpected end of file
Try this way,
<html>
<head>
<script type="text/javascript" src="<?php echo base_url('assests/js/jquery.min.js') ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assests/css/bootstrap.min.css') ?>">
<script type="text/javascript" src="<?php echo base_url('assests/js/bootstrap.min.js') ?>"></script>
</head>
<body>
<div class="container">
<table class="hover hover-table">
<tr>
<th>No</th>
<th>Code</th>
<th>Name</th>
<th>Address</th>
</tr>
<?php if(count($all_user>0))
{ $i=0;
foreach ($all_user as $user)
{
$i++;
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $user->code ?></td>
<td><?php echo $user->name?></td>
<td><?php echo $user->address ?></td>
</tr>
<?php
}
}
?>
</table>
</div>
</body>
</html>

DataTables plugin doesn't work

I've found this awesome plugin that sorts and filters and shows pagination etc. and I've tried it on a test empty page and it works. But when I put the same code on my project it doesn't. What is going on? The only difference I can see is that my app has PHP/Mysql data, but this plugin is supposed to work fine with both. :/
Links
<!-- BOOTSTRAP-->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- DATATABLES-->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs-3.3.7/jq-2.2.4/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.13/af-2.1.3/b-1.2.4/b-colvis-1.2.4/b-flash-1.2.4/b-html5-1.2.4/b-print-1.2.4/cr-1.3.2/fc-3.2.2/fh-3.1.2/kt-2.2.0/r-2.1.1/rr-1.2.0/sc-1.4.2/se-1.2.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs-3.3.7/jq-2.2.4/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.13/af-2.1.3/b-1.2.4/b-colvis-1.2.4/b-flash-1.2.4/b-html5-1.2.4/b-print-1.2.4/cr-1.3.2/fc-3.2.2/fh-3.1.2/kt-2.2.0/r-2.1.1/rr-1.2.0/sc-1.4.2/se-1.2.0/datatables.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
Table
<div class="row">
<?php
$prepIexec = $conn->prepare('SELECT id, nom, zona, tlf, email, gats, coordinador FROM TaulaCasesAcollida ORDER BY nom ASC');
$prepIexec->execute();
if($prepIexec->rowCount() > 0){ ?>
<div class="table-responsive">
<table id="taula_acollida" class='table taulaAco display' cellspacing="0" width="100%">
<thead>
<tr>
<th class="taulaAco_fila1 uppercase">Nom</th>
<th class="taulaAco_fila1 uppercase">Zona</th>
<th class="taulaAco_fila1 uppercase">Telèfon</th>
<th class="taulaAco_fila1 uppercase">Email</th>
<th class="taulaAco_fila1 uppercase">Gats</th>
<th class="taulaAco_fila1 uppercase">Coordinador</th>
</tr>
</thead>
<?php
while($row = $prepIexec->fetch(PDO::FETCH_ASSOC)){
extract($row);
?>
<tbody>
<tr>
<td class="taulaAco_fila2 capitalize"><?php echo $nom; ?></td>
<td class="taulaAco_fila2"><?php echo $zona; ?></td>
<td class="taulaAco_fila2"><?php echo $tlf; ?></td>
<td class="taulaAco_fila2"><?php echo $email; ?></td>
<td class="taulaAco_fila2 capitalize"><?php echo $gats; ?></td>
<td class="taulaAco_fila2 capitalize"><?php echo $coordinador; ?></td>
<td class="taulaAco_fila2 E">
<a title="editar" href="editar_casaAcollida.php?edit_id=<?php echo $row['id']; ?>" title="editar"><i class="material-icons iconoEditar">mode_edit</i></a>
<a class="X" title="eliminar" href="?eliminar_id=<?php echo $row['id']; ?>" onclick="return confirm('Estàs segur que vols el·liminar aquesta casa d'acollida?')"><i class="material-icons iconoEliminar">cancel</i></a>
</td>
</tr>
<?php
}
}
else{
?>
<div class="col-xs-12">
<div class="alert alert-warning">No hi ha cases d'acollida.</div>
</div>
<?php
}
?>
</tbody>
</table>
</div>
</div>
Script
<script>
$(document).ready(function() {
$('#taula_acollida').dataTable();
});
</script>
Put <tbody> outside the while loop
You have 6 columns in <thead> and 7 columns in <tbody>. This causes mData error.

jQuery DataTable appears without pagination control

I'm trying to insert Bootstrap pagination to my dynamic table content which I extract from my database as below, but nothing works:
<?php
$sn = $_POST["SN"];
$Reference = $_POST["Reference"];
$Libelle = $_POST["Libelle"];
$NOMENCLATURE = $_POST["NOMENCLATURE"];
$matricule = $_POST["matricule"];
include "./Connections/localhost.php";
$sqlTous = "
select mat.SN, mat.NOMENCLATURE, mat.ID_materiel, mat.Reference, mat.Date_affectation, mat.libelle, mat.Date_Acquisition, mat.Fournisseur,mat.commentaire, mat.Contrat from materiel mat
left join user usr on usr.ID_User = mat.ID_User ";
?>
<div>
<form method="post" action="./consultation.php" id="formClient">
<h4 class="widgettitle" width="10%" >Résultat</h4>
<br/>
<table class="table table-striped" id="example" class="display">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<?php
$reponse3 = mysql_query($sqlTous);
while ($rowMat1 = mysql_fetch_array($reponse3)) { ?>
<tbody class="table table-striped" id="example" class="display">
<tr>
<td><?php echo $rowMat1['SN']; ?></td>
<td><?php echo $rowMat1['NOMENCLATURE']; ?></td>
<td><?php echo $rowMat1['Reference']; ?></td>
<td><?php echo $rowMat1['libelle']; ?> </td>
<td><?php echo $rowMat1['Date_Acquisition']; ?></td>
<td><?php echo $rowMat1['Date_affectation']; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</form>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
In addition, i have included the following js and css files:
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Recherche matériel</title>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery-migrate-1.1.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.2.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
There are some problems with your code:
tbody element shouldn't have id and class attributes and should be outside of the while loop.
You're including jquery-1.9.1.min.js twice.
<tbody>
<?php
$reponse3 = mysql_query($sqlTous);
while ($rowMat1 = mysql_fetch_array($reponse3)) { ?>
<tr>
<td><?php echo $rowMat1['SN']; ?></td>
<td><?php echo $rowMat1['NOMENCLATURE']; ?></td>
<td><?php echo $rowMat1['Reference']; ?></td>
<td><?php echo $rowMat1['libelle']; ?> </td>
<td><?php echo $rowMat1['Date_Acquisition']; ?></td>
<td><?php echo $rowMat1['Date_affectation']; ?></td>
</tr>
<?php } ?>
</tbody>

Combine multiple table results into one table in php

I followed a web tutorial on how to build a basic search engine from my database the problem is that when I get the results displayed it shows each result in its own table on my page? I want to merge the results so they all show under one table in html.
<?php
include("dbconnect.php");
if(!isset($_POST['search'])) {
header("Location:www.bacons.me");
}
$search_sql="SELECT * FROM users WHERE username OR FirstName LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query) !=0) {
$search_rs=mysql_fetch_assoc($search_query);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Bacons.me">
<meta name="keywords" content="HTML,CSS,JavaScript">
<meta name="author" content="James Narey">
<meta name=viewport content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="stylesheet" type="text/css" href="main.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<title>Bacons.me</title>
</head>
<body>
<h1 class="logo">BACONS.ME</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Search</li>
<li>Contact</li>
</ul>
<div class="handle">Menu<span class="arrow"> ▾</span></div>
</nav>
<p>Search Results</p>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
<table>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th>
</tr>
<tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
</table>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
} else {
echo "No results found";
}
?>
<html>
<title>results</title>
<head>
</head>
<body>
</body>
</html>
<footer>
<p class="footer">Website created by James Narey 2015.</p>
</footer>
</body>
</html>}
Put your table tag outside your do while loop, otherwise it would create new table at every iteration of the loop.
Your code structure should be like this
<?php if(mysql_num_rows($search_query) !=0) {?>
<table>
<?php do { ?>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th>
</tr>
<tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
?>
</table>
...} while ($search_rs=mysql_fetch_assoc($search_query));
I suspect this is the issue. You're creating a table for each time you fetch the next record. You probably want to do something like this:
<table>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
// fill in table...
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
?>
</table>
Notice the table tags are outside the loop, so you'll only create a new row for each record you fetch
Just place the table tags outside the loop structure:
<table> do { ?> <tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th> </tr> <tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td> </tr> <br> <?php } while ($search_rs=mysql_fetch_assoc($search_query)); </table>

Categories