select2 partial load data - php

I'm using select2 plugin to show my dropdown data .
I have problem , because my dropdown select2 have 18000 data .
How it is so that the data displayed in part, not all data is loaded. I use the Medoo database framework. how do i do it?
This my source code .
<div class="form-group">
<label for="nama_user"><?php _e('Nama User'); ?></label>
<select class="form-control select2 select2-hidden-accessible" id="nama_user" name="nama_user" style="width: 100%;" tabindex="-1" aria-hidden="true">
<option value="0"><?php _e('Nobody'); ?></option>
<?php foreach ($ldap as $ldap) { ?>
<option value='<?php echo $ldap['id']; ?>'><?php echo $ldap['nama_user']; ?></option>
<?php } ?>
</select>
</div>
This code for jquery .
$(".select2").select2();
$(".select2tag").select2({
tags: true,
maximumSelectionLength: 1
});
$(".select2tags").select2({
tags: true
});
This Function for call data with Medoo
case "gemilang/add":
$contacts = getTable("contacts");
if($isAdmin) { $assets = getTable("assets"); } else { $assets = getTableFiltered("assets","clientid",$liu['clientid']); }
$clients = getTable("clients");
$cabang_gemilang = getTable("cabang_gemilang");
$ldap = getTable("ldap");
$admins = getTableFiltered("people","type","admin");
if($isAdmin) { $users = getTableFiltered("people","type","user"); } else { $users = getTableFiltered("people","type","user","clientid",$liu['clientid']); }
break;

Related

CodeIgniter dropdown from multiple table

