how to pass multiple values onchange event of <select> in codeigniter - php

I want to show student from selected school, class and section. if I delete 'class_id' => $class_id, 'section_id' => $section_id, from controller then it show all student from selected school otherwise it show nothing. any solution please. where am wrong ?
This is file from where I have to select value
<div class="form-group">
<label class="col-sm-3 control-label" >School<span class="required">*</span></label>
<div class="col-sm-5">
<select name="school_id" id="school_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select School...</option>
<?php if (!empty($all_school_info)): foreach ($all_school_info as $v_school): ?>
<option value="<?php echo $v_school->school_id; ?>"
<?php if (!empty($all_student_complain_info->school_id)) {
echo $v_school->school_id == $all_student_complain_info->school_id ? 'selected ' : ''; } ?>>
<?php echo $v_school->school_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Class<span class="required">*</span></label>
<div class="col-sm-5">
<select name="class_id" id="class_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select Class...</option>
<?php if (!empty($all_classes_info)): foreach ($all_classes_info as $v_class): ?>
<option value="<?php echo $v_class->class_id; ?>"
<?php if (!empty($all_student_complain_info->class_id)) {
echo $v_class->class_id == $all_student_complain_info->class_id ? 'selected ' : ''; } ?>>
<?php echo $v_class->classes_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Section<span class="required">*</span></label>
<div class="col-sm-5">
<select name="section_id" id="section_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select Section...</option>
<?php if (!empty($all_section_info)): foreach ($all_section_info as $v_section): ?>
<option value="<?php echo $v_section->section_id; ?>"
<?php if (!empty($all_student_complain_info->section_id)) {
echo $v_section->section_id == $all_student_complain_info->section_id ? 'selected ' : ''; } ?>>
<?php echo $v_section->section_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Student<span class="required">*</span></label>
<div class="col-sm-5">
<select name="student_id" id="student" class="js-example-basic-multiple form-control" >
<option value="" >Select Student...</option>
<?php if (!empty($student_info)): foreach ($student_info as $v_student): ?>
<option value="<?php echo $v_student->student_id; ?>"
<?php if (!empty($all_student_complain_info->student_id)) {
echo $v_student->student_id == $all_student_complain_info->student_id ? 'selected ' : ''; } ?>>
<?php echo $v_student->student_id.' '.$v_student->student_name.' ('.$v_student->student_father_name.')' ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
This is ajax.php
function get_student_by_school_class_section_id() {
var school_id = document.getElementById('school_id').value;
var class_id = document.getElementById('class_id').value;
var section_id = document.getElementById('section_id').value;
var base_url = '<?= base_url() ?>';
var strURL = base_url + "admin/global_controller/get_student_by_school_class_section_id/" + school_id + "/" + class_id + "/" + section_id;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
var result = req.responseText;
$("#student").html("<option value='' >Select Student...</option>");
$("#student").append(result);
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("POST", strURL, true);
req.send(null);
}
}
this is controller.php
public function get_student_by_school_class_section_id($school_id, $class_id, $section_id) {
$HTML = NULL;
$this->studentrecord_model->_table_name = 'tbl_studentrecords';
$this->studentrecord_model->_order_by = 'student_id';
$student_info = $this->studentrecord_model->get_by(array('school_id' => $school_id, 'class_id' => $class_id, 'section_id' => $section_id, 'status' => '1'), FALSE);
if (!empty($student_info)) {
foreach ($student_info as $v_student_info) {
$HTML.="<option value='" . $v_student_info->student_id . "'>" .$v_student_info->student_id.' '.$v_student_info->student_name.' ('.$v_student_info->student_father_name.')'. "</option>";
}
}
echo $HTML;
}
I want to show student from selected school, class and section. if I delete 'class_id' => $class_id, 'section_id' => $section_id, from controller then it show all student from selected school otherwise it show nothing. any solution please. where am wrong ?

