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
Related
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;
}
Can't make basic things. The connection to database is good, cause it shows list, deletes data lines. Also after submiting, it doesn't show any errors, but it doesn't show in database...I am still a beginner in PHP, I searched thoroughly but I can't find out what's wrong in code
This is my model class:
<?php
class abonimentas {
private $abonimentas_lentele = '';
public function __construct() {
$this->abonimentas_lentele = config::DB_PREFIX . 'abonimentas';
}
public function getAbonimentasListCount() {
$query = " SELECT COUNT(`{$this->abonimentas_lentele}`.`id`) as `kiekis`
FROM `{$this->abonimentas_lentele}`";
$data = mysql::select($query);
return $data[0]['kiekis'];
}
public function getAbonimentas($id) {
$query = " SELECT *
FROM `{$this->abonimentas_lentele}`
WHERE `id`='{$id}'";
$data = mysql::select($query);
return $data[0];
}
public function insertAbonimentas($data) {
$query = " INSERT INTO `abonimentas`
(
`pavadinimas`,
`aprasymas`,
`papildomos_salygos`,
`kaina`
)
VALUES
(
'{$data['pavadinimas']}',
'{$data['aprasymas']}',
'{$data['papildomos_salygos']}'
'{$data['kaina']}'
)";
mysql::query($query);
return mysql::getLastInsertedId();
}
public function updateAbonimentas($data) {
$query = " UPDATE `abonimentas`
SET `pavadinimas`='{$data['pavadinimas']}',
`aprasymas`='{$data['aprasymas']}',
`papildomos_salygos`='{$data['papildomos_salygos']}',
`kaina`='{$data['kaina']}'
WHERE `id`='{$data['id']}'";
mysql::query($query);
}
}
This is creation class:
<?php
include 'libraries/abonimentas.class.php';
$abonimentasObj = new abonimentas();
$formErrors = null;
$data = array();
$required = array('pavadinimas', 'aprasymas', 'papildomos_salygos', 'kaina');
$maxLengths = array (
'pavadinimas' => 40,
'aprasymas' => 200,
'papildomos_salygos' => 200
);
if(!empty($_POST['submit'])) {
// nustatome laukų validatorių tipus
$validations = array (
'pavadinimas' => 'anything',
'aprasymas' => 'anything',
'papildomos_salygos' => 'anything',
'kaina' => 'price');
include 'utils/validator.class.php';
$validator = new validator($validations, $required, $maxLengths);
if($validator->validate($_POST)) {
$dataPrepared = $validator->preparePostFieldsForSQL();
$dataPrepared['id'] = $abonimentasObj->insertAbonimentas($dataPrepared);
$abonimentasObj->updateAbonimentas($dataPrepared);
header("Location: index.php?module={$module}&action=list");
die();
} else {
$formErrors = $validator->getErrorHTML();
$data = $_POST;
}
}
include 'templates/abonimentas_form.tpl.php';
?>
This is template for input:
<ul id="pagePath">
<li>Pradžia</li>
<li>Abonimentai</li>
<li><?php if(!empty($id)) echo "Abonimento redagavimas"; else echo "Naujas abonimentas"; ?></li>
</ul>
<div class="float-clear"></div>
<div id="formContainer">
<?php if($formErrors != null) { ?>
<div class="errorBox">
Neįvesti arba neteisingai įvesti šie laukai:
<?php
echo $formErrors;
?>
</div>
<?php } ?>
<form action="" method="post">
<fieldset>
<legend>Abonimento informacija</legend>
<p>
<label class="field" for="pavadinimas">Pavadinimas<?php echo in_array('pavadinimas', $required) ? '<span> *</span>' : ''; ?></label>
<input type="text" id="pavadinimas" name="pavadinimas" class="textbox textbox-200" value="<?php echo isset($data['pavadinimas']) ? $data['pavadinimas'] : ''; ?>">
<?php if(key_exists('pavadinimas', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['pavadinimas']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="aprasymas">Aprašymas<?php echo in_array('aprasymas', $required) ? '<span> *</span>' : ''; ?></label>
<textarea id="aprasymas" name="aprasymas" class=""><?php echo isset($data['aprasymas']) ? $data['aprasymas'] : ''; ?></textarea>
<?php if(key_exists('aprasymas', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['aprasymas']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="papildomos_salygos">Papildomos sąlygos<?php echo in_array('papildomos_salygos', $required) ? '<span> *</span>' : ''; ?></label>
<textarea id="papildomos_salygos" name="papildomos_salygos" class=""><?php echo isset($data['papildomos_salygos']) ? $data['papildomos_salygos'] : ''; ?></textarea>
<?php if(key_exists('papildomos_salygos', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['papildomos_salygos']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="kaina">Kaina<?php echo in_array('kaina', $required) ? '<span> *</span>' : ''; ?></label>
<input type="number" id="kaina" name="kaina" class=""><?php echo isset($data['kaina']) ? $data['kaina'] : ''; ?></input>
<?php if(key_exists('kaina', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['kaina']} simb.)</span>"; ?>
</p>
</fieldset>
<p class="required-note">* pažymėtus laukus užpildyti privaloma</p>
<p>
<input type="submit" class="submit button" name="submit" value="Išsaugoti">
</p>
<?php if(isset($data['id'])) { ?>
<input type="hidden" name="id" value="<?php echo $data['id']; ?>" />
<?php } ?>
</form>
</div>
I have this code:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php if($l_id == '1'){ echo 'class="active"'; }?>><?php echo $l_name;?></li>
<?php } // closing 1st while loop
?>
</ul>
<div class="tab-content">
<?php
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php if($l_id == '1'){ echo 'in active'; }?>" id="<?php echo $l_name;?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang;?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang;?>" id="title_<?php echo $l_lang;?>" value="<?php echo $opened['title_'.$l_lang.'']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang;?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang;?>" id="header_<?php echo $l_lang;?>" value="<?php echo $opened['header_'.$l_lang.'']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php if($page_type == 'tour'){ echo 8;}else {echo 12;} ?>">
<label for="body_<?php echo $l_lang;?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang;?>" id="body_<?php echo $l_lang;?>" rows="7" placeholder="Page Body"><?php echo $opened['body_'.$l_lang.'']; ?></textarea>
</div>
</div>
<?php } //closing 2nd while loop
?>
</div>
When running it, the result is a tabbed form (I skipped the form tags and some html from above, to reduce the code writen) and everything is OK.
My questions are:
How to have the same output, but with a single query and while loop?
Is it possible to make this a function? Any hints?
Thank you!
I think you will have to loop twice, but you don't need to make two queries at all!
Use mysqli_fetch_all to store the results in an array and then loop through it
For example:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$langs = mysqli_fetch_all($r);
foreach($langs as $lang){
//render links
}
//...
foreach($langs as $lang){
//render tabs
}
Your script will run much faster
Try
<?php
function get_rows()
{
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$languages = array();
while($langs = mysqli_fetch_assoc($r))
{
$languages[] = $langs;
}
return $languages;
}
$languages = get_rows();
if($languages != false)
{
?>
<ul>
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php
if($l_id == '1')
{
echo 'class="active"';
}
?>><?php echo $l_name; ?></li>
<?php
}
?>
</ul>
<div class="tab-content">
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php
if($l_id == '1')
{
echo 'in active';
}
?>" id="<?php echo $l_name; ?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang; ?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang; ?>" id="title_<?php echo $l_lang; ?>" value="<?php echo $opened['title_' . $l_lang . '']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang; ?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang; ?>" id="header_<?php echo $l_lang; ?>" value="<?php echo $opened['header_' . $l_lang . '']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php
if($page_type == 'tour')
{
echo 8;
}
else
{
echo 12;
}
?>">
<label for="body_<?php echo $l_lang; ?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang; ?>" id="body_<?php echo $l_lang; ?>" rows="7" placeholder="Page Body"><?php echo $opened['body_' . $l_lang . '']; ?></textarea>
</div>
</div>
<?php } ?>
</div>
<?php
}
?>
Save results while first iterating in some array and the iterate over this array for the second time:
$tmp_array = array();
while ($langs = mysqli_fetch_assoc($r)){
$tmp_array[] = $langs;
}
//some code here
//and second array
foreach ($tmp_array as $langs) {
//more code here
}
<div class="row" id="related_campaigns">
<div class="span6">
<div class="control-group">
<label class="control-label" for="inputgaveabonnement">
Campaign<b>*</b>
</label>
<div class="controls">
<div class="fieldrow_horz1">
<div class="fieldgroup">
<label>
Name <b>*</b>
</label>
</div>
<div class="fieldgroup">
<label>
Image <b>*</b>
</label>
</div>
<div class="fieldgroup">
<label>
Url <b>*</b>
</label>
</div>
</div>
</div>
<?php
// Save to array so that we can loop it out later =)
$result = db2_getrelcamp( array('campaigns_id' => $this_id) );
if (!is_null($result))
{
$numsub1 = 0;
while ( $row = $result->fetch_object() )
{
$numsub1 = $numsub1 +1;
array_push($arrcampaignname, $row->campaignname);
array_push($arrcampaignimage, $row->campaignimage);
array_push($arrcampaignurl, $row->campaignurl);
}
$formcountfld1=$numsub1;
}
else
{
$numsub1=1;
$formcountfld1=1;
}
$i = 1;
if (1==1) {
for ($i = 1; $i <= $numsub1; $i++) {
?>
<div class="fieldrow_horz1">
<div class="fieldgroup">
<input type="text" id="campaignname_<?= $i ?>" name="campaignname_<?= $i ?>" value="<?= isset($arrcampaignname[$i-1]) ? $arrcampaignname[$i-1] : '' ?>" />
</div>
<div class="fieldgroup">
<select id="campaignimage_<?= $i ?>" name="campaignimage_<?= $i ?>" alue="<?= isset($arrcampaignimage[$i-1]) ? $arrcampaignimage[$i-1] : '' ?>" />
<?php
// * Hæmta alla filer i mappen
$dir = "../images/campaigns/";
$files = scandir($dir);
$strSelected = "";
$somethingChecked = false;
foreach($files as $key => $value)
{
if ($value != '.' && $value != '..')
{
if ( $formImage === $value ) {
$strSelected = ' selected="selected"';
$somethingChecked = true;
} else
$strSelected = '';
echo '<option value="' . $value . '"' . $strSelected . '>' . $value . '</option>';
}
}
?>
<?php
if ($somethingChecked) {
$strSelected = '';
} else {
$strSelected = ' selected="selected"';
}
echo "<option disabled='disabled'></option>";
echo '<option value=""' . $strSelected .'>- Ikke bruk bilde -</option>';
?>
</select>
<p class="help-block">A Related image for this campaign from your image archive (upload).</p>
</div>
<div class="fieldgroup">
<input type="text" id="campaignurl_<?= $i ?>" name="campaignurl_<?= $i ?>" value="<?= isset($arrcampaignurl[$i-1]) ? $arrcampaignurl[$i-1] : '' ?>" />
</div>
<img src="../images/remove.png" alt="Delete" class="link" id="remove_<?= $i ?>" />
</div>
<?php /*?>value="<?= isset($arrEtternavn[$i-1]) ? $arrEtternavn[$i-1] : '' ?>"
<?php value="<?= $arrEtternavn[$i-1] ?>" */?> <?php
}
} else {
?>
<div class="fieldrow_horz1" >
<div class="fieldgroup">
<input type="text" id="campaignname_<?= $i ?>" name="campaignname_<?= $i ?>" value="" />
</div>
<div class="fieldgroup">
<select id="campaignimage_<?= $i ?>" name="campaignimage_<?= $i ?>" class="span3" >
<?php
// * Hæmta alla filer i mappen
$dir = "../images/campaigns/";
$files = scandir($dir);
$strSelected = "";
$somethingChecked = false;
foreach($files as $key => $value)
{
if ($value != '.' && $value != '..')
{
if ( $formImage === $value ) {
$strSelected = ' selected="selected"';
$somethingChecked = true;
} else
$strSelected = '';
echo '<option value="' . $value . '"' . $strSelected . '>' . $value . '</option>';
}
}
?>
<?php
if ($somethingChecked) {
$strSelected = '';
} else {
$strSelected = ' selected="selected"';
}
echo "<option disabled='disabled'></option>";
echo '<option value=""' . $strSelected .'>- Ikke bruk bilde -</option>';
?>
</select>
<p class="help-block">A Realated image for this campaign from your image archive (upload).</p>
</div>
<div class="fieldgroup">
<input type="text" id="campaignurl_<?= $i ?>" name="campaignurl_<?= $i ?>" value="" />
</div>
</div>
<?php
}
?>
<?php
$thisId = "countfld1";
$thisVar = $formcountfld1; ?>
<input type="hidden" name="<?= strtolower($thisId) ?>" class="input-medium" id="input<?= $thisId ?>" value="<?= htmlspecialchars($thisVar, ENT_QUOTES) ?>" />
<p class="leggtil1">
+ Legg til flere
</p>
</div>
</div>
</div>
i want to store multiple aticle values in database like title ,image and url but itis storing only one value in db any suggestion .thanks in advance
Look at the documentation for
serialize();
unserialize();
They basically encode and decode, an array or object into a specific text based encoded string, a little like JSON, but its not JSON.
They are used all over, look at a wordpress database for example.
serialize()
unserialize()
I need to set a four order choose of sports in gym, so i have to use four dropdown, i have te retrieve the list of data from database, it's an array drom database that containe 30 disciplines when i choose the fisrt one in the first dropdown the seconde dropdown musn't containe this value, and also when i choose a value from the 3rd dropdown this dropdown musn't containe value1 and value2 from dropdown1 and dropdown2, the same thing about the dropdown four it must containe only 27 values from database
My model : participantsport_m.php :
public function get_new()
{
$participant_sport = new stdClass();
$participant_sport->nom = '';
$participant_sport->prenom = '';
$participant_sport->trimestres = '';
$participant_sport->sport_ordre1 = '';
$participant_sport->sport_ordre2 = '';
$participant_sport->sport_ordre3 = '';
$participant_sport->sport_ordre4 = '';
$participant_sport->sport_montant_paye = '';
$participant_sport->sport_debut_periode = '';
$participant_sport->sport_fin_periode = '';
return $participant_sport;
}
function get_discipline_sport() {
$this ->db->select('id, nom_discipline');
$query = $this ->db->get('disciplines_sport');
$salles = array();
if ($query->result()) {
foreach ($query->result() as $discipline) {
$disciplines[$discipline->id] = $discipline->nom_discipline;
}
return $disciplines;
} else {
return FALSE;
}
}
The structure of database :
id - nom_discipline - tarif_discipline - description
My controller agent.php :
public function inscriresport ($id = NULL)
{
// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'Agent non trouvé';
// explode to array
// print_r($this->data['participant_sport']['trimestres[]']); // test before explode
$this->data['participant_sport']->trimestres = explode(",", $this->data['participant_sport']->trimestres);
// print_r($this->data['participant_sport']['trimestres[]']); // test after explode
}
else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
}
// Set up the form
$rules = $this->participantsport_m->rules_participantsport;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->participantsport_m->array_from_post(array('nom', 'prenom', 'beneficiaire', 'trimestres' , 'sport_ordre1', 'sport_ordre2', 'sport_ordre3', 'sport_ordre3', ,'sport_montant_paye', 'sport_debut_periode', 'sport_fin_periode'));
$this->participantsport_m->save($data, $id);
redirect('admin/agent/profile/3608');
}
// Load the view
$this->data['subview'] = 'admin/agent/inscriresport';
$this->load->view('admin/_layout_main', $this->data);
}
And my view inscriresport.php :
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-align-justify"></i>
</span>
<h5><?php echo empty($participant->id) ? 'Nouveau Agent OCP:' : 'Modification de: ' . $participant_sport->nom.' '.$participant_sport->prenom; ?></h5>
</div>
<div class="widget-content nopadding">
<?php echo form_open(); ?>
<div <?php if(form_error('nom')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Nom :</label>
<div class="controls">
<?php echo form_input('nom', set_value('nom', $this->input->get('nom') )); ?>
<span class="help-inline"><?php echo form_error('nom'); ?></span>
</div>
</div>
<div <?php if(form_error('prenom')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Prenom :</label>
<div class="controls">
<?php echo form_input('prenom', set_value('prenom', $this->input->get('prenom') )); ?>
<span class="help-inline"><?php echo form_error('prenom'); ?></span>
</div>
</div>
<div <?php if(form_error('beneficiaire')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Bénéficiaire :</label>
<div class="controls">
<label><?php echo form_radio('beneficiaire', 'Agent', $this->input->get('beneficiaire') == 'Agent') ?> Agent </label>
<label><?php echo form_radio('beneficiaire', 'Conjoint', $this->input->get('beneficiaire') == 'Conjoint') ?> Conjoint </label>
<label><?php echo form_radio('beneficiaire', 'Enfant', $this->input->get('beneficiaire') == 'Enfant') ?> Enfant </label>
<span class="help-inline"><?php echo form_error('beneficiaire'); ?></span>
</div>
</div>
<div <?php if(form_error('trimestres[]')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
<label class="control-label">Trimestres :</label>
<div class="controls" >
<?php $options = array(
'trim1' => ' Premier trimestre (Janv,Fév,Mars)',
'trim2' => ' Deuxiéme trimestre (Avril,Mai,Juin)',
'trim3' => ' Troisiéme trimestre (Juill,Aout,Sept)',
'trim4' => ' Quatriéme trimestre (Oct,Nov,Déc)',
);
echo form_multiselect('trimestres[]', $options , $this->input->post('trimestres') ? $this->input->post('trimestres') : $participant_sport->trimestres, 'id="trim"'); ?>
<span class="help-inline"><?php echo form_error('trimestres[]'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_ordre1')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
<label class="control-label">Nom de la Salle :</label>
<div class="controls">
<?php echo form_dropdown('sport_ordre1', $countries , $this->input->post('sport_ordre1') ? $this->input->post('sport_ordre1') : $participant_salle->sport_ordre1 , 'id="ordre1"'); ?>
<span class="help-inline"><?php echo form_error('sport_ordre1'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_ordre2')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
<label class="control-label">Nom de la Salle :</label>
<div class="controls">
<?php echo form_dropdown('sport_ordre2', $countries , $this->input->post('sport_ordre2') ? $this->input->post('sport_ordre2') : $participant_salle->sport_ordre2 , 'id="ordre2"'); ?>
<span class="help-inline"><?php echo form_error('sport_ordre2'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_ordre3')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
<label class="control-label">Nom de la Salle :</label>
<div class="controls">
<?php echo form_dropdown('sport_ordre3', $countries , $this->input->post('sport_ordre3') ? $this->input->post('sport_ordre3') : $participant_salle->sport_ordre3 , 'id="ordre3"'); ?>
<span class="help-inline"><?php echo form_error('sport_ordre3'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_ordre4')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
<label class="control-label">Nom de la Salle :</label>
<div class="controls">
<?php echo form_dropdown('sport_ordre4', $countries , $this->input->post('sport_ordre4') ? $this->input->post('sport_ordre4') : $participant_salle->sport_ordre4 , 'id="ordre4"'); ?>
<span class="help-inline"><?php echo form_error('sport_ordre4'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_montant_paye')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Montant à payé :</label>
<div class="controls">
<?php echo form_input('sport_montant_paye', set_value('sport_montant_paye', $participant_sport->sport_montant_paye)); ?>
<span class="help-inline"><?php echo form_error('sport_montant_paye'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_debut_periode')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Debut période :</label>
<div class="controls">
<input type="text" name="sport_debut_periode" id="date3" value="<?php echo set_value('sport_debut_periode', $participant_sport->sport_debut_periode) ?>" />
<span class="help-inline"><?php echo form_error('sport_debut_periode'); ?></span>
</div>
</div>
<div <?php if(form_error('sport_fin_periode')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
<label class="control-label">Fin période :</label>
<div class="controls">
<input type="text" name="sport_fin_periode" id="date4" value="<?php echo set_value('sport_fin_periode', $participant_sport->sport_fin_periode) ?>" />
<span class="help-inline"><?php echo form_error('sport_fin_periode'); ?></span>
</div>
</div>
<div class="form-actions">
<?php echo form_submit('submit', 'Enregistrer', 'class="btn btn-success"'); ?>
</div>
<?php echo form_close();?>
</div>
</div>
</br>
I need how can i do it by javascript i'm newbie on javascript and i don't know where i start, Any Help plsease? Many thanks.
I suggest to include all sports in all dropdowns and when you want to submit your form, go and check to see whether there are any same valued dropdowns or not. You can get them with:
var flag = false;
var e1 = document.getElementById("dropdown1 id");
var value1 = e1.options[e1.selectedIndex].value;
var e2 = document.getElementById("dropdown2 id");
var value2 = e2.options[e2.selectedIndex].value;
...
if(value1 != value2 && ...)
{
flag = true;
}
if(flag)
{
// Stuff
}