Saving data from a drop down list in CodeIgniter - codeigniter

I created a menu page where it has a drop down menu with a list of menus from the database and it also has a textbox to enter new menus.
The problem I'm having is that I can't seem to figure out how to save my dropdown. So for example I have a menu called "About Us" in the drop down list and I want to create a new menu called "Team", and "Team" is a child of "About Us"
So in my table I would have something like this
id | parent | title
------------------------
1 | NULL | About Us
2 | 1 | Team
Menu Controller
function get_data_from_post()
{
$data['title'] = $this->input->post('title', TRUE);
$data['parent'] = $this->input->post('parent', TRUE);
if(!isset($data)){
$data = '';
}
return $data;
}
function get_data_from_db($update_id)
{
$query = $this->get_where($update_id);
foreach($query->result() as $row){
$data['title'] = $row->title;
$data['parent'] = $row->parent;
}
return $data;
}
function create()
{
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
if($submit == "Submit"){
//person has submitted the form
$data = $this->get_data_from_post();
}else{
if(is_numeric($update_id)){
$data = $this->get_data_from_db($update_id);
}
}
if(!isset($data)){
$data = $this->get_data_from_post();
}
//$titles = array();
$query = $this->get('title');
foreach($query->result() as $row){
$titles[] = $row->title;
}
$data['titles'] = $titles;
$data['update_id'] = $update_id;
$data['view_file'] = "create";
$this->load->module('templates');
$this->templates->admin_template($data);
}
function submit()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
if($this->form_validation->run($this) == FALSE){
$this->create();
}else{
$data = $this->get_data_from_post();
$update_id = $this->uri->segment(3);
if(is_numeric($update_id)){
$this->_update($update_id, $data);
}else{
$this->_insert($data);
}
redirect('menus/manage');
}
}
create.php view
<div class="row">
<div class="col-md-12">
<h2>Create Menus</h2>
<h5>Welcome Jhon Deo , Need to make dynamic. </h5>
</div>
</div>
<hr />
<?php
echo validation_errors("<p style='color: red;'>", "</p>");
echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<select name="menus">
<?php
foreach($titles as $title){
echo "<option value=".$title.">".$title."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<!-- <input class="form-control" /> -->
<?php
$data = array(
'name' => 'title',
'id' => 'title',
'value' => $title,
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
<?php
$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Submit',
'class' => 'btn btn-success',
'style' => 'width: 100%',
);
echo form_submit($data);
?>
</form>
</div>
</div>
<?php
echo form_close();
?>
UPDATE:
this is what I have when I print_r($titles)
Array
(
[0] => About Us
[1] => Home
)
If there is anything you don't understand or if you need me to give more information please let me know.

You should have declared a model. From there, you can create a function that will save the values in the database that you initialize via controller. You should utilize the MVC pattern of it. CodeIgniter has a great documentation to read about what I am pointing out.. https://codeigniter.com/user_guide/overview/mvc.html?highlight=model

Related

I can not validate form with multi feild by variable name in laravel 5.4

I am using Laravel 5.4
my create form is multi feild for multi language. name of field in my form is variable:
{!! Form::open(['route' => 'pages.store','files'=>true]) !!}
#if( isset($languages) && $languages->count() > 0 )
#foreach($languages as $language)
<div class="form-group">
{!! Form::label('subject_'.$language->code, 'subject in '.$language->name) !!}
<div class="form-line">
{!! Form::text('subject_'.$language->code,old('subject'),['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
<!-- TinyMCE -->
{!! Form::textarea('content_'.$language->code,'',['class'=>'tinymce']) !!}
<!-- #END# TinyMCE -->
</div>
#if (!$loop->last)
<hr class="style18">
#endif
#endforeach
<div class="form-group">
{!! Form::submit('save change',['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
and my control code:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'subject' => 'required|max:255',
'content' => 'required|max:255',
]);
$languages = Languages::all();
$page = new Page();
$page->save(); // Eloquent
foreach ($languages as $language) {
$pageTrans = new PageTrans();
$pageTrans['page_id']= $page->id;
$pageTrans['locale'] = $language->locale;
$pageTrans['subject'] = $request->input('subject_' . $language->code);
$pageTrans['content'] = $request->input('content_' . $language->code);
$pageTrans->save(); // Eloquent
}
return redirect(route('pages.index'));
}
but problem with validat and not detect feild name. Do you know of a solution or a better way to do this?
You can change your controller same this. I didn't test. Please let me know if have any problem on that.
$languages = Languages::all();
if( $languages->count() ){
$data = [];
foreach ($languages as $language) {
$data['subject_'.$language->code] = 'required|max:255';
$data['content_'.$language->code] = 'required';
}
$validator = Validator::make($request->all(), $data);
if ($validator->fails()) {
return redirect(route('pages.store'))
->withErrors($validator)
->withInput();
}else{
$page = new Page();
$page->save(); // Eloquent
foreach ($languages as $language) {
$pageTrans = new PageTrans();
$pageTrans['page_id']= $page->id;
$pageTrans['locale'] = $language->locale;
$pageTrans['subject'] = $request->input('subject_' . $language->code);
$pageTrans['content'] = $request->input('content_' . $language->code);
$pageTrans->save(); // Eloquent
}
return redirect(route('pages.index'));
}
}
Thank you!

CodeIgniter insert into two tables

This is my model and i have problem when i go create "pjesma", i choose "izvodjac" in the form, and how to implement this with codeigniter3, i have to insert values into "izvodi_pjesmu", from where do i get "pjesma_id" that i am just inserting, i hope its clear, if you see the EER diagram below you will understand i hope. Thank you.
diagram
controller method:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci')
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$this->pjesma_model->kreiraj($data1, $data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
model method:
public function kreiraj($data1, $data2)
{
$this->db->insert('izvodi_pjesmu', $data2);
$this->db->insert('pjesma', $data1);
}
view:
<h1>Kreiranje pjesme</h1>
<hr>
<div class="row">
<div class="col"></div>
<div class="col-9">
<?php if (validation_errors()): ?>
<div class="alert alert-info">
<?php echo validation_errors(); ?>
</div>
<?php endif; ?>
<?php echo form_open('pjesma/kreiraj'); ?>
<div class="form-group">
<label for="izvodjaci">Izvođač:</label>
<select class="form-control" name="izvodjaci" id="izvodjaci">
<option value="0">odabir izvođača</option>
<?php foreach($izvodjaci as $izvodjac): ?>
<option value="<?php echo $izvodjac->izvodjac_id; ?>"><?php echo $izvodjac->naziv; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="naslov">Naslov:</label>
<input type="text" class="form-control" id="naslov" name="naslov" value="<?php echo set_value('naslov'); ?>" />
</div>
<div class="form-group">
<label for="tablatura">Tablatura:</label>
<textarea class="form-control" id="tablatura" name="tablatura"></textarea>
</div>
<button type="submit" class="btn btn-default" name="kreiraj">Kreiraj pjesmu</button>
<?php echo form_close(); ?>
</div>
<div class="col"></div>
Try this:
Controller:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$id= $this->pjesma_model->kreiraj($data1);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci'),
'pjesma_pjesma_id' => $id
);
$this->pjesma_model->pjesma_pjesmu($data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
Model:
public function kreiraj($data1)
{
$this->db->insert('pjesma', $data1);
return $this->db->insert_id();
}
public function pjesma_pjesmu($data)
{
$this->db->insert('izvodi_pjesmu', $data);
return $this->db->insert_id();
}
You can use $this->db->insert_id();
Which will return the last inserted id. Add this line after inserting data in pjesma table which will return 'pjesma_id'. Then you can use it.
Like this -
public function kreiraj($data1, $data2)
{
$this->db->insert('pjesma', $data1);
// ge tlast inserted id
$pjesma_id = $this->db->insert_id();
$izvodjac_izvodjac_id = $data2['izvodjac_izvodjac_id'];
$data = array( 'pjesma_id' => $pjesma_id,
'izvodjac_izvodjac_id' => $izvodjac_izvodjac_id );
$this->db->insert('izvodi_pjesmu', $data);
}
You can do this thing like this, may be this will helps you,
$insert = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->db->insert('tabl1', $insert);
$output["insert_id"] = $this->db->insert_id();
if (isset($output["insert_id"])) {
$insert = array(
'field1' => $value1,
'field2' => $value2,
);
$this->db->insert('tabl2', $insert);
}

Load table from database to dropdown CodeIgniter

I have trouble with loading the table to the form_dropdown. I have tried any solution from the same trouble with me but its not working.
Here's the controller code:
<?php
class Registrasi extends Superuser_Controller
{
public $data = array(
'halaman' => 'registrasi',
'main_view' => 'program/administrasi/registrasi_list',
'title' => 'Data Registrasi',
);
public function __construct()
{
parent::__construct();
$this->load->model('program/administrasi/Registrasi_model', 'registrasi_model');
}
public function index()
{
$registrasi = $this->registrasi_model->get_all_registrasi_data();
$this->data['registrasiData'] = $registrasi;
$this->load->view($this->layout, $this->data);
}
public function tambah()
{
$this->data['namaNegara'] = $this->registrasi_model->get_nama_negara();
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
}
Here's the model code:
<?php
class Registrasi_model extends MY_Model
{
protected $_tabel = 'tb_registrasi';
protected $form_rules = array(
array(
'field' => 'Negara_Tujuan',
'label' => 'Negara Tujuan',
'rules' => 'trim|xss_clean|required|max_length[50]'
)
);
public $default_value = array(
'Negara_Tujuan' => ''
);
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
return $query->result();
}
}
Here's the view code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>
The trouble I'm having is: Invalid argument supplied for foreach()
Your controller code must be :-
public function tambah()
{
$namaNegara[''] = 'select a option';
$namaNegaras = $this->registrasi_model->get_nama_negara();
foreach($namaNegaras as $namaNegaranew)
{
$namaNegara[$namaNegaranew->id] = $namaNegaranew->Nama_Negara ;
}
$this->data['namaNegara'] = $namaNegara;
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
And in view in place of
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
Simply use
<?php echo form_dropdown('NegaraTujuan', $namaNegara , set_value('NegaraTujuan')); ?>
I think i have the answer for my problem:
Controller code (still same, no change whatsoever)
Model code: change the function into
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
$dropdowns = $query->result();
foreach ($dropdowns as $dropdown)
{
$dropdownlist[$dropdown->Nama_Negara] = $dropdown->Nama_Negara;
}
$finaldropdown = $dropdownlist;
return $finaldropdown;
}
View code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php echo form_dropdown('NegaraTujuan', $namaNegara, set_value('NegaraTujuan'), 'class="dropdown"'); ?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>

How do I get domPDF to display my codeigniter view correctly

I feel there is a small step that I am missing that apparently everyone on the other related questions understands.
I have created a simple CI 2 view, controller and model, shown below:
I have installed dompdf into the helpers folder like so:
applications/helpers/dompdf
applications/helpers/dompdf/dompdf_help.php
What I want to happen is when user clicks the submit button on the view page, send form data to the db, then get a pdf of that filled in form.
Between getting underdefined var errors or nothing at all, except for the data going to db, I can't see what I am missing.
Could some please guide me? What am I not getting here?
View
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test pdf</title>
</head>
<body>
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open('quicksubmit', $attributes); ?>
<p>
<label for="title">Title <span class="required">*</span></label>
<?php echo form_error('title'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Mrs' => 'Mrs',
'Miss' => 'Miss',
'Ms' => 'Ms',
'Mr' => 'Mr',
); ?>
<br /><?php echo form_dropdown('title', $options, set_value('title'))?>
</p>
<p>
<label for="first_name">First Name</label>
<?php echo form_error('first_name'); ?>
<br /><input id="first_name" type="text" name="first_name" maxlength="100" value="<?php echo set_value('first_name'); ?>" />
</p>
<p>
<label for="last_name">Last Name <span class="required">*</span></label>
<?php echo form_error('last_name'); ?>
<br /><input id="last_name" type="text" name="last_name" maxlength="100" value="<?php echo set_value('last_name'); ?>" />
</p>
<p>
<label for="branch">Branch</label>
<?php echo form_error('branch'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Branch 1' => 'Branch One',
'Branch 2' => 'Branch Two',
); ?>
<br /><?php echo form_dropdown('branch', $options, set_value('branch'))?>
</p>
<p>
<label for="zip">Zip</label>
<?php echo form_error('zip'); ?>
<br /><input id="zip" type="text" name="zip" maxlength="7" value="<?php echo set_value('zip'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
</body>
</html>
Controller
<?php
class Quicksubmit extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('quicksubmit_model');
}
function index()
{
$this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('quicksubmit_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$this->pdf($output);
$form_data = array(
'title' => set_value('title'),
'first_name' => set_value('first_name'),
'last_name' => set_value('last_name'),
'branch' => set_value('branch'),
'zip' => set_value('zip')
);
// run insert model to write data to db
if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('quicksubmit/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
redirect(base_url(),'refresh');
/*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';*/
}
function pdf()
{
$this->load->helper(array('dompdf', 'file'));
// page info here, db calls, etc.
$html = $this->load->view('quicksubmit_view', $data, true);
pdf_create($html, 'filename');
/*or
$data = pdf_create($html, '', false);
write_file('name', $data);*/
//if you want to write it to disk and/or send it as an attachment
}
}
?>
Model
<?php
class Quicksubmit_model extends CI_Model {
function __construct()
{
parent::__construct();
}
// --------------------------------------------------------------------
/**
* function SaveForm()
*
* insert form data
* #param $form_data - array
* #return Bool - TRUE or FALSE
*/
function SaveForm($form_data)
{
$this->db->insert('quicksubmit', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
}
?>
dompdf_help.php file
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf");
} else {
return $dompdf->output();
}
}
?>
you were nearly there!
It is probably better to store dompdf in the third_party folder, and it is not a code igniter helper. - see the path i store it in in the constructor. Then it is always available.
Also, it is probably better to do the 'work' of the program in the model, so this includes making PDFs etc.
don't use a ?> at the end of your code.
i modded your code to work, and verified it did work. it simply saves a file named tmp/name.pdf. I am sure you can work out the rest. i did comment out the database loader because that wasn't needed for me to test the code.
see enc.
<?php
class Quicksubmit extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
//$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('file');
$this->load->model('quicksubmit_model');
global $_dompdf_show_warnings;global $_dompdf_debug;global $_DOMPDF_DEBUG_TYPES;global $_dompdf_warnings;$_dompdf_show_warnings = FALSE;
require_once(realpath(APPPATH."third_party/dompdf")."/dompdf_config.inc.php"); // remember that the constant DOMPDF_TEMP_DIR may need to be changed.
spl_autoload_register('DOMPDF_autoload');
}
function index()
{
$this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('quicksubmit_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$form_data = array(
'title' => set_value('title'),
'first_name' => set_value('first_name'),
'last_name' => set_value('last_name'),
'branch' => set_value('branch'),
'zip' => set_value('zip')
);
$this->pdf($form_data);
// run insert model to write data to db
if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('quicksubmit/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
redirect(base_url(),'refresh');
/*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';*/
}
function pdf($data)
{
$dompdf = new DOMPDF();
$html = $this->load->view('quicksubmit_view', $data, true);
$dompdf->set_paper('a4','portrait');
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
write_file('tmp/name.pdf', $pdf);
}
}

Ajax search and CGridView

I'm trying to implement an ajax search in my CGridView and I'm not having much luck getting it to work.
My Grid:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'talent-grid',
'dataProvider'=>$model->searchTalent(),
'hideHeader'=>true,
'template' => '{pager}{items}{pager}',
'pager'=>array('cssFile'=>'/css/pager.css','header' => '',),
'cssFile'=>'/css/client-grid.css',
'columns'=>array(
array(
'name'=>'talent_id',
'type'=>'raw',
'value'=>'$data->getTalentGridRow($data)',
),
),
)); ?>
Search form:
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<div class="row">
<?php echo $form->label($model,'full_name',array('class'=>'inline')); ?>
<?php echo $form->textField($model,'full_name',array('size'=>60,'maxlength'=>64)); ?>
</div>
<div class="row">
<?php echo $form->label($model,'gender_id',array('class'=>'inline')); ?>
<?php echo $form->checkBoxList($model, 'gender_id',CHtml::listData(Gender::model()->findAll(), 'gender_id', 'name'),array('separator'=>' ')); ?>
<?php echo $form->error($model,'gender_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
The search model:
public function searchTalent() {
$criteria=new CDbCriteria;
$criteria->compare('full_name',$this->full_name,true);
if ($this->gender_id != "") {
$criteria->compare('gender_id',$this->gender_id);
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>30,
),
));
}
The javascript:
Yii::app()->clientScript->registerScript('searchTalent', "
$('#search-form form').submit(function(){
$.fn.yiiGridView.update('talent-list', {
data: $(this).serialize()
});
return false;
});
");
The controller:
public function actionClients() {
$model = new Talent('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Talent'])) {
$model->attributes = $_GET['Talent'];
}
$this->render('clients', array(
'model' => $model,
'pages' => 10
));
}
the js submit fires, but the grid doesn't get updated. Not sure why.
You have specified the id of gridview in js as talent-list but the original id is talent-grid as specified in widget initialization call . So change the line as
Yii::app()->clientScript->registerScript('searchTalent', "
$('#search-form form').submit(function(){
$.fn.yiiGridView.update('talent-grid', {
data: $(this).serialize()
});
return false;
});
");

Resources