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
Related
Hi any one help me how to get data in drop down from database in codeigniter.below is my code it is not work working. The below code is not working properly the drop down not appearing when click please help me to get data. I am doing it in codeigniter. I want to get data from database in dropdown. I think the code is perfect am new to codeigniter the below code is not working properly the drop down not appearing when click please help me to get data. I am doing it in codeigniter. I want to get data from database in dropdown. I think the code is perfect am new to codeigniter
Model:
public function getdepartment() {
$query = $this->db->query("select department from object_data");
$result = $this->executeSelectQuery($query);
return $result;
}
View:
<div class="col-md-4">
<h4> Courses:</h4>
<?php $dp = $this->base_model->getdepartment(); ?>
</div>
<div class="col-md-8">
<select name="report_type" class="form-control">
<?php foreach($dp as $row) {
echo '<option value="'.$row->department.'">'.$row->department.'</option>';
}?>
</select>
</div>
Controller:
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
the above code is not working properly the drop down not appearing when click please help me to get data
Model
public function getdepartment() {
$query = $this->db->query("select department from object_data");
return $query->result();
}
controller
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
views
<div class="col-md-4">
<h4> Courses:</h4>
<?php $dp = $this->base_model->getdepartment(); ?>
</div>
<div class="col-md-8">
<select name="report_type" class="form-control">
<?php foreach($dprtmnt as $row) {
echo '<option value="'.$row->department.'">'.$row-
>department.'</option>';
}?>
</select>
</div>
you should try codeigniter own methods it is the good way for practice.
Model :
public function getdepartment() {
$query = $this->db->select('department')->from('object_data')->get();
return $query->result();
}
Controller :
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
Views :
<select name="report_type" class="form-control">
<?php foreach($dprtmnt as $row) {
echo '<option value="'.$row->department.'">'.$row-
>department.'</option>';
}?>
</select>
I was wrote this function in my model :
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
And I want to run a foreach loop on result of this array in my view file :
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach (??? as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
But I don't know what should I write in the first parameter in foreach loop.Can anybody help me with this ?
Thanks
You should call the function get_writers() in controller like :-
$writers = $this->your_model->get_writers();
Then you need to assign this $result variable data in view section like :-
$this->view->load('view_page',array('writers'=>$writers));
After that you can use that variable data in form in view section.
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($writers as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
Get the value on controller and pass to view like this:
MODEL
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
CONTROLLER (inside of your method)
$this->view->load('your_page_in_view_path', array('result' => $this->model->get_writers()));
VIEW
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($result as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
I am new to codeigniter and I'm having a problem in retrieving data from database to drop down list. Can anyone help me with this?
My View:
<?php echo form_open('form/myform'); ?>
<select id="addother" >
<option value="none" selected="selected"> ------Select School------ </option>
<?php foreach($groups as $row) {
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
} ?>
</select>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'school', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
<?php echo form_close(); ?>
My Controller:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
My Model:
function getAll() {
$query = $this->db->get('tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
I can't retrieve data from database into the drop down list. I would really appreciate your help. Thanks!
i think that you could be persist the select value when the form is submitted in the "myForm" method. So, in the same method, retrieve the new group values. Try this:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if ($this->form_validation->run() == TRUE){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
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).
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;
}