this is studentrecord_model.php
public function all_student_record_info_by_scs($school_id, $class_id, $section_id) {
$this->db->select('tbl_studentrecords.*', FALSE);
$this->db->select('tbl_school.*', FALSE);
$this->db->select('tbl_class.*', FALSE);
$this->db->select('tbl_section.*', FALSE);
$this->db->select('tbl_fee_department.*', FALSE);
$this->db->from('tbl_studentrecords');
$this->db->join('tbl_school', 'tbl_school.school_id = tbl_studentrecords.school_id', 'left');
$this->db->join('tbl_class', 'tbl_class.class_id = tbl_studentrecords.class_id', 'left');
$this->db->join('tbl_section', 'tbl_section.section_id = tbl_studentrecords.section_id', 'left');
$this->db->join('tbl_fee_department', 'tbl_fee_department.fee_department_id = tbl_studentrecords.fee_department_id', 'left');
$this->db->where('tbl_studentrecords.school_id', $school_id);
$this->db->where('tbl_studentrecords.class_id', $class_id);
$this->db->where('tbl_studentrecords.section_id', $section_id);
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}

I have done by changing global_controller.php code like this
public function get_student_by_school_class_section_id($school_id, $class_id, $section_id) {
$HTML = NULL;
$school = $school_id;
$class = $class_id;
$section = $section_id;
$this->studentrecord_model->_table_name = 'tbl_studentrecords';
$this->studentrecord_model->_order_by = 'student_id';
$student_info = $this->studentrecord_model->get_by(array('school_id' => $school, 'class_id' => $class, 'section_id' => $section, 'status' => '1'), FALSE);
if (!empty($student_info)) {
foreach ($student_info as $v_student_info) {
$HTML.="<option value='" . $v_student_info->student_id . "'>" .$v_student_info->student_id.' '.$v_student_info->student_name.' ('.$v_student_info->student_father_name.')'. "</option>";
}
}
echo $HTML;
}

Related

Displaying specific data from the database in a dependent select box in Codeigniter

