How to pass data to view and Load view into a variable - codeigniter

I would like to pass $data variable to print view then I want to pass result of print view to a variable ($myvariable ), I don't want to show anything to user :
$myvariable = $this->load->view('print' , $data,true);
but $myvariable is null and print view will load!
updated
function makePDF(){
$pdfFilePath = FCPATH."assests/gifts/test.pdf";
$data['page_title'] = 'Hello world'; // pass data to the view
if (file_exists($pdfFilePath) == FALSE)
{
ini_set('memory_limit','32M'); // boost the memory limit if it's low <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="😉" draggable="false" class="emoji">
$html = $this->printer(212486); // render the view into HTML
var_dump($html);
return;
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="😉" draggable="false" class="emoji">
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F'); // save to file because we can
}
}
function printer( $id = 0 ){
....
$data = array ('coupons'=>$coupons , 'shop'=>$shop , 'details'=>$parent );
return $this->view('print' , $data,true);
}

Don't use a return to get your view back, instead, adjust your functions to save the view into a variable and then return that variable. Also, when you say $myvariable is NULL, I assume you are referring to the $html variable in your UPDATED section?
Firstly, these lines will display your view just because you're dumping out written html and then exiting the function:
var_dump($html);
return;
So to clean up, I would suggest altering your functions like so:
function makePDF(){
$pdfFilePath = FCPATH."assests/gifts/test.pdf";
$data['page_title'] = 'Hello world'; // pass data to the view
if (file_exists($pdfFilePath) == FALSE)
{
ini_set('memory_limit','32M'); // boost the memory limit if it's low
$html = $this->printer(212486); // render the view into HTML
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output($pdfFilePath, 'F'); // save to file because we can
}
}
and...
function printer( $id = 0 ){
....
$data = array ('coupons'=>$coupons , 'shop'=>$shop , 'details'=>$parent );
$return_html = $this->load->view('print' , $data, true);
return $return_html;
}
The biggest difference here is ensuring the view syntax is written correctly from $this->view('print', $data, true); to $this->LOAD->view('print', $data, true);
Also, for good measure the view return was saved into a variable and returned as that object. I would step these changes one at a time because taking that second step may not be necessary.

Related

Creating a Dynamic PDF and Saving it in a folder using Loop with MYSQL and Codeigniter

