Using Dompdf to view .pdf from CakePHP 2 page - cakephp

I have just downloaded dompdf-0.6.1 and want to use it in my CakePHP 2.0.5 application.
I have included Router::parseExtensions('json', 'pdf'); in routes.php and created a default.ctp in app -> View -> Layouts.
<?php
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
echo $dompdf->output();
I want to allow a user to view an income statement as a .pdf file - which can be printed. The income statement takes the parameters of startDate and endDate for generation.
The file below is the income_statement.ctp. What do I need to do in order to allow a user to view the statement as .pdf by pressing on a link on the browser page?
.......some code......
<div style="text-align: center">
<h1><?php echo $this->Session->read("Business.name"); ?><br>Income Statement</h1>
<h3>
For period starting <?php echo $startDate; ?> to <?php echo $endDate; ?>
</h3>
<table align="center" width="400px;">
<tr>
<th style="text-align: left;" colspan="2">Revenue</td>
</tr>
........code.........
<td style="text-align: left;">
<?php
$space = " ";
for ($i = 0; $i < 12; $i++)
echo $space;
?>
<b>Total Operating Expenses</b>
</td>
<td style="text-align: right;"><b><?php echo number_format($totalExpenses + $saleDiscount, 2); ?></b></td>
</tr>
<tr>
<th style="text-align: right"><?php echo $totalDescription; ?></th>
<th style="text-align: right"><?php echo number_format($net, 2); ?></th>
</tr>
</table>
....Link for view as pdf.....
</div>

You can either save the pdfs as actual files and then link to them (link to site.com/pdf/filename.pdf):
public function action() {
$view = new View(null, false);
$view->set(compact('variable1', 'variable2'));
$view->viewPath = 'Folder';
$output = $view->render('income_statement', 'layout');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($output), Configure::read('App.encoding'));
$dompdf->render();
file_put_contents(APP . 'webroot' . DS . 'pdf' . DS .'filename.pdf');
}
Or, you can just echo what $dompdf returns from the render, and just change your page's headers (link to site.com/controller/action.pdf):
public function action() {
$this->layout = $this->autoRender = false;
$this->response->header(array('Content-type' => 'application/pdf'));
//Code for generating pdf data similar to above
echo $dompdf->render();
}

Related

Displaying data from database based on specific values Codeigniter 3

I don't know how to explain this but i hope you can get the glimpse about my problem.
I have list of kode_mtk, if i click the button Input it will direct to view list of student who takes that subject.
I want to display that list of student based on kode_mtk and kelas. i tried to put $kode_mtk and $kelas in model and controller but it doesn't works.
I hope you can guide me well since i'm newbie in codeigniter.
This is my controller :
public function display(){
$data['nama'] = $this->session->userdata('username');
$kode_mtk = $this->input->post('kode_mtk');
$kelas = $this->input->post('kelas');
$data['hasil'] = $this->mtk_model->hasil();
$this->load->view('templates/header_tutor',$data);
$this->load->view('tutor/daftar_mtk_np', $data);
$this->load->view('templates/footer_tutor');
}
This is my model :
function hasil () {
$data = $this->session->userdata('username');
$kode_mtk = $this->input->post('kode_mtk');
$kelas = $this->input->post('kelas');
$this->db->select('
tb_kelas.idtutor,
tb_kelas.nim,
tb_kelas.nama,
tb_kelas.masa,
tb_kelas.kelas,
tb_kelas.idtutorial,
tb_mtk.kode_mtk,
tb_mtk.kode_program_studi,
prodi.nama_program_studi
');
$this->db->from('tb_kelas');
$this->db->join('tb_mtk', 'tb_mtk.idtutorial = tb_kelas.idtutorial');
$this->db->join('prodi', 'prodi.kode_program_studi = tb_mtk.kode_program_studi');
$this->db->where('tb_kelas.idtutor',$data);
$this->db->where('tb_kelas.masa',20172);
$this->db->where('tb_mtk.kode_mtk',$kode_mtk);
$this->db->where('tb_kelas.kelas',$kelas);
$query = $this->db->get();
return $query->result();
}
And here is my view
<div class="box-body table-responsive no-padding">
<div class="form-group">
<table class="table table-hover">
<tr>
<th>No</th>
<th>NIM</th>
<th>NAMA</th>
<th>KELAS</th>
<th>KODE MTK</th>
<th>PRODI</th>
<th>N.AKHIR</th>
<th>AKSI</th>
</tr>
<?php
if(isset($hasil)>0){$i=1; foreach($hasil as $row) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->nim; ?></td>
<td><?php echo $row->nama; ?></td>
<td><?php echo $row->kelas; ?></td>
<td><?php echo $row->kode_mtk; ?></td>
<td><?php echo $row->nama_program_studi; ?></td>
<td><input type="text" class="form-control na" id="na<?php echo $i; ?>" name="na" readonly /></td>
<td>Input Nilai<td>
</tr>
<?php $i++;}}?>
</form>
</table>
</div>
</div>
So, i found the mistakes. I changed $this->input->post to $this->input->get and put this in model. So in controller i just load $data['hasil'] = $this->mtk_model->hasil();
Thanks for your comments all :)