I am new to codeigniter and I am working on some project and I happen to need to display a drop down but filled with data from two or more tables. I don't know if join will solve the problem. Here is my view code:
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php foreach($dataget as $val)
{
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
?>
</select>
</div>
and here is my model:
function get_store()
{
$this->db->select('StoreName');
$this->db->from('store');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
and here is my controller:
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$datadrop['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
I am working on the edit page of the project can someone please help. Thanks in advance.
In your controller
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$data['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
To stop duplicate rows you can use in your view
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php
$store_name = array();
foreach($dataget as $val)
{
$store_name[] = $val->Store;
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
if(!in_array($value['StoreName'],$store_name))//it will stop duplication of rows...
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
}
?>
</select>
</div>
Well, looks like you put incorrect view variable
$datadrop['storename'] = $this->wip_model->get_store();
Should be changed to
$data['storename'] = $this->wip_model->get_store();
And I'd recommend to merge 2 arrays ($dataget/$storename) into one in controller, then use one foreach in the view, the code must be better

Codeigniter : Jquery fill the textbox based on combobox

I am Nubie in JQuery especially in ajax..
I have read articles about dropdown combobox (read from database) then fills other textbox (read from database based on combobox data) but I have confused
I Need Jquery / Ajax / something to show it
can someone solve my problem and explain to me..
Controller
public function tambah()
{
$cek = $this->session->userdata('logged_in');
if(empty($cek))
{
header('location:'.base_url().'app_admin');
}
else
{
$d['nama_madrasah'] = "";
$d['nsm'] = "";
$d['provinsi'] = "";
$d['alamat'] = "";
$d['kecamatan'] = "";
$d['kab_kota'] = "";
$d['jenis_bantuan'] = "";
$d['jenjang'] = "";
$d['th_ang'] = "";
$d['sumber_dana'] = "";
$d['tahap_pencairan'] = "";
$d['st'] = "tambah";
$d['dt_nsm'] = $this->app_model->ShowNSMMaster();
$this->load->view("app_admin/madrasah/input",$d);
}
}
My View
<select data-placeholder="Cari NSM..." class="chzn-select" style="width:260px;" tabindex="2" name="nsm" id="dt_nsm" >
<?php
if($st=='edit')
{
?>
<option value="<?php echo '$nsm'; ?>" selected="selected"><?php echo '$nsm'; ?></option>
<?php
}
else
{
?>
<option value="" selected="selected">- Pilih -</option>
<?php
foreach($dt_nsm->result_array() as $dp1)
{
?>
<option value="<?php echo $dp1['nsm']; ?>"><?php echo $dp1['nsm']; ?> | <?php echo $dp1['nama_madrasah']; ?></option>
<?php
}
}
?>
</select>
</span></div>
<label class="control-label" for="nama_madrasah">Nama Madrasah </label>
<div class="controls">
<input type="text" class="span" name="nama_madrasah" id="nama_madrasahx" value="<?php echo $nama_madrasah; ?>" placeholder="">
</div>
<label class="control-label" for="alamat">Alamat</label>
<div class="controls">
<input type="text" class="span" name="alamat" id="alamatx" value="<?php echo $alamat; ?>" placeholder="">
</div>
JS
<script type="text/javascript">
$('#dt_nsm').on('change', function(){
dt_nsm = $('#dt_nsm option:selected').val(); // the dropdown item selected value
$.ajax({
type :'POST',
dataType:'json',
data : { dt_nsm : dt_nsm },
url : 'getresult.php',
success : function(result){
$('#nama_madrasahx).val(result['nama_madrasahx']);
$('#kecamatanx).val(result['kecamatanx']);
}
});});
</script>
getresult.php
<?php
require_once ('../config/db.config.php');
$dt_nsm = $_POST['dt_nsm'];
$query = "SELECT * FROM bantuan WHERE nsm = '$nsm' ";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo json_encode($row);
?>
Thanks For your time. Need Advice !
change
foreach($dt_nsm->result_array() as $dp1)
to
foreach($dt_nsm->result_array() as $key=>$dp1)

AJAX PHP : Dynamic drop down list using AJAX

Today, I went to code a form with Dynamic dropwdown list in it.
Mission : I want to make dynamic dropdown list when User choose Category, then the store with that category will appear.
It is the form :
<select name="category" class="form-control" id="category" onchange="ajaxStore(this.value)">
<option value="-1"> - Choose Category -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select>
<label for="inputName" class="control-label">Store Name</label>
<select name="store" class="form-control" id="store">
<option value="-1"> - Choose Store -</option>
</select>
This is the AJAX :
<!-- linking drop down AJAX -->
<script type="text/javascript">
var ajaxku=buatajax();
function ajaxStore(id){
var url="objects/StoreAPIService.php?category="+id+"&sid="+Math.random();
ajaxku.onreadystatechange=stateChanged;
ajaxku.open("GET",url,true);
ajaxku.send(null);
}
function buatajax(){
if (window.XMLHttpRequest){
return new XMLHttpRequest();
}
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function stateChanged(){
var data;
if (ajaxku.readyState==4){
data=ajaxku.responseText;
if(data.length>=0){
document.getElementById("store").innerHTML = data
}else{
document.getElementById("store").value = "<option selected>- Choose Store -</option>";
}
}
}
</script>
And this the code of StoreAPIService.php :
session_start();
$stores = array();
$store_category_id = $_GET['category'];
try
{
//$client = new GuzzleClient();
$response = $this->client->get('admin/store/bycat/rev/'.$store_category_id,
['headers' => ['Authorization' => $_SESSION['login']['apiKey']]
]);
$statusCode = $response->getStatusCode();
// Check that the request is successful.
if ($statusCode == 200)
{
$error = $response->json();
echo"<option value=''>- Choose Store -</option>";
foreach ($error['stores'] as $store)
{
$mainStore = new StoreSummary();
echo "<option value='{$mainStore->setStoreId($store['store_id'])};'>{$mainStore->setStoreName($store['store_name'])};</option>";
array_push($stores, $mainStore);
}
}
}
I got no error, but the Store with 'Category' that I Choose is not appearing.
Could anyone please check if I went wrong.
thanks for your helps.
You were trying to change options of a dropdown menu. i did this by changing dropdown menu itself.
I created a div with id="store" in the html form and changing the innerHTML of div using ajax
html form
<select name="category" class="form-control" id="category" onchange="ajaxStore(this.value)">
<option value="-1"> - Choose Category -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select><label for="inputName" class="control-label">Store Name</label>
<div id="store"><select name="store" class="form-control">
<option value="-1"> - Choose Store -</option>
</select>
</div> <!--end of store -->
ajax code
<!-- linking drop down AJAX -->
<script type="text/javascript">
var ajaxku=buatajax();
function ajaxStore(id){
var url="objects/StoreAPIService.php?category="+id+"&sid="+Math.random();
ajaxku.onreadystatechange=stateChanged;
ajaxku.open("GET",url,true);
ajaxku.send(null);
}
function buatajax(){
if (window.XMLHttpRequest){
return new XMLHttpRequest();
}
if (window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function stateChanged(){
var data;
if (ajaxku.readyState==4){
data=ajaxku.responseText;
if(data.length>=0){
document.getElementById("store").innerHTML = data
}else{
document.getElementById("store").value = "<select name=\"store\" class=\"form-control\">
<option value=\"-1\"> - Choose Store -</option>
</select>";
}
}
}
</script>
StoreAPIService.php :
session_start();
$stores = array();
$store_category_id = $_GET['category'];
try
{
//$client = new GuzzleClient();
$response = $this->client->get('admin/store/bycat/rev/'.$store_category_id,
['headers' => ['Authorization' => $_SESSION['login']['apiKey']]
]);
$statusCode = $response->getStatusCode();
// Check that the request is successful.
if ($statusCode == 200)
{
$error = $response->json();
echo "<select name=\"store\" class=\"form-control\">
<option value=\"\">- Choose Store -</option>";
foreach ($error['stores'] as $store)
{
$mainStore = new StoreSummary();
echo "<option value='{$mainStore->setStoreId($store['store_id'])};'>{$mainStore->setStoreName($store['store_name'])};</option>";
array_push($stores, $mainStore);
}//end of foreach
echo "</select>"
}
}

Change dropdown based on another one in CodeIgniter

I am new to CI. I need to Change some dropdown list according to another dropdown.
My controller code:
public function index()
{
$data = array();
$data['from_list'] = $this->from->list_from();
$data['to_list'] = $this->to->list_to();
$data['fromwhere_list'] = $this->fromwhere->list_fromwhere();
//$data['fromwhere_from_list'] = $this->fromwhere->list_fromwhere_from();
$data['towhere_list'] = $this->towhere->list_towhere();
$data['header'] = array('view'=>'header','data'=>array());
$data['main_content'] = array('view'=>'home','data'=>array());
$data['footer'] = array('view'=>'','data'=>array());
$this->load->view('template',$data);
//$this->load->view('home');
}
model code
function list_fromwhere()
{
$this->db->select('fromwhere_id,from_id,from_from_name');
$this->db->from('mt_from_from');
$query = $this->db->get();
return $query->result();
}
function list_from()
{
$this->db->select('from_id,from_name');
$this->db->from('mt_from');
$query = $this->db->get();
return $query->result();
}
My View code
<select class="" name="from" id="from" style="width: 20%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item)
{
?>
<option value="<?php echo $from_item->from_id?>"><?php echo $from_item->from_name?></option>
<?php
}
?>
<!-- <option value="volvo">Airport</option>
<option value="saab">Town</option>
<option value="mercedes">Hotel</option> -->
</select>
<select class="" name="from_place" id="from_place" style="width: 56%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item){
foreach($fromwhere_list as $fromwhere_item)
{
if($fromwhere_item->from_id == $from_item->from_id){
?>
<option value="<?php echo $fromwhere_item->fromwhere_id?>"><?php echo $fromwhere_item->from_from_name?></option>
<?php
}
}
}
?>
</select>
When we select the town, country by from dropdown list (firstone), I need to automatically change from place dropdown (secondone).

Dynamic get data in select box codeigniter

I have two select boxes, if I select an option, it will give me results according to the options and then the results will display in the 2nd select box. How can I do this?
I think, I have to do a onchange function but I don't know how can I get data from controller in JavaScript .
class StockInController extends MY_Controller {
function index(){
$data['main_content'] = 'stockInView';
$this->load->model('supplierModel');
$query = $this->supplierModel->getAllSuppliers();
if ($query){
$data['records1'] = $query;
}
$this->load->model('categoryModel');
$query = $this->categoryModel->getAllCategories();
if ($query){
$data['records2'] = $query;
}
$this->load->model('itemsModel');
$query = $this->itemsModel->getAllItems();
if ($query){
$data['records3'] = $query;
}
$this->load->view('dashboardTemplate/template',$data);
}
function addStockIn(){
//getting parameters from view
$data = array(
'item_name' => $this->input->post('item_name'),
'cat_id' => $this->input->post('cat_id')
);
//$is_ajax = $this->input->post('ajax'); //or use this line
//$this->input->is_ajax_request();
$result = array();
$this->load->model('stockInModel');
$query = $this->stockInModel->addStocktoDB($data);
if ($query){ //&& any other condition
$result['res'] = 1;//process successful - replace 1 with any message
} else {
$result['res'] = 0;//process failed - replace 0 with any message
}
echo json_encode($result);//at the end of the function.
}
}
Here is the View:
<div class="control-group">
<label for="selsear" class="control-label">Select a Category</label>
<div class="controls">
<select name="select1" id="seslsear" class='cho'>
<option id = "y" value="0">--Select--</option>
<?php foreach($records2 as $r) { ?>
<option value="<?=$r->cat_id?>"><?=$r->cat_name?></option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label for="selsear" class="control-label">Select an item</label>
<div class="controls">
<select name="select1" id="selsear" class='cho'>
<option id = "y" value="0">--Select--</option>
<?php foreach($records3 as $r) { ?>
<option value="<?=$r->item_id?>"><?=$r->item_name?></option>
<?php } ?>
</select>
</div>
</div>
You will have to write a function on-change of select box & feed data in success of ajax
$('#selsear').change(function() {
var form_data = {
item_name: $('#item_name').val(),
cat_id: $('#selsear').val()
};
$.ajax({
url:'controller_name/function_name',
data:form_data,
datatype:'application/json',
success:function(data){
$.each(data,function(i){
htmlString="<option value='"+data[i]['item_id']+"'>'"+data[i]['item_name']+"'</option>"
});
$("#selsear").html(htmlString);
}
});
The controller function name is the name of function which retrieves data from controller.
I hope this will help you!

Categories