This is the public function with looping (Edited)
public function generate_salary_slip(){
$this->load->helper('file');
$this->load->library('m_pdf');
$data = [];
$slip = $this->Admin_Login_Model->generate_salary_slip();
//$html = array();
$html='';
if(!empty($slip)){
foreach($slip as $slip_list){
$id = $slip_list->emp_id;
$data['attendance'] = $this->Admin_Login_Model->get_slip_attendance($id);
$data['emp_profile'] = $this->Admin_Login_Model->get_emp_details($id);
$data['late_day'] = $this->Admin_Login_Model->get_late_days($id);
$data['half_days'] = $this->Admin_Login_Model->get_half_days($id);
$data['salary'] = $this->Admin_Login_Model->get_salary_slip($id);
$data['second_half'] = $this->Admin_Login_Model->get_half_Second_days($id);
//$html=$this->load->view('admin/payslip',$data);
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html .= $this->load->view('admin/payslip', $data, true);
}
$htmlval = $html;
//echo $html;exit;
$this->m_pdf->pdf->WriteHTML($htmlval);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");
$this->session->set_flashdata('success','Salary slips saved in directory!');
redirect('admin_attendance_list');
}else{
$this->session->set_flashdata('message','Salary slips not available for this month!');
redirect('admin_attendance_list');
}
}
File is getting saved in the folder, but Only with the First Record. Also, Repeated loop is getting generated, but with same record.
How to create a HTML by loop.
PS: Using MPDF to generate PDF File
Any hep will be highly appreciated.
You need to take the render, writeHtml and Output calls outside the forech loop, there is you problem.
You could save all the data in a var and then render the HTML as you are doing now and then generate the PDF.
EDIT: Your code should look something like this (please read comments below):
$htmlData = array();
if(!empty($slip)){
foreach ($slip as $slip_list) {
//
$htmlData[] = $data;
}
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html = $this->load->view('admin/payslip', $htmlData, true);
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");

Codeigniter variable not found message in sub views

I have stacked with the situation , please anyone help me !
Controller ->
public function index() {
$data = array();
$data ['tittle'] = 'Membership Payment';
$data ['page'] = $this->load->view('membership_payment', $data, true);
$data ['breadcrumb'] = $this->load->view('breadcrumb', $data, true);
$data['result'] = $this->payment_model->membership_payment();
$data['db']='---------------';
$this->load->view('master', $data);
View->master page
<?php echo $db; ?>
Master pages working fine , but problem is i have subpages membership_payment , in this page the data array variable are not working ,
Display the variable not found , there is any trick to pass data array to master subpage
Thanks in Advance !
ROB
I'm assuming that you've used $result which is the returned data of membership_payment(); method in the membership_payment view file.
If so, just change the order of the following line and put that over the $data['page'], as follows:
// ...
$data['result'] = $this->payment_model->membership_payment();
$data ['page'] = $this->load->view('membership_payment', $data, true);
// ...

Kohana 3.2 + PHPExcel creates empty document when there are more than 31 rows written.

I'm trying to create a simple excel document that contains a 3-column list. (First Name, Last Name, Email Address)
When I output more than 31 rows, a blank excel file is created. Does anyone know how to fix it?
My code below:
$guests = ORM::factory('guest')
->order_by('last_name', 'ASC')
->find_all()
->as_array();
$columns = array('first_name', 'last_name', 'email');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator('Me')
->setLastModifiedBy('Me)
->setTitle('Guest List')
->setSubject('Guest List')
->setDescription('Title of Report, run on ' . date('m/d/Y H:i:s'));
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);
foreach($guests as $row => $guest) {
$source = array();
$column = 0;
$next_row = $objPHPExcel->getActiveSheet()->getHighestRow();
foreach($guest->as_array() as $column_name => $data) {
if(in_array($column_name, $columns)) {
$objPHPExcel->getActiveSheet()
->setCellValueByColumnAndRow($column, $next_row+1, $data);
$column++;
// unset($data);
}
}
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$this->response->body($objWriter->save('php://output'));
$this->response->send_file(TRUE, 'GuestList.xls');
Kohana has a hard time handling the rendered file using the send_file() method, so write the file to disk using the built-in method from PHPExcel. Then use Kohana to deliver the saved file as a download.
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($filename, 'EXCEL2007');
$this->response->send_file( DOCROOT.$filename );

How to output uploading errors as flashdata - CodeIgniter

I'm wanting to display any errors produced while uploading a file by redirecting the user back to the same page they used to upload the file. The thing is that I pre-populate some inputs on that page, so I can't use redirect(controller/method_to_load_page) as that wouldn't carry out the data population.
Using $this->load->view('add_page',$error); won't work either (the way that I'm using it), because although that will show the errors, it won't show the input fields with the pre-populated data.
This is the method in my controller which loads the form to upload a file and other fields:
public function add_products_page()
{
//get each categories subcategory and place them in their own variable
$data['categories1'] = $this->admin_model->getSubcategories('1');
$data['error'] = '';
$this->load->view('add_page',$data);
}
And from that page, the user can fill in the form and choose an image. The form will then be submitted to this method in the same controller:
public function add_book()
{
$id = $this->admin_model->add_book();
$config['file_name'] = $id;
$config['upload_path'] = './images/';
$config['allowed_types'] = 'jpg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_page', $error);
}
else
{
redirect('admin/add_products_page', 'refresh');
}
}
If there are no errors, the redirect to add_products_page() method is okay, however when there's an error, the view 'add_page' is loaded and the errors are shown, but my pre-populated field isn't populated anymore because the method to do that isn't being called.
So I really would like to know how I can both load my view by calling the method add_products_page to populate my input fields, and also show errors when they occur. Please if someone could help I would appreciate it greatly.
Why post to a different method and then redirect? You can handle it all in the same method.
public function add_products_page()
{
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
$result = $this->_add_book();
if ($result['is_error'])
{
// TODO: whatever it takes to show an error message
$data['error'] = $result['message'];
}
else
{
// TODO: whatever it takes to show a success message
$data['error'] = '';
}
}
//get each categories subcategory and place them in their own variable
$data['categories1'] = $this->admin_model->getSubcategories('1');
$this->load->view('add_page',$data);
}
public function _add_book() // add underscore so it's not directly accessible
{
$result = array(
'is_error' => TRUE,
'message' => '',
);
$id = $this->admin_model->add_book();
$config['file_name'] = $id;
$config['upload_path'] = './images/';
$config['allowed_types'] = 'jpg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
// set an error message
$result['message'] = $error;
}
else
{
// set a success message
$result['is_error'] = FALSE;
$result['message'] = 'SOME SUCCESS MSG';
}
return $result;
}

calling function from same controller not passing $data

I am building an HTML mobile app using Jquery Mobile. Its amazing so far.
However, In building my dynamnic template, I ran into a bit of a snag.
Heres my controller:
function index()
{
$data['page'] = "home";
$page['head'] = $this->load->view('template/head',$data,TRUE);
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
$page['nav'] = $this->nav($data['page']);
$this->load->view("pages/home", $page);
}
function nav($page)
{
$data['page'] = $page;
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$page,TRUE);
}
Regardless as to why I have it set up this way, any reason why, within the function nav($page) the view would return Undefined variable: header and Undefined variable: footer errors?
It's because you're trying to overwrite a string based variable with an array index.
You're passing in $page to the method, but then trying to create an array called $page to pass to the view. You need to rename the array to pass to the view:
function nav($page)
{
$data['page'] = $page;
// you should rename the array to pass to the view (from $page to $my_page)
$my_page['header'] = $this->load->view('template/header', $data, TRUE);
$my_page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$my_page,TRUE);
}

Resources