Displays all the data with the select option in CodeIgniter - php

I tried to display data via select option based on the field division and successful , but how do I display all the data ?
Controllers
public function dokumen()
{
$divisi = $this->input->post('divisi');
$data=array('title' =>'KOPKAR - Pelanggan',
'divisi' => $this->induk_internal->get_divisi(),
'get_dokumen' => $this->induk_internal->get_dokumen($divisi),
'isi' =>'admin/DCR/daftarindukinternal'
);
$this->load->view('layout/wrapper',$data);
}
Models
function get_dokumen($divisi) {
$this->db->select('*');
$this->db->from('daftar_detail_internal');
$this->db->where('divisi',$divisi);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Views
<div class="col-md-3">
<select class="selectpicker" style="display: none;" name="divisi" autofocus>
<option value="if click show all data">ALL</option>
<?php foreach($divisi as $row) { ?>
<option value="<?php echo $row->divisi;?>"><?php echo $row->divisi;?>
</option>
<?php } ?>
</select>

Related

How can I insert my database particular column values into a list in view page

i want to list 'category_name' into a list. i tried but i couldn't.
here is my controller and I'm trying 2 call the fn get_cat.
then I'm looking to pass that array to my view page
controller categorylist
public function get_categorylist()
{
$data['records']= $this->categorylist_model->get_cat();
$this->load->view('categorylist_view',$data);
}
model Categorylist_model
public function get_cat()
{
$this->db->select('category_name');
$this->db->from('amm_report_category');
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
view categorylist_view
<body>
<div>
<form action="<? echo base_url(); ?>categorylist/get_categorylist" method="post">
<select name="categorylist">
<? foreach ($result as $var)
{?>
<option value="<?echo $var->category_name;?>"><?echo $var->category_name;?> </option>
<?}?>
</select>
<p><input type="submit" value="get_categorylist" name=""/></p>
</form>
</div>
</center>
</body>
model
public function get_cat()
{
$this->db->select('catogory_id,category_name');
$this->db->from('amm_report_category');
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
view
<select name="categorylist">
<? foreach ($records as $var)
{?>
<option value="<?echo $var->category_id;?>"><?echo $var->category_name;?> </option>
<?}?>
</select>
Try this..
you store data in data['records'] . But you used to display array name as $result.
You need to use in foreach "foreach ($records as $var) "
public function get_categorylist()
{
$data['records']= $this->categorylist_model->get_cat();
$this->load->view('categorylist_view',$data);
}
public function get_cat()
{
$this->db->select('category_name');
$query = $this->db->get('amm_report_category');
if($query->num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
<body>
<div>
<form action="<?php echo base_url(); ?>categorylist/get_categorylist" method="post">
<select name="categorylist">
<?php foreach ($records as $var)
{ ?>
<option value="<?php echo $var->category_name;?>"><?php echo $var->category_name; ?> </option>
<?php } ?>
</select>
<p><input type="submit" value="get_categorylist" name=""/></p>
</form>
</div>
</center>
</body>

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).

Codeigniter search engine system using multi filter keyword

I'm building my own book library management system using Codeigniter. I get stuck when I want to buil search engine using filtered keyword. I have four tables (books, publisher, category, format) and I use join to retrieve data. But the results were not what I excepted. Below I show my codes:
// TABLE STRUCTURE
Books |book_id, publisher_id, cat_id, format_id, title, author, ... etc|
Publisher |publisher_id, publisher, address|
Category |cat_id, category, description|
Format |format_id, format|
// MODEL
public function findBooks ( $keyword, $publisher, $category, $format, $offset, $limit )
{
$this->db->select('*');
$this->db->join('publisher as p', 'b.publisher_id=p.publisher_id', 'left');
$this->db->join('category as c', 'b.cat_id=c.cat_id', 'left');
$this->db->join('format as f', 'b.format_id=f.format_id', 'left');
if(!empty($keyword)) {
$this->db->like('b.title', $keyword);
$this->db->like('p.publisher', $publisher);
$this->db->or_like('c.category', $category);
$this->db->or_like('f.format', $format);
}
$this->db->order_by('book_id', 'ASC');
$getData = $this->db->get('books as b', $offset, $limit);
if($getData->num_rows() > 0)
{
return $getData->result();
} else {
return NULL;
}
}
public function getPublishers()
{
$query = $this->db->query("SELECT * FROM publisher ORDER BY publisher_id ASC");
return $query;
}
public function getCategories()
{
$query = $this->db->query("SELECT * FROM category ORDER BY cat_id ASC");
return $query;
}
public function getFormat()
{
$query = $this->db->query("SELECT * FROM format ORDER BY format_id ASC");
return $query;
}
// BOOKS CONTROLLER
public function find($keyword='', $offset = '', $limit = 3;)
{
if($this->uri->segment(3) === FALSE){
$offset = 0;
}else{
$offset = ($this->uri->segment(3)-1) * $limit;
}
$keyword = mysql_real_escape_string($this->input->post('term'));
$publisher = $this->input->post('publisher_id');
$category = $this->input->post('cat_id');
$format = $this->input->post('format_id');
$check = $this->adminModel->findBooks( $keyword, $publisher, $category, $format, $offset, $limit );
if($check)
{
$data['message'] = "";
$data['res'] = $check;
$this->load->view('search_result', $data);
} else {
$data['message'] = "<div class='alert alert-warning'>No result. Please try with another keyword.</div>";
$this->load->view('search_result', $data);
}
}
// FORM VIEW
<div>
<h4>Find Books</h4>
<form action="<?php base_url(); ?>books/find" method="POST" class="form-horizontal" role="form">
<div class="input-group input-group col-md-12">
<label for="term" class="sr-only"></label>
<input type="text" class="form-control" name="term" placeholder="Enter Keyword">
</div>
<div class="input-group col-md-12">
<label for="publisher">By Publisher</label>
<select name="publisher" id="publisher" class="form-control">
<option value="0">All</option>
<?php foreach ($pub->result() as $p) { ?>
<option value="<?php echo $p->publisher_id; ?>"<?php echo set_select('publisher_id', $p->publisher_id, (!empty($data) && $data == $p->publisher_id ? TRUE : FALSE )); ?>><?php echo ucwords($p->publisher); ?></option>
<?php ; } ?>
</select>
</div>
<div class="input-group col-md-12">
<label for="category">By Category</label>
<select name="category" id="category" class="form-control">
<option value="0">All</option>
<?php foreach($cats->result() as $cat){ ?>
<option value="<?php echo $cat->cat_id; ?>"<?php echo set_select('cat_id', $cat->cat_id, (!empty($data) && $data == $cat->cat_id ? TRUE : FALSE )); ?>><?php echo ucwords($cat->category); ?></option>
<?php } ?>
</select>
</div>
<div class="input-group col-md-12">
<label for="format">By Format</label>
<select name="format" id="format" class="form-control">
<option value="0">All</option>
<?php foreach ($format->result() as $frm) { ?>
<option value="<?php echo $frm->format_id; ?>"><?php echo set_select('format', $frm->format_id, (!empty($data) && $data == $frm->format_id ? TRUE : FALSE )); ?><?php echo ucwords($frm->format); ?></option>
<?php ; } ?>
</select>
</div>
<div class="input-group col-md-12">
<button type="submit" class="btn btn-success">FIND</button>
</div>
</form>
</div>
Here what I except is, for instance, I search term "Adam" and it might be a name of person, publisher, or part of title. So, if I search the keyword "Adam" and filtered ONLY by publisher, it will show result such as Adam Publishing rather than "History of Adam and Eve" (as book title).
Another question, am I getting wrong in my code? If so, give me direction what it should be.
Best regards

How to search record from db by selecting search category in codeigniter?

I have list of record in my index page. Now i want to search a particular record/records by choosing the category (e.g phone No, email etc).How can i do this? help..
Here is my view:
<?php
$attributes = array('class'=>'searchform', 'id'=>'searchform');
echo form_open('crud/searchCrud', $attributes);?>
<div class="formelements">
<div class="formlbl">
Search:
</div>
<div class="forminput">
<input type="text" name="searchon" id ="searchon"/>
</div>
<div class="searchtyp">
<select class="searchby" name="searchby">
<option value="0">---Select--- </option>
<option value="name">Name</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
</div>
<div class="searchinput">
<input type="submit" name="search" value="Search" />
</div>
</div>
<?php echo form_close();?>
Here is My controller:
public function searchCrud()
{
$records = $this->crud_mdl->searchCrud();
foreach ($records as $record) {
echo $record->name;
}
}
Here is My Model:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$this->db->get('test')->result();
return $this->db->like($searchon, $searchby);
}
1) You should never access POST data directly in the Model.
2) Always collect them in Controller and then pass it to the Model.
3) Also always try to create your Model functions, re-usable.
Modified your code a bit. Try running it.
Controller:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$records = $this->crud_mdl->searchCrud($searchby, $searchon);
foreach ($records as $record)
{
echo $record->name;
}
}
Model:
public function searchCrud($searchby, $searchon)
{
$this->db->like($searchon, $searchby);
$query = $this->db->get('test');
if($query->num_rows() > 0)
return $query->result();
else
return FALSE;
}

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