I Have form in that i am displaying data.When I click on the print button I am going to save the data in pdf format for printing for that I am using FPDF Library.The problem is when i send the data from the controller to view only headers displaying like below But Data is not displaying. I have written my view file like below.I have got stuck since yesterday.I Have getting the data in controller but not passing to the view.I can't understand why it is happening .The Way i am doing wrong or what please any help Would be Appreciated.Thanks in Advance.
My controller Code
public function printpage(){
$this->load->library('Myfpdf');
$data['result']=$this->Vendor_Model->displaydata();
if(!empty(array_filter($data))){
$this->load->view('Savedata',$data);
}else{
redirect('Vendorcontroller/showeditview');
}
}
My Modal code for getting the data from the database
public function displaydata(){
$query=$this->db->query('SELECT `vndr_id`, s.state as state,`vndr_name`, `vndr_address`, `vndr_pincode`, `vndr_telephone`, `vndr_mobile`, `vndr_mailid`, `vndr_country`, `vndr_gsttin`, `vndr_cstno`, `vndr_totaldebit`, `vndr_totalcredit`, `vndr_bankname`, `vndr_acno`, `vndr_ifsccode` FROM `vendors` vndr INNER JOIN states s ON vndr_state=s.state_id order by vndr_id');
if($query->num_rows()>0){
return $query->result_array();
}
}
My view code for print the data as pdf format
class printview extends FPDF
{
function Header()
{
$this->SetFont('Arial','B',15);
$this->Cell(276,5,"VendorDetails",0,0,'C');
$this->ln(15);
$this->SetFont('Arial','B',10);
$this->Cell(55,10,"Name",1,0,'C');
$this->Cell(90,10,"Address",1,0,'C');
$this->Cell(50,10,"Telephone",1,0,'C');
$this->Cell(70,10,"Email",1,0,'C');
$this->ln();
}
function Footer(){
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function viewdata(){
if(isset($result)){
foreach($result as $values){
$this->SetFont('Arial','',10);
$this->Cell(55,10,$values['vndr_name'],1,0,'L');
$this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L');
$this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L');
$this->Cell(70,10,$values['vndr_mailid'],1,0,'L');
$this->ln();
}
}
}
}
$this->pdf = new Printview();
$this->pdf->SetMargins(15, 10, 20);
$this->pdf->AliasNbPages();
$this->pdf->AddPage('L','A4',0);
$this->pdf->viewdata();
$this->pdf->Output();
This is Fdf library class
require('fpdf/fpdf.php');
class Myfpdf extends fpdf
{
function __construct(){
parent::__construct();
$ci=& get_instance();
}
}
Try to pass the $result data as a parameter :
$this->pdf->viewdata($result);
And also on the method :
function viewdata($result){
...
}
I Have changed this way it is working fine now.
This is controller
-------------------------
public function printpage(){
$this->load->library('Myfpdf');
$data['result']=$this->Vendor_Model->displaydata();
if(!empty(array_filter($data))){
$this->load->view('Savedata',$data);
}else{
redirect('Vendorcontroller/showeditview');
}
}
This is view file
function viewdata($result){
if(isset($result)){
foreach($result as $values){
$this->SetFont('Arial','',10);
$this->Cell(55,10,$values['vndr_name'],1,0,'L');
$this->Cell(90,10,$values['vndr_address'].','.$values['state'].'-'.$values['vndr_pincode'],1,0,'L');
$this->Cell(50,10,$values['vndr_telephone'].','.$values['vndr_mobile'],1,0,'L');
$this->Cell(70,10,$values['vndr_mailid'],1,0,'L');
$this->ln();
}
}
}
}
$this->pdf = new Printview();
$this->pdf->SetMargins(15, 10, 20);
$this->pdf->AliasNbPages();
$this->pdf->AddPage('L','A4',0);
$this->pdf->viewdata($result);
$this->pdf->Output();
Related
I'm stamping my PDF documents with FPDI and TCPDF functions, and I'm trying to figure it out, how can I add a line below the text in Header, and upon the text in footer. Here is my code:
<?php
require_once('lib/tcpdf/tcpdf.php');
require_once('fpdi.php');
$fullPathToFile = "TestClanek6.pdf";
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile; //global
if(is_null($this->_tplIdx)) {
// number of pages
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
if($this->page > 0) {
//$this->SetPrintHeader(true);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0);
$this->Write(15, "Vol. 1, No. 15, Year: 2015, duff");
$this->Image('logo.png', 100, 2, 75,7);
} //end of IF
$this->useTemplate($this->_tplIdx, 0, 0,200);
} //end of HEADER
function Footer() {
if($this->page > 0) {
$this->SetY(-20);
$this->SetFont('times', 'I', 11);
$this->SetTextColor(0,0,0);
$this->Write(0, "Page", '', 0, 'C');
} //end of if
} // end of footer
} //end of CLASS
// new PDF file
$pdf = new PDF();
$pdf->addPage();
if($pdf->numPages>0) {
for($i=1;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
//$pdf->SetPrintHeader(false);
//$pdf->SetPrintFooter(false);
}
}
$file_time = time();
$pdf->Output("$file_time.pdf", "F");//, "I");
echo "Link: '<a href=$file_time.pdf>Stamped article</a>'";
?>
I've tried a lot of things like setPrintHeader(), etc. but nothing what I've found works for me. Could i please somebody to help?
Thank you.
duff
You can use the Line method to draw a line in FPDF. If you want a straight horizontal line, just make sure the ordinates (y values) for the start and end of the line are the same. Something like this for example:
$pdf->Ln(15,$pdf->y,200,$pdf->y);
You would modify the values to suit your needs and insert it in the overridden methods for Header and Footer as appropriate for your application.
Better would be to leave the two methods (Header and Footer) empty. this way you would overwrite the drawing from super-class.
like this:
class EmptyFPDI extends FPDI
{
public function Header()
{
}
public function Footer()
{
}
}
I am using igniter datatables in my code for datatables with server side processing.My code is like :
public function datatable()
{
$this->datatables->select("
insight_worksheet.id,
insight_worksheet.workingDate,
insight_worksheet.reason,
...
insight_worksheet.worksheet_status
")
->from("insight_worksheet")
->edit_column('Actions', '$1', 'callback_test(insight_worksheet.id,insight_worksheet.worksheet_status)')
...
->join("insight_status","status.id=worksheet.status","left");
echo $this->datatables->generate();
}
public function test($id,$worksheetStatus){
return "srimanta";
}
in my view page for Actions column direct string callback_test(insight_worksheet.id,insight_worksheet.worksheet_status)
is showing instead of actual work whereas for other columns, showing exact data.
For time being I put two functions in my controller class.
Could you please let me know the issue in my code?
Thanks in advance.
Try adding the callback function test in a helper file.
Your model file:
public function datatable()
{
$this->datatables->select("
insight_worksheet.id,
insight_worksheet.workingDate,
insight_worksheet.reason,
...
insight_worksheet.worksheet_status
")
->from("insight_worksheet")
->edit_column('Actions', '$1', 'test(insight_worksheet.id,insight_worksheet.worksheet_status)')
...
->join("insight_status","status.id=worksheet.status","left");
echo $this->datatables->generate();
}
your helper file:
public function test($id,$worksheetStatus){
return "srimanta";
}
Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}
Codes:
View
<html>
<body>
<?php echo form_open('samplecontroller/sample'); ?>
<input type="text" name="samplename"/>
<input type="submit" value="go"/>
<?php echo form_close(); ?>
</body>
</html>
Controller
<?php
Class samplecontroller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Sample_insert');
}
function index()
{
$this->load->view('sample.php');
}
function show($data)
{
$this->load->view('sampleresult',$data);
}
function sample()
{
if($result = $this->Sample_insert->insert_into_sample())
{
$this->show($result);
}
}
}
?>
Model
<?php
Class Sample_insert extends CI_Model
{
function insert_into_sample()
{
$sample = array(
'samplename' => $this->input->post('samplename'),
);
$this->db->insert('sample', $sample);
$latest_id = $this->db->insert_id();
return $latest_id;
}
}
?>
The flow is that, i have 2 views. The other one contains only 1 text input, after which goes to controller then controller passes it on to model then model gets the value within the text input then inserts to my sample table.
The sample table has 2 columns, id(PK,AI) and name
after it inserts, on my model. i added a line, return $latest_id. Then passes goes back to my controller then passes it off to another view that only has
print_r($data);
After all that, it displays an error.
Message: Undefined variable: data
Im not sure where the flow went wrong or if i made a syntax mistake. If someone knows/expert on insert_id(), could you pin point where exactly i am wrong? anyways, ive made my research and been getting results on the web how buggy insert_id is. Im not sure if its true or not but ive been redirected mostly to forums wherein insert_id returns null and some say its a bug. Im hoping it isnt.
As per your comment, You have to do some changes in your controller.
function show($data)
{
$this->load->view('sampleresult',array("data"=>$data));
}
You can get inserted id in view in the name of $data.
For ex:
echo $data;
Edit: As per your comment in answer, For multiple row insert in model, add below code
$id_arr = array();
foreach($sample_data as $sample)
{
$this->db->insert('sample', $sample);
$id_arr[] = $this->db->insert_id();
}
return $id_arr;
Now in your view, you will get ids in $data
foreach($data as $id)
{
echo $id;
}
I'm trying to generate a pdf using the FPDF class but the problem I'm having is how to pass variable data fetched from mysql database (queried data are stored in an array) into the pdf generated.
Here are the codes in the script meant to generate the pdf
<?php
#include_once("includes/db.php");
require('fpdf/fpdf.php');
#include_once("includes/course_info_query.php");
$obj = new trainingCourses();
$course_details = array();
if(isset($_GET['c_id'])){
$course_details = $obj->getPubCourseDetails($_GET['c_id']);
}
class PDF extends FPDF
{
public $course_details;
public function setData($input){
$this->course_details = $input;
}
function Header()
{
// Logo
$this->Image('s_pdf_logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',20);
// Move to the right
$this->Cell(40);
// Title
$this->Cell(30,10,$this->course_details['comp_name']);
// Draw an header line
$this->Line(10,26,200,26);
// Line break
$this->Ln(20);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Begin with regular font
$this->SetFont('Arial','',9);
// Then put a blue underlined link
//$this->SetTextColor(0,0,255);
$this->SetFont('','U');
$this->Write(10,$this->course_details['comp_name'],'http://www.skills.com');
// Arial italic 8
$this->SetFont('Arial','I',9);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}
function BodyTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();
/* Build cells for the first row */
$this->SetFont('Arial','',10);
$this->SetY(40);
// First Row
$this->Cell(35,8,'Starting Date : ',0,0,'L');
$this->Cell(30,8,$this->course_details['event_date'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Course Fee : ',0,0,'L');
$this->Cell(20,8,$this->course_details['fee'],0,0,'C');
$this->Ln();
// Second Row
$this->Cell(35,8,'Seating Capacity : ',0,0,'L');
$this->Cell(30,8,$this->course_details['sit_capacity'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'Duration : ',0,0,'L');
$this->Cell(20,8,$this->course_details['duration'].' Day(s)',0,0,'L');
$this->Ln();
// Third Row
$this->Cell(35,8,'Venue : ',0,0,'L');
$this->Cell(30,8,$this->course_details['venue'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'City : ',0,0,'L');
$this->Cell(20,8,$this->course_details['city'],0,0,'L');
$this->Ln();
// Fourth Row
$this->Cell(35,8,'Other Details : ',0,0,'L');
$this->Cell(150,8,$this->course_details['other_det'],0,0,'L');
$this->Ln();
}
function CourseBody()
{
$this->SetY(80);
//$this->WriteHTML($html);
$this->Write(10,$this->course_details['desc']);
}
function PrintChapter()
{
$this->AddPage();
$this->BodyTop();
$this->CourseBody();
}
}
$pdf = new PDF();
$pdf->setData($course_details);
//$pdf->Header();
$pdf->SetAuthor($this->course_details['comp_name']);
$pdf->PrintChapter();
$pdf->Output();
?>
I hope to get some help with this...Thanks
It's just basic OOP (object oriented programming) really.
Add a variable to your PDF Class as such:
public $data;
Add a function within your PDF Class which accepts a parameter and use this function to set the variable above:
public function setData($input){
$this->data = $input;
}
And then you'll be able to access $this->data from within your PDF Class. Don't forget to actually call the function that you just defined (just after the constructor).
EDIT:
To set the data:
$pdf = new PDF();
$pdf->setData($course_details);
Then within your PDF class $this->data will be your array. You might want to do the following so you can understand the format of your array:
print_r($this->data);