How can I load data from my database into a select box? This select box is dependent to another select box.
I will need to update the details of my products. When I click the specific product in the table, the product details will load on my update view. Everything is almost fine except for one select box namely size select box, which the data didn't show.
I tried many solutions but none of them works.
This is my product table view:
<div class="col-sm-10" id="main">
<div class="table-wrapper">
<div id="content">
<legend class="text-danger"><h1>Product List</h1></legend>
<?php $tableAttr = array(
'table_open' => '<table class="table table-responsive table-striped table-hover" id="item_tbl">',
);
$item_table = $this->table->set_heading('No.','PRODUCT CODE','PRODUCT DESCRIPTION','BRAND', 'CATEGORY', 'SIZE','ORIGINAL PRICE','ACTION');
$item_table = $this->table->set_template($tableAttr);
$num = 0;
foreach ($items as $item) {
$itemName = urlencode($item->prod_desc);
$num++;
$item_table = $this->table->add_row($num, $item->prod_code, $item->prod_desc, $item->brand_name,$item->category_name,$item->size,'₱'. $item->original_price,"
<a href='".base_url("item/update/$itemName")."'><button class='btn btn-info btn-sm'>EDIT</button></a>
");
}
echo $this->table->generate($item_table); ?>
</div>
</div>
</div>
So when I click edit button it will another form the product_update_view.php:
<div class="col-sm-10" id="main" style="padding: 20px;">
<?php echo form_open("item/item_update/$item->prod_id");
echo form_fieldset('<h3 class="text-info">Update Item</h3>'); ?>
<input type="hidden" name="cur_code" value="<?php echo $item->prod_code ?>">
<input type="hidden" name="cur_name" value="<?php echo $item->prod_desc ?>">
<input type="hidden" name="cur_category" value="<?php echo $item->category_name ?>">
<input type="hidden" name="cur_brand" value="<?php echo $item->brand_name ?>">
<input type="hidden" name="cur_size" value="<?php echo $item->size ?>">
<input type="hidden" name="cur_price" value="<?php echo $item->original_price ?>">
<div class="form-group">
<label for='prod_code'>Product Code:</label>
<input type="text" name="up_code" class="form-control" value="<?php echo $item->prod_code; ?>">
</div>
<div class="form-group">
<label for='prod_name'>Product Description:</label>
<input type="text" name=up_name" class="form-control" value="<?php echo $item->prod_desc; ?>">
</div>
<div class="form-group">
<label for='prod_name'>Brand:</label>
<select name="brand" class="form-control">
<option value="Select Brand" selected="selected">Select Brand</option>
<?php foreach($brand as $br):?>
<option value="<?php echo $br->brand_id; ?>" <?php if ($br->brand_id == $item->brand_id) {echo "selected='selected'";} ?>><?php echo $br->brand_name?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" name="category" id="category">
<option value="Select Brand"> Select Category</option>
<?php foreach($category as $cat): ?>
<option value="<?php echo $cat->category_id; ?>" <?php if ($cat->category_id == $item->category_id) {echo "selected='selected'";} ?>><?php echo $cat->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="sizes">Size:</label>
<select class="form-control" name="category" id="sizes">
<option value="Select size"> Select Size</option>
</select>
</div>
<div class="form-group">
<label for='prod_price'>Price:</label>
<input type="text" name="price" class="form-control" value="<?php echo $item->original_price; ?>">
</div>
<div class="form-group">
<input type="submit" name="submit_account" class="btn btn-primary" value="Save" >
</div>
</div>
<script src="<?php echo base_url() ?>dropdown/js/jquery.js"></script>
<script src="<?php echo base_url() ?>dropdown/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#category').on('change', function(){
var category_id = $(this).val();
if(category_id === '')
$.ajax({
url:"<?php echo base_url() ?>welcome/get_sizes",
type: "POST",
data: {'category_id' : category_id},
dataType: 'json',
success: function(data){
$('#sizes').html(data);
},
error: function(){
alert('Error occur...!!');
}
});
}
});
});
</script>
</div>
<?php echo form_close(); ?>
The controller (item.php):
public function update($name) {
$this->load->model('Dependent_model', 'dep_model', TRUE);
$this->load->model('brand_model');
$data['category'] = $this->dep_model->get_category_query();
$data['brand'] = $this->brand_model->getBrandName();
$this->load->model('item_model');
$data['item'] = $this->item_model->item_info($name);
$this->load->view('header');
$this->load->view('side_menu');
$this->load->view('product_update_view.php',$data);
$this->load->view('footer');
}
The controller of dependent select box (welcome.php):
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Dependent_model', 'dep_model', TRUE);
}
public function index()
{
$data['category'] = $this->dep_model->get_category_query();
$this->load->view('new_product', $data);
}
public function get_sizes()
{
$category_id = $this->input->post('category_id');
$sizes = $this->dep_model->get_sizes_query($category_id);
if(count($sizes)>0)
{
$pro_select_box = '';
$pro_select_box .= '<option value="Select Size">Select Size</option>';
foreach ($sizes as $sz) {
$pro_select_box .='<option value="'.$sz->size_id.'">'.$sz->size.'</option>';
}
echo json_encode($pro_select_box);
}
}
}
Models (item_model.php):
public function item_info ($itemName) {
$this->load->database();
$this->db->select('products.*,category.category_name,sizes.size,brand.brand_name')
->from('products')
->from('category');
$this->db->from('sizes');
$this->db->from('brand');
$this->db->where('products.category_id = category.category_id');
$this->db->where('products.size_id = sizes.size_id');
$this->db->where('products.brand_id = brand.brand_id');
$this->db->where('prod_desc', urldecode($itemName));
$result=$this->db->get();
return $result->row();
}
The dependent select box model (dependent_model):
<?php
class Dependent_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_category_query()
{
$query = $this->db->get('category');
return $query->result();
}
public function get_sizes_query($category_id)
{
$query = $this->db->get_where('sizes', array('category_id' => $category_id));
return $query->result();
}
}
The problem is it does not show the size of the specific product in product_update view.
Replace this jquery.
<script src="<?php echo base_url() ?>dropdown/js/jquery.js"></script>
<script src="<?php echo base_url() ?>dropdown/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var cat_id = $("#category option:selected").val();
if(cat_id != '')
$.fn.ajaxCall(cat_id);
ajaxCall(category_id);
$('#category').on('change', function(){
var category_id = $(this).val();
if(category_id != '')
$.fn.ajaxCall(category_id);
});
$.fn.ajaxCall = function(category_id){
$.ajax({
url:"<?php echo base_url() ?>welcome/get_sizes",
type: "POST",
data: {'category_id' : category_id},
dataType: 'json',
success: function(data){
$('#sizes').html(data);
},
error: function(){
alert('Error occur...!!');
}
});
}
});
</script>

