I am new to Codeigniter and want to export data present in my MYSQL database into PDF file using MPDF. The code is as follows:
View:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Export PDF</title>
</head>
<body>
<div id="container">
<h4>Member Data</h4>
<table border="1">
<tr>
<th>group_id</th>
<th>group_name</th>
<th>Archieved</th>
</tr>
<?php
foreach ($member as $rows) {
echo $rows['group_id'];
?>
<tr>
<td><?php echo $rows['group_id'] ?></td>
<td><?php echo $rows['group_name']?></td>
<td><?php echo $rows['archieved'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<br> <br>
<a href='<?php echo base_url(); ?>index.php/member_con/topdf'><span style='color:green;'>Export to Pdf</span></a>
</div>
<?php
?>
</body>
</html>
Controller:
<?php
class Member_con extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('member_model');
$this->load->helper('url');
$this->load->library('mpdf');
}
public function index() {
$data['member'] = $this->member_model->alldata();
$this->load->view('member_view', $data);
}
function topdf() {
$this->mpdf->useOnlyCoreFonts = true;
$filename = "VISH";
$data['member'] = $this->member_model->alldata();
$html = $this->load->view('member_view', $data['member'], true);
$this->mpdf->setTitle('Posts');
$this->mpdf->writeHTML($html);
$this->mpdf->output($filename, 'D');
}
}
?>
Model:
<?php
class Member_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function Member_Model() {
parent::Model();
}
function alldata()
{
$this->db->select('*');
$this->db->from('groups');
$this->db->order_by('group_id','ASC');
$getData = $this->db->get();
if($getData->num_rows() > 0)
return $getData->result_array();
else return null;
}
}
?>
with this code, it is giving me a blank PDF file, with only text as 'Member Data' and 'Export as pdf'. I have checked whether it is passing data to view, and yes it is doing so.
But don't know what is the matter with 'foreach' loop. I is printing everything outside 'foreach' loop, but bot the data members. Can anyone please let me know what should I do?
Thanks in advance....
Got the answer. In controller, instead of
$html = $this->load->view('member_view', $data['member'], true);
I used following:
$html = $this->load->view('member_view', $data, true);
Related
i don't understand why this is happening but same row on every page reload shows. I'm new to codeigniter and know basics of php and programming. Thank you for help in advance.
model:
class Clanovi_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_clanovi($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('clan');
return $query->result();
}
public function broji_clanove() {
return $this->db->count_all('clan');
}
}
controller:
class Clanovi extends CI_Controller {
public function index() {
$this->load->model('Clanovi_model');
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = 'http://localhost/codeigniter5/index.php/clanovi';
$config['total_rows'] = $this->Clanovi_model->broji_clanove();
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(3));
$data['paginacija'] = $this->pagination->create_links();
$this->load->view('zaglavlje');
$this->load->view('clanovi', $data);
$this->load->view('podnozje');
}
}
view:
<h2>Članovi</h2>
<hr>
<table class="table table-striped">
<tr>
<th>Ime</th>
<th>Prezime</th>
<th>Adresa</th>
<th>Korisničko ime</th>
<th>Lozinka</th>
</tr>
<?php foreach ($clanovi as $clan): ?>
<tr>
<td><?php echo $clan->ime; ?></td>
<td><?php echo $clan->prezime; ?></td>
<td><?php echo $clan->adresa; ?></td>
<td><?php echo $clan->korisnicko_ime; ?></td>
<td><?php echo $clan->lozinka; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $paginacija; ?>
routes:
$route['clanovi/(:num)'] = 'clanovi/index/$1';
$route['clanovi'] = 'clanovi';
screenshot1
screenshot2
Found solution by looking what $this->uri->segment(3); does, and changed value to 2.
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(2));
controller
car.php
<?php
class Car extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('company_model');
}
public function index()
{
//starts by running the query for the countries
//dropdown
$data['companydrop'] = $this->company_model->company();
//loads up the view with the query results
$this->load->view('car_view', $data);
}
//call to fill the second dropdown with the cities
public function car_model()
{
//set selected country id from POST
echo $company_id = $this->input->post('company_id',TRUE);
//run the query for the cities we specified earlier
$cardata['cardrop']=$this->company_model->car($company_id);
print_r($cardata);
$output = null;
foreach ($cardata['cardrop'] as $row)
{
//here we build a dropdown item line for each
// query result
$output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";
}
echo $output;
}
}
?>
model
company_model
<?php
class Company_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
//fill your contry dropdown
public function company()
{
$this->db->select('company_id,company_name');
$this->db->from('company');
$query = $this->db->get();
// the query mean select cat_id,category from
//category
foreach($query->result_array() as $row){
$data[$row['company_id']]=$row['company_name'];
}
// the fetching data from database is return
return $data;
}
//fill your cities dropdown depending on the selected city
public function car($company_id=string)
{
$this->db->select('car_id,car_model');
$this->db->from('car');
$this->db->where('company',$company_id);
$query = $this->db->get();
return $query->result();
}
}
?>
view
car_view
<html>
<head>
<title>car dealers</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#companydrop").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo base_url();?>index.php/car/car_model",
data: {id:$(this).val()},
type: "POST",
success:function(data){
$("#cardrop").html(data);
alert(data);
}
});
});
});
</script>
<style>
body{
no-repeat;
background:url(../../../video-fallback-background.jpg)
}
</style>
</head>
<body>
<!--company dropdown-->
<?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?>
<br />
<br />
<!--car dropdown-->
<select name="cardrop" id="cardrop">
<option value="">Select</option>
</select>
<br />
</body>
</html>
dynamic dropdown is not working as the first select which is the
company name is working as it is fetched from database,but car model is not working,it not fetched to the dropdown.i need to fetch the car company model from database and then after selecting the company the model of that specified company has to be listed in the second dropdown.i have created database in phpmyadmin and created two table car and company,in company copany_id and company_name where as in car has car_id,car_name and company_id
Check this sample code for creating dropdown in codeigniter.
<?php
$js = 'id="unicode" class="form-control"';
$unicode = array(
'2' => 'No',
'1' => 'Yes'
);
echo form_dropdown('unicode', $unicode, set_value('unicode'), $js);
?>
Here Dropdown id is unicode,class is form-control.
Html will look like :
<select name="unicode" id="unicode" class="form-control">
<option value="2">No</option>
<option value="1">Yes</option>
</select>
You can get you values from db in an array and then store it in a variable like $unicode.Hope this helps.Check this ref link
For setting another dropdown based on first dropdown:
$("#dropdown1").change(function () {
var end = this.value;
$('#dropdown2').val(end );
});
In Your Car Controller Please remove print_r($cardata); first.
Then see in your console what response you are getting from the call. I suggest you to get data in json format and parse it on client end. It is the best practice.
i corrected the code and finally it worked,i will post the correct code, if it helps anyone in future.thanks to everyone who tried to help me..
controller
car.php
<?php
class Car extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('company_model');
}
public function index()
{
$data['companydrop'] = $this->company_model->company();
$this->load->view('car_view', $data);
}
public function car_model()
{
$company_id = $this->input->post('company_id',TRUE);
$cardata['cardrop']=$this->company_model->car($company_id);
$output = null;
foreach ($cardata['cardrop'] as $row)
{
$output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";
}
echo $output;
}
}
?>
model
company_model
<?php
class Company_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function company()
{
$this->db->select('company_id,company_name');
$this->db->from('company');
$query = $this->db->get();
foreach($query->result_array() as $row){
$data[$row['company_id']]=$row['company_name'];
}
return $data;
}
public function car($company_id)
{
$this->db->select('car_id,car_model');
$this->db->from('car');
$this->db->where('company_id',$company_id);
$query = $this->db->get();
return $query->result();
}
}
?>
view
car_view
<html>
<head>
<title>car dealers</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#companydrop").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo base_url();?>index.php/car/car_model",
data: {company_id:$(this).val()},
type: "POST",
success:function(data){
$('#cardrop option[value!=0]').remove()
$("#cardrop").append(data);
}
});
});
});
</script>
<style>
body{
no-repeat;
background:url(../../../video-fallback-background.jpg)
}
</style>
</head>
<body>
<center><font color="#333366"><strong></strong><h2>CR Motors</h2></font></center>
<center><font color="#FF8000"><h3>Select the car to purchase...</h3></center></font>
<!--company dropdown-->
<tr>
<td>
<font color="#00FF99">
Select the company</font>
<?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?> </td>
</tr>
<br />
<br />
<!--car dropdown-->
<tr>
<td>
<font color="#00FF99">
Select the model</font>
<select name="cardrop" id="cardrop">
<option value="0">Select</option>
</select>
</td>
</tr>
<br />
</body>
</html>
i want to upload the image profile and the user id from my android application to database. i tried this method and it work in local but when i test it with (POSTMAN) a rest client it doesn't work. i think because i call a view.
this is my code please help me.
my controller
class Users extends REST_Controller {
function __construct()
{ parent::__construct();
$this->load->model('Users_model','',TRUE);}public function index_get()
{
$this->load->view('users');
}
public function save_image_post()
{
$id = $_POST["id"];
$url = $this->do_upload();
$this->Users_model->save_image($id, $url);
//$this->Users_model->save_image($url);
}
private function do_upload()
{
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
return "";
}
}
this is my model:
public function save_image($id,$url)
{
$this->db->set('id', $id);
$this->db->set('image_user', $url);
$this->db->insert('users');
}
this is my view:
<!DOCTYPE html><html lang="en"><head></head><body>
<?php echo form_open_multipart('api/users/save_image/'); ?>
<table class="table">
<tr>
<td>Id</td>
<td><?php echo form_input('id'); ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo form_upload('pic'); ?></td>
</tr>
<tr><td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table></body></html>
I'm newbie for grocery crud. I'm having a database table lets say customer_details, it includes customer_name & customer_id etc columns. I'm having another table called purches_details, customer_id is the foriegn key for this table.
I want to create a tab panel (menu) in my view according to the customer_name column in customer_detail table. tabs names should be customer names
& when some one click the relevent customer_name tab the the relevent purches details should show as the grocery crud grid.
Please help me friends, I want it so deeply.
Thanks.
I got this answer ...
the controller from grocery CRUD 1.2.3 with CodeIgniter 2.1.2 (! try to use CI 2.1.2 or CI 2.1.0 versions + latest grocery CRUD):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Examples extends CI_Controller { public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->library('grocery_crud');
}
public function _example_output($output = null)
{
$this->load->view('example', $output);
} public function index()
{
$this->_example_output( (object) array('output' => '', 'js_files' => array(), 'css_files' => array()));
}
public function customers()
{
$crud = new grocery_crud();
$crud->set_table('customer_details');
$crud->set_subject('Customer Details');
$output = $crud->render();
$output->menu = $this->db->select('customer_id, customer_name')->get('customer_details')->result();
$this->_example_output($output);
} public function purches_details($customer)
{
$company = $this->uri->segment(3);
$crud = new grocery_crud();
$crud->set_table('purches_details');
$crud->set_subject('Purches Details');
$crud->where('customer_id', $customer);
$output = $crud->render();
$output->menu = $this->db->select('customer_id, customer_name')->get('customer_details')->result();
$this->_example_output($output);
}
}
and the view with our customer names links:
[CODE]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<?php
foreach($css_files as $file): ?>
<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($js_files as $file): ?>
<script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
<style type='text/css'>
body
{
font-family: Arial;
font-size: 14px;
}
a {
color: blue;
text-decoration: none;
font-size: 14px;
}
a:hover
{
text-decoration: underline;
}
</style>
</head>
<body>
<div>
<?php echo anchor('examples/customers', 'All Customers');?> :
<?php foreach ($menu as $link) {?>
<?php echo anchor("examples/purches_details/$link->customer_id", "$link->customer_name");?> ·
<?php }?>
</div>
<div style='height:20px;'></div>
<div>
<?php echo $output; ?>
</div>
</body>
</html>
HI
i want to insert data using form in CI but i am facing the problem
Here is my model code
function add_form() {
$this->load->database();
$id = $this->input->post('id');
$name = $this->input->post('name');
$age = $this->input->post('age');
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
$this->db->insert('user',$data);
}
Here is my controller code
function simpleform() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('welcomedb_model');
if( $this->input->post('submit') ) {
$this->welcomedb_model->add_form();
}
$this->load->view('welcomedb_view');
}
and here is my view code
<?php echo form_open('welcomedb/submit'); ?>
<? echo $name; ?>:
<? echo form_input('name'); ?>
</br>
<? echo $age; ?>:
<? echo form_input('age'); ?>
</br>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
Thanks for your help
Your form is submitting to welcomedb/submit, but your controller appears to be at welcomedb/simpleform... maybe you need to change that.
Otherwise, there doesn't appear to be anything wrong.
Probably:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
Remove comma from the last element (age) like this:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age')
);
and always dump error.
Best way is to do these code ......
First conifg the autoload and database.
into autoload:$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','form','file');
Into controller
<?php
class CDemo extends CI_Controller
{
function index()
{
$this->load->view('VDemo');
}
function save()
{
$this->load->model('MDemo');
if($this->input->post('submit'))
{
$this->MDemo->process();
}
redirect('CDemo');
}
}
?>
Into Model
<?php
class MDemo extends CI_Model
{
function process()
{
$Id = $this->input->post('Id');
$Name = $this->input->post('Name');
$data = array(
'Id'=>$Id,
'Name'=>$Name
);
$this->db->insert('test',$data);
}
}
?>
Into View
<html>
<head>
<title>DEMO</title>
</head>
<body>
<h1>Form Biodata</h1>
<?php
echo form_open('CDemo/save', array('name' => 'VDemo'));
?>
<table>
<tr>
<td>Id :</td>
<td><input type="text" name="Id"></input></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="Name"></input></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></input></td>
</tr>
</table>
<?php
echo form_close();
?>
<textarea rows="" cols="">Hello</textarea>
</body>
</html>