Pagination won't load all data from a ng-repeat after i reload

im using ng-repeat to display all values from a database using mysql, im converting the data to string in a php script shown here.
<?php
require("../easyCRUD/Usuarios.class.php");
require("../easyCRUD/TipoUsuario.class.php");
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$usuario = new Usuarios();
$tipousuario = new TipoUsuario();
$usuarios = $usuario->all();
$data="";
$len = count($usuarios);
$i = 0;
foreach($usuarios as $ban){
//echo "i=".$i;
$tipousuario->find($ban['id_tipo_usuario']);
$data .= '{';
$data .= '"id_usuario":"' . $ban['id_usuario'] . '",';
$data .= '"usuario":"' . $ban['usuario'] . '",';
$data .= '"nombre":"' . $ban['nombre'] . '",';
$data .= '"apellido":"' . $ban['apellido'] . '",';
$data .= '"cedula":"' . $ban['cedula'] . '",';
$data .= '"correo":"' . $ban['correo'] . '",';
$data .= '"telefono":"' . $ban['telefono'] . '",';
$data .= '"estado":"' . $ban['estado_cliente'] . '",';
$data .= '"tipo_usuario":"' . $tipousuario->tipo_usuario. '"';
$data .= '}';
if ($i != $len - 1) {
$data .= ',';
}
$i++;
}
echo '{"records":[' . $data . ']}';
?>
then in the controller i bind the data to the scope.
$scope.getAll = function(){
console.log("starto");
$http.get("procesos/read_usuario.php").success(function(response){
$scope.names = response.records;
console.log($scope.names);
console.log("finish");
});
}
and in the view i show the values in with a ng-repeat
<tbody ng-init="getAll()">
<tr ng-repeat="d in names | filter:search | startFrom:(currentPage - 1 )*pageSize | limitTo: pageSize">
<td class="text-align-center" >
<p class="text-center">{{ d.id_usuario }}</p>
</td>
<td>
{{d.usuario}}
</td>
<td>
<p class="text-center" >{{d.tipo_usuario}}</p>
</td>
<td>
<p class="text-center" >{{d.nombre}}</p>
</td>
<td>
<p class="text-center" >{{d.apellido}}</p>
</td>
<td>
<p class="text-center" >{{d.cedula}}</p>
</td>
<td>
<p class="text-center" >{{d.correo}}</p>
</td>
<td>
<p class="text-center" >{{d.telefono}}</p>
</td>
<td>
<p class="text-center" >{{d.estado_cliente}}</p>
</td>
<td>
<a ng-click="readOne(d.id_usuario)" class="btn btn-primary" id={{"aElement"+d.id}} >Edit</a>
<a ng-click="deleteElement(d.id_usuario)" class="btn btn-Danger">Delete</a>
</td>
</tr>
in my console it displays all the data correctly even "usuario" but when i render it in the view the only value that doesn't show is "usuario".
edit: it seems to be a problem with pagination, im using angular-bootstrap pagination, if i click next it shows the missing values.
You are trying to render tipo_usuario but in your console log the property is called usuario.
turns out it was the pagination, i didnt set the value of the current page and the page size.
$scope.pageSize=5;
$scope.currentPage=1;

How to post the data of chosen item in dropdown into a table

How do I put the information of the selected item in dropdown list into the table
like if I choose "chris" from the dropdown all of the details of chris that is on the database will be post in a table?
I would like to make it like this:
Select Customer:DropdownList you will choose one from the dropdown after you select one there is a table below that will display further details of the one you have chosen
Firstname| LastName| Address
Chris | Chris | California
CONTROLLER
public function salesorders() {
if ($this->session->userdata('logged_in')) {
$this->header2();
$data['groups'] = $this->secretary_model->getAllGroups();
$this->load->view('secretary/transactions',$data);
} else {
redirect('secretary/sec_login_view');
}
}
MODEL
function getAllGroups()
{
$query = $this->db->query('SELECT firstname FROM tblcustomer');
return $query->result();
}
VIEW
<?php
echo "Select Customer";
$options = array();
foreach($groups as $group)
{
$options[$group->firstname] = $group->firstname;
}
echo form_dropdown('dropdown', $options);
?>
<br>
<br>
<br>
<table id="myTable" class="table table-bordered" >
<thead >
<tr>
<th>Order #</th>
<th>Customer Name </th>
<th>Items</th>
<th>Note</th>
<th>Qtt.</th>
<th>Total Price</th>
<th>Shipping Address</th>
<th>Status</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
You can try this as well. I use this and Working fine
<ul>
<?php
foreach($groups as $group)
{
?>
<li value="<?php echo $group['id']; ?>"><?php echo $group['firstname']; ?></li>
<?php
}
?>
</ul>
in your model change this $query = $this->db->query('SELECT firstname FROM tblcustomer'); to this $query = $this->db->query('SELECT * FROM tblcustomer');