Codeigniter Searchbar

Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->

Not able to fetch the static dropdown value using codeigniter php

I am having a blog page which needs to be updated but unable to fetch the dropdown value from database.Here is the code for fetching dropdown value from database.While adding the data it is inserting successfully but while fetching the data to edit it is not working.Thanks In Advance
View:
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>
Controller:
function editblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="message error"> ','</span>');
$this->form_validation->set_rules('position','Position');
if($this->form_validation->run()== FALSE)
{
$data['records']=$this->blogs_model->getblogsdata($this->input->post('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
{
$result = $this->blogs_model->update($this->input->post('blog_id'));
if(is_array($result))
{
$data['errors']=$result;
$data['records']=$this->blogs_model->getblogsdata($this->uri->segment('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
$this->flash->success('<h2>Successfully Updated the record.<h2>');
redirect('blogs');
}
}
Model:
function update($id)
{
$data=array(
'position'=>$this->input->post('position')
);
$this->db->where(array('blog_id'=>$id));
$this->db->update('blogs', $data);
return true;
}
Can you try something like this below one
Please try the below code
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<?php for($i=1;i<3;i++)
$option = '<option value="'.$i.'"';
if($i === $r->position ){
$option .= 'selected';
}
$option .= '>'.$i.'</option>';
<?php?>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>
Fetching the value from database for static dropdown. Here is the answer.
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<?php
$selected = ($this->input->post('position')) ? $this->input->post('position') : $r->position; ;
$position = array("1" => "1", "2" => "2", "3" => "3", "4" => "Select none");
echo form_dropdown('position', $position, $selected);
?>
</div>

Opencart add products in admin panel

I am developing a module for opencart 2.2.0.0 and I copied some of the basic functionality but mine isn't working.
I got to the point where the view is displaying the product form but in the original module the products which are available are shown and with my module they don't.
Original code from admin:
Controller
class ControllerModuleFeatured extends Controller {
private $error = array();
public function index() {
$this->load->language('module/featured');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('featured', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], true));
}
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_product'] = $this->language->get('entry_product');
$data['entry_limit'] = $this->language->get('entry_limit');
$data['entry_width'] = $this->language->get('entry_width');
$data['entry_height'] = $this->language->get('entry_height');
$data['entry_status'] = $this->language->get('entry_status');
$data['help_product'] = $this->language->get('help_product');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (isset($this->error['name'])) {
$data['error_name'] = $this->error['name'];
} else {
$data['error_name'] = '';
}
if (isset($this->error['width'])) {
$data['error_width'] = $this->error['width'];
} else {
$data['error_width'] = '';
}
if (isset($this->error['height'])) {
$data['error_height'] = $this->error['height'];
} else {
$data['error_height'] = '';
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], true)
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/featured', 'token=' . $this->session->data['token'], true)
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/featured', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true)
);
}
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('module/featured', 'token=' . $this->session->data['token'], true);
} else {
$data['action'] = $this->url->link('module/featured', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
}
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], true);
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
$data['token'] = $this->session->data['token'];
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
$this->load->model('catalog/product');
$data['products'] = array();
if (!empty($this->request->post['product'])) {
$products = $this->request->post['product'];
} elseif (!empty($module_info['product'])) {
$products = $module_info['product'];
} else {
$products = array();
}
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['name']
);
}
}
if (isset($this->request->post['limit'])) {
$data['limit'] = $this->request->post['limit'];
} elseif (!empty($module_info)) {
$data['limit'] = $module_info['limit'];
} else {
$data['limit'] = 5;
}
if (isset($this->request->post['width'])) {
$data['width'] = $this->request->post['width'];
} elseif (!empty($module_info)) {
$data['width'] = $module_info['width'];
} else {
$data['width'] = 200;
}
if (isset($this->request->post['height'])) {
$data['height'] = $this->request->post['height'];
} elseif (!empty($module_info)) {
$data['height'] = $module_info['height'];
} else {
$data['height'] = 200;
}
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/featured', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/featured')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!$this->request->post['width']) {
$this->error['width'] = $this->language->get('error_width');
}
if (!$this->request->post['height']) {
$this->error['height'] = $this->language->get('error_height');
}
return !$this->error;
}
}
View
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-featured" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-featured" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="<?php echo $help_product; ?>"><?php echo $entry_product; ?></span></label>
<div class="col-sm-10">
<input type="text" name="product_name" value="" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($products as $product) { ?>
<div id="featured-product<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
<input type="hidden" name="product[]" value="<?php echo $product['product_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-limit"><?php echo $entry_limit; ?></label>
<div class="col-sm-10">
<input type="text" name="limit" value="<?php echo $limit; ?>" placeholder="<?php echo $entry_limit; ?>" id="input-limit" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-width"><?php echo $entry_width; ?></label>
<div class="col-sm-10">
<input type="text" name="width" value="<?php echo $width; ?>" placeholder="<?php echo $entry_width; ?>" id="input-width" class="form-control" />
<?php if ($error_width) { ?>
<div class="text-danger"><?php echo $error_width; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-height"><?php echo $entry_height; ?></label>
<div class="col-sm-10">
<input type="text" name="height" value="<?php echo $height; ?>" placeholder="<?php echo $entry_height; ?>" id="input-height" class="form-control" />
<?php if ($error_height) { ?>
<div class="text-danger"><?php echo $error_height; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('input[name=\'product_name\']').autocomplete({
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['product_id']
}
}));
}
});
},
select: function(item) {
$('input[name=\'product_name\']').val('');
$('#featured-product' + item['value']).remove();
$('#featured-product').append('<div id="featured-product' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product[]" value="' + item['value'] + '" /></div>');
}
});
$('#featured-product').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
//--></script></div>
<?php echo $footer; ?>
Which results in:
And here is my module code:
Controller
class ControllerModulePopular extends Controller {
private $error = array();
public function index() {
$this->load->language('module/popular');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
// Validate and check for errors
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('popular', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->cache->delete('product');
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
// Set language
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_product'] = $this->language->get('entry_product');
$data['entry_limit'] = $this->language->get('entry_limit');
$data['entry_width'] = $this->language->get('entry_width');
$data['entry_height'] = $this->language->get('entry_height');
$data['entry_status'] = $this->language->get('entry_status');
$data['help_product'] = $this->language->get('help_product');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (isset($this->error['name'])) {
$data['error_name'] = $this->error['name'];
} else {
$data['error_name'] = '';
}
if (isset($this->error['width'])) {
$data['error_width'] = $this->error['width'];
} else {
$data['error_width'] = '';
}
if (isset($this->error['height'])) {
$data['error_height'] = $this->error['height'];
} else {
$data['error_height'] = '';
}
// Breadcrumbs
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/popular', 'token=' . $this->session->data['token'], 'SSL')
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/popular', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL')
);
}
// Setting the action variable depending upon the presence of module_id.
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('module/popular', 'token=' . $this->session->data['token'], 'SSL');
} else {
$data['action'] = $this->url->link('module/popular', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
}
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
// If the module_id is present in URL and the action is not POST then this will fetch the module information based on the module_id.
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
// Products
$this->load->model('catalog/product');
$data['products'] = array();
if (!empty($this->request->post['product'])) {
$products = $this->request->post['product'];
} elseif (!empty($module_info['product'])) {
$products = $module_info['product'];
} else {
$products = array();
}
foreach ($products as $product_id) {
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$data['products'][] = array(
'product_id' => $product_info['product_id'],
'name' => $product_info['name']
);
}
}
// End products
if (isset($this->request->post['limit'])) {
$data['limit'] = $this->request->post['limit'];
} elseif (!empty($module_info)) {
$data['limit'] = $module_info['limit'];
} else {
$data['limit'] = 5;
}
if (isset($this->request->post['width'])) {
$data['width'] = $this->request->post['width'];
} elseif (!empty($module_info)) {
$data['width'] = $module_info['width'];
} else {
$data['width'] = 200;
}
if (isset($this->request->post['height'])) {
$data['height'] = $this->request->post['height'];
} elseif (!empty($module_info)) {
$data['height'] = $module_info['height'];
} else {
$data['height'] = 200;
}
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
// Loading the header, footer and column_left controller and then setting the output with the $data. Finally HTML will be constructed from the template/data.
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/popular.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/popular')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!$this->request->post['width']) {
$this->error['width'] = $this->language->get('error_width');
}
if (!$this->request->post['height']) {
$this->error['height'] = $this->language->get('error_height');
}
return !$this->error;
}
}
View
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-popular" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-popular" class="form-horizontal">
<!-- Entry Name -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
<?php } ?>
</div>
</div>
<!-- Entry Name -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="<?php echo $help_product; ?>"><?php echo $entry_product; ?></span></label>
<div class="col-sm-10">
<input type="text" name="product_name" value="" placeholder="<?php echo $entry_product; ?>" id="input-product" class="form-control" />
<div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($products as $product) { ?>
<div id="featured-product<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
<input type="hidden" name="product[]" value="<?php echo $product['product_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
<!-- Entry Limit -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-limit"><?php echo $entry_limit; ?></label>
<div class="col-sm-10">
<input type="text" name="limit" value="<?php echo $limit; ?>" placeholder="<?php echo $entry_limit; ?>" id="input-limit" class="form-control" />
</div>
</div>
<!-- Entry Limit -->
<div class="form-group">
<label class="col-sm-2 control-label" for="input-width"><?php echo $entry_width; ?></label>
<div class="col-sm-10">
<input type="text" name="width" value="<?php echo $width; ?>" placeholder="<?php echo $entry_width; ?>" id="input-width" class="form-control" />
<?php if ($error_width) { ?>
<div class="text-danger"><?php echo $error_width; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-height"><?php echo $entry_height; ?></label>
<div class="col-sm-10">
<input type="text" name="height" value="<?php echo $height; ?>" placeholder="<?php echo $entry_height; ?>" id="input-height" class="form-control" />
<?php if ($error_height) { ?>
<div class="text-danger"><?php echo $error_height; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('input[name=\'product_name\']').autocomplete({
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['product_id']
}
}));
}
});
},
select: function(item) {
$('input[name=\'product_name\']').val('');
$('#featured-product' + item['value']).remove();
$('#featured-product').append('<div id="featured-product' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product[]" value="' + item['value'] + '" /></div>');
}
});
$('#featured-product').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove();
});
//--></script>
</div>
<?php echo $footer; ?>
Why am I not able to choose products from the available list?
P.S. sorry for the long code examples but I didn't knew what's needed to explain my problem.
You have missed a line of code in Controller file,
Add below line
$data['token'] = $this->session->data['token'];
Above $this->load->model('catalog/product'); in Controller file

How do i retrieve the value of dropdown when editing in codeigniter

OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?

Categories