我在将数据从标签选择传递到控制器时遇到问题。我尝试了很多次来修复它。 我只想将分页数据打印成 pdf 文件。
这里是代码控制器:
public function process_pdf(){
$limit = $this->input->get('per_page') ? $this->input->get('per_page') : 5;
$page = $this->uri->segment(3) ? $this->uri->segment(3) : 1;
// $submit = $this->input->get('submit');
// if ($submit) {
$this->generate_pdf($page, $limit);
// }
}
public function generate_pdf($page, $limit) {
$stylesheet = file_get_contents('assets/css/bootstrap-theme.css');
//$limit = $this->input->get('page') ? $this->input->get('page') : 5;
//$page = $this->session->userdata('current_page') ? $this->session->userdata('current_page') : 1;
$data ['limit'] = $limit;
$data ['page'] = $page;
$data ['result'] = $this->MPendapatan->current_page_pdf($limit, $page);
$html = $this->load->view('pdf_view', $data, true);
// Set header agar respons berupa PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="laporan.pdf"');
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->writeHTML($html);
$mpdf->output();
}
这是视图代码:
<h4>Pendapatan Rental Mobil</h4>
<?php $limit = 0;?>
<div class="row">
<div class=" col-xs-0 col-xs-offset-0">
<select id="per_page_pendapatan">
<option value="5"<?php echo ($this->input->get('per_page') == 5 || $limit == 5) ? 'selected' : ''; ?>>5</option>
<option value="10" <?php echo ($this->input->get('per_page') == 10 || $limit == 10) ? 'selected' : ''; ?> >10</option>
<option value="25" <?php echo ($this->input->get('per_page') == 25 || $limit == 25 ) ? 'selected' : ''; ?>>25</option>
<option value="50" <?php echo ($this->input->get('per_page') == 50 || $limit == 50) ? 'selected' : ''; ?>>50</option>
<option value="100" <?php echo ($this->input->get('per_page') == 100 || $limit == 100) ? 'selected' : ''; ?>>100</option>
</select>
</div>
<div class=" col-xs-11 col-xs-offset-11">
<!-- <form action="Pendapatan/process_pdf" method="GET" target="_blank">
<div class=" col-xs-11">
<input type="submit" class="btn btn-primary" name="print" id="printButton">
</div>
</form> -->
<a href="<?php echo site_url(); ?>/Pendapatan/process_pdf" target="_blank"> <button class=" btn btn-primary" id="printButton">Print</button></a>
</div>
</div>
<br>
<div class="rows">
<div class=" col-md-12" id="dataContainer">
<?php if ($result) : ?>
<?php $total_sewa = 0; ?>
<?php $total_denda = 0; ?>
<?php $total_pembayaran = 0; ?>
<?php
// Dapatkan nomor halaman saat ini dari segmen URL atau set sebagai 1 jika tidak ada
$halaman_sekarang = $this->uri->segment(3) ? $this->uri->segment(3) : 1;
// Dapatkan nilai per_page dari segmen URL atau set sebagai 5 jika tidak ada
$selected_limit = $this->input->get('per_page') ? $this->input->get('per_page') : 5;
// Hitung nomor awal untuk halaman saat ini
$nomor_awal = ($halaman_sekarang - 1) * $selected_limit + 1;
?>
<table class="table table-bordered">
<thead>
<tr>
<th>No.</th>
<th>Nama Pelanggan</th>
<th>Tanggal Pinjam</th>
<th>Lama Pinjam</th>
<th>Tanggal Pengembalian</th>
<th>Kelebihan Sewa</th>
<th>Sewa Pembayaran</th>
<th>Denda</th>
<th>Total Pembayaran</th>
</tr>
</thead>
<tbody>
<?php
$i = $nomor_awal;
?>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->nama_pelanggan ?></td>
<td><?php echo $row->tanggal_pinjam ?></td>
<td><?php echo $row->lama_pinjam ?></td>
<td><?php echo $row->tanggal_kembali ?></td>
<td><?php echo $row->total_keterlambatan_pengembalian ?></td>
<td><?php echo "Rp.". number_format($row->total_pembayaran , 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($row->denda, 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($row->sub_total_pembayaran, 0, ',', '.') ?></td>
</tr>
<?php $total_sewa += $row->total_pembayaran; ?>
<?php $total_denda += $row->denda; ?>
<?php $total_pembayaran += $row->sub_total_pembayaran; ?>
<?php $i++; ?>
<?php endforeach ?>
<tr>
<td colspan="6" style="text-align: right;"><strong>Total:</strong></td>
<td><?php echo "Rp.". number_format($total_sewa, 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($total_denda, 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($total_pembayaran, 0, ',', '.') ?></td>
</tr>
</tbody>
</table>
<p><?php echo $links; ?></p>
<?php else : ?>
<br>
Tidak ada data
<?php endif ?>
</div>
</div>
</div>
</div>
<script>
//print ke pdf
$("#printButton").click(function () {
// Ambil select tag saat ini
var limit = $("#per_page_pendapatan").val();
// Ambil nomor halaman dari URI saat ini
var page = <?php echo $this->uri->segment(3) ? $this->uri->segment(3) : 1; ?>;
// Kirim permintaan AJAX ke controller
$.ajax({
type: "GET",
url: "<?php echo site_url(); ?>/Pendapatan/process_pdf/" + limit,
data: { "page": page }, // Kirim data submit agar metode generate_pdf dijalankan
success: function (response) {
// Tanggapi respons jika diperlukan (optional)
// Misalnya, tampilkan pesan sukses atau sebagainya
console.log("PDF berhasil dicetak!");
},
error: function (xhr, status, error) {
// Tanggapi jika terjadi kesalahan saat melakukan permintaan AJAX
console.log("Terjadi kesalahan: " + error);
},
});
});
</script>
</body>
</html>
当我更改标签时,从此代码中选择:
<select id="per_page_pendapatan">
<option value="5"<?php echo ($this->input->get('per_page') == 5 || $limit == 5) ? 'selected' : ''; ?>>5</option>
<option value="10" <?php echo ($this->input->get('per_page') == 10 || $limit == 10) ? 'selected' : ''; ?> >10</option>
<option value="25" <?php echo ($this->input->get('per_page') == 25 || $limit == 25 ) ? 'selected' : ''; ?>>25</option>
<option value="50" <?php echo ($this->input->get('per_page') == 50 || $limit == 50) ? 'selected' : ''; ?>>50</option>
<option value="100" <?php echo ($this->input->get('per_page') == 100 || $limit == 100) ? 'selected' : ''; ?>>100</option>
</select>
它将更改表格分页中的数据。
如果您在此处看到代码按钮
<div class=" col-xs-11 col-xs-offset-11">
<!-- <form action="Pendapatan/process_pdf" method="GET" target="_blank">
<div class=" col-xs-11">
<input type="submit" class="btn btn-primary" name="print" id="printButton">
</div>
</form> -->
<a href="<?php echo site_url(); ?>/Pendapatan/process_pdf" target="_blank"> <button class=" btn btn-primary" id="printButton">Print</button></a>
</div>
按钮将激活ajax将数据发送到控制器中的方法,这里是ajax代码:
$("#printButton").click(function () {
// Ambil select tag saat ini
var limit = $("#per_page_pendapatan").val();
// Ambil nomor halaman dari URI saat ini
var page = <?php echo $this->uri->segment(3) ? $this->uri->segment(3) : 1; ?>;
// Kirim permintaan AJAX ke controller
$.ajax({
type: "GET",
url: "<?php echo site_url(); ?>/Pendapatan/process_pdf/" + limit,
data: { "page": page }, // Kirim data submit agar metode generate_pdf dijalankan
success: function (response) {
// Tanggapi respons jika diperlukan (optional)
// Misalnya, tampilkan pesan sukses atau sebagainya
console.log("PDF berhasil dicetak!");
},
error: function (xhr, status, error) {
// Tanggapi jika terjadi kesalahan saat melakukan permintaan AJAX
console.log("Terjadi kesalahan: " + error);
},
});
});
这是代码控制器:
public function process_pdf(){
$limit = $this->input->get('per_page') ? $this->input->get('per_page') : 5;
$page = $this->uri->segment(3) ? $this->uri->segment(3) : 1;
// $submit = $this->input->get('submit');
// if ($submit) {
$this->generate_pdf($page, $limit);
// }
}
然后将发送到此处的方法以加载视图以显示 pdf 文件:
public function generate_pdf($page, $limit) {
$stylesheet = file_get_contents('assets/css/bootstrap-theme.css');
//$limit = $this->input->get('page') ? $this->input->get('page') : 5;
//$page = $this->session->userdata('current_page') ? $this->session->userdata('current_page') : 1;
$data ['limit'] = $limit;
$data ['page'] = $page;
$data ['result'] = $this->MPendapatan->current_page_pdf($limit, $page);
$html = $this->load->view('pdf_view', $data, true);
// Set header agar respons berupa PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="laporan.pdf"');
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->writeHTML($html);
$mpdf->output();
}
问题是当您将标签选择值更改为10或其他值时,在视图mpdf中仍然显示5个值数据。
在我回显变量 $limit 后,它仍然显示值 5。这是 pdf 文件的视图
<?php $total_sewa = 0; ?>
<?php $total_denda = 0; ?>
<?php $total_pembayaran = 0; ?>
<?php
echo 'berikut ini data limit dan page :'.nl2br("\n");
echo "page ".$page.nl2br("\n");
echo "limit ".$limit.nl2br("\n");
?>
<table class=" table table-bordered">
<thead>
<tr>
<th>No.</th>
<th>Nama Pelanggan</th>
<th>Tanggal Pinjam</th>
<th>Lama Pinjam</th>
<th>Tanggal Pengembalian</th>
<th>Kelebihan Sewa</th>
<th>Sewa Pembayaran</th>
<th>Denda</th>
<th>Total Pembayaran</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->nama_pelanggan ?></td>
<td><?php echo $row->tanggal_pinjam ?></td>
<td><?php echo $row->lama_pinjam ?></td>
<td><?php echo $row->tanggal_kembali ?></td>
<td><?php echo $row->total_keterlambatan_pengembalian ?></td>
<td><?php echo "Rp." . number_format($row->total_pembayaran, 0, ',', '.') ?></td>
<td><?php echo "Rp." . number_format($row->denda, 0, ',', '.') ?></td>
<td><?php echo "Rp." . number_format($row->sub_total_pembayaran, 0, ',', '.') ?></td>
</tr>
<?php $i++; ?>
<?php $total_sewa += $row->total_pembayaran; ?>
<?php $total_denda += $row->denda; ?>
<?php $total_pembayaran += $row->sub_total_pembayaran; ?>
<?php endforeach ?>
<tr>
<th colspan="6" style="text-align: right;"><strong>Total:</strong></th>
<td><?php echo "Rp.". number_format($total_sewa, 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($total_denda, 0, ',', '.') ?></td>
<td><?php echo "Rp.". number_format($total_pembayaran, 0, ',', '.') ?></td>
</tr>
</tbody>
</table>
如果您从 view_pdf 中看到 php 代码,我会回显该值:
<?php
echo 'berikut ini data limit dan page :'.nl2br("\n");
echo "page ".$page.nl2br("\n");
echo "limit ".$limit.nl2br("\n");
?>