Delete multiple rows by ticking Checkbox

How do I delete multiple rows by checking a checkbox?
I have a table of records, and I'd like to to delete multiple rows at once.
This is my my deleteselected function:
UPDATED
public function order() {
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
and in my view.
<?php
echo $this->Form->create('Order',array('url' => array('controller' => 'admin', 'action' => 'order'))); ?>
<table class="table table-bordered table-hover">
<th>Name</th>
<th>Email</th>
<th>phone</th>
<th>view</th>
<th>delete</th>
<thead>
</tr>
</thead>
<tbody>
<?php foreach ($order as $orders): ?>
<tr>
<td><?php echo $orders['Order']['varfullname']; ?></td>
<td><?php echo $orders['Order']['varemailid']; ?></td>
<td><?php echo $orders['Order']['varphone']; ?></td>
<td>
<?php echo $this->Html->link('',
array('controller' => 'orders', 'action' => 'view', $orders['Order']['id']), array('title'=>'VIEW','class' => 'fa fa-search'));?>
</td>
<td><input type="checkbox" name="order_id[]" value ="<?php echo $orders['Order']['id'];?>" hiddenField = "false">
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</br>
<?
echo $this->Form->end('Deleteselected'); ?>
</div>
</div>
</br>
</div>
<!-- /.row -->
after using below answer my delete selected working fine
Use in ctp
//in ctp Along with EACH record use Checkbox integration . Dont use Checkbox code u used.
<input type="checkbox" name="order_id[]" value ="<?php echo $referenceSingle['Order']['id'];?>" hiddenField = "false">
//In controller
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
UPDATE
Use Above Code in your Index method itself ,Dont create deleteAll method.
OR
Make <form action TO deleteAll
LIKE
echo $this->Form->create('searchCourse',array('url' => array('controller' => 'Courses', 'action' => 'index')));
UPDATE
For proper Workflow refer :
ALWAYS do paperwork and Algorithm ,Before Coding SIR.

Cakephp parameters not passing

I've spent a bunch of time trying to figure this problem out and had zero luck. I have tried routing with no success. I'll put the logic first then the code.
Users are in /pra/Fields/view/1 looking at a list of fields where fields.template_id=1
Users click add new field /pra/Fields/add_new/1 they are brought to a form where they enter the information they want to
about the new field they create.
when a user clicks on add/submit, the new field is saved to the database
The user is taken back to /pra/Fields/view/1 so they can see the new field added to the template
currently the first 3 steps are happening, however when it comes to redirecting back to /pra/Fields/view/1 The user is being redirected to /pra/Fields/view/
here is the code for the add_new function
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$name=$this->Field->field('template_id', array('template_id'=>$id));
//if the field data saves
$this->set('name',$name);
if(($this->Field->save($this->data)))
{
//user is redirected to fields/view/$name
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
}
//sets the $id variable
$this->set('id',$id);
here is the add_new view
<table id="formatform">
</br></br>
<?php
$options = array('Money'=>'Money','Text'=>'Text','Description'=>'Description');
?>
<?php echo $this->Form->create('Field', array('action'=>'add_new')); ?>
<?php echo $this->Form->input('id',array('type'=>'hidden')); ?>
<tr>
<td></td>
<td><?php echo $this->Form->input('template_id',array('type'=>'hidden','default' => $id)); ?></td>
</tr>
<tr>
<td align='center'>Field Name:</td>
<td align='left'><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Default Value:</td>
<td align='left'><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Field Type:</td>
<td align='left'><?php echo $this->Form->input('field_type', array('label'=>false,'type'=>'select','options'=>$options)); ?></td>
</tr>
<tr>
<td align='center'>Description:</td>
<td align='left'><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('active', array('type'=>'hidden','default'=>true)); ?></td>
</tr>
<td></td> <td align = "left"><?php echo $this->Form->end('Submit');?></td>
and here is the view function code
function view($name){
//lists information about fields that correspond to the
//template they have clicked 'view' on
$this->set('title_for_layout', 'Field Details');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
//sets $conditions where template_id=$name and field.active=true
$conditions=array(
"AND"=>array(
'template_id'=> $name,
'Field.active'=>true));
//finds all fields where 'conditions'=$conditions
$fields = $this->Template->Field->find('all',array(
'conditions' => $conditions));
//sets all the variables
$this->set('conditions', $conditions);
$this->set('field', $fields);
$this->set('field', $this->paginate('Field', $conditions));
}
It might happen that you have an issue here:
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
Check if $name is not empty
$name = $this->Field->field('template_id', array('template_id'=>$id));
if(!empty($name)){
$this->set('name',$name);
if(($this->Field->save($this->data)))
{
//user is redirected to fields/view/$name
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
}
}
the rest of your code
//sets the $id variable
$this->set('id',$id);
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$this->set('id',$id);
if(($this->Field->save($this->data)))
{
$id = $this->data['Field']['template_id'];
$this->set('id',$id);
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$id));
}
}

Resources