I'm having some trouble displaying the values retrieved from a db and displaying them in a dropdown menu, using codeigniter. The code is as follows:
Controller:
<?php
class AuthorSignupC extends CI_Controller
{
function index()
{
$this->load->model('AuthorSignupM');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title']= 'All Conferences';
$data['groups'] = $this->AuthorSignupM->ViewAllConf();
$this->load->view('AuthorSignup', $data);
}
}
Model:
<?php
class AuthorSignupM extends CI_Model
{
function ViewAllConf()
{
$this->db->select('ConfLName');
$this->db->from('conference');
//$query = $this->db->query('SELECT ConfLName FROM conference');
$query = $this->db->get();
return $query->result();
}
}
?>
View:
<select class="form-control">
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->description.'">'.$row->description.'</option>';
}
?>
</select>
You're selecting just one column:
$this->db->select('ConfLName');
$this->db->from('conference');
but retrieving 'description', and I guess your error reporting is set too low to show you the error.
You should loop over confLName (or use $this->db->select('description');)
<select class="form-control">
<?php foreach($groups as $row) : ?>
<option value="<?php echo $row->ConfLName;?>"><?php echo $row->ConfLName;?></option>
<?php endforeach;?>
</select>
Related
I am designing application using CI. and want to show the drop down however its not displaying it.
View
<label>Select Customer</label>
<select name="name" id="name" class="form-control" required="true">
<option selected="">Select Customer</option>
<?php if(isset($client_name)) {
//var_dump($name);
foreach($client_name as $tn)
{
$tn=(array)$tn;
echo '<option selected="" value="'.$tn['name'].'" >'.$tn['name'].'</option>';
//echo '<option selected="" value="'.$tn['tid'].'" >'.$tn['tname'].'</option>';
}
}
else{
echo '<option selected="" value="Data Not found" >Error</option>';
}
?>
</select>
Model:
public function fetch_client(){
$this->db->select('name');
$this->db->distinct();
$this->db->from($this->table);
$query = $this->db->get();
return $query->result();
}
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Invoice extends CI_Controller {
public function __construct()
{
parent::__construct();
//$this->load->model('person_model','person');
$this->load->model('client_model','client');
}
public function index()
{
$this->load->helper('url');
//$this->load->view('person_view');
$data['client_name']=$this->client->fetch_client();
$this->load->view('createinvoice',$data);
}
}
The front end is only displaying Error saying data not found. Not sure where I am going wrong. Please help !!!
Edit this
if(isset($client_name))
to
if(isset($client_name) && !empty($client_name))
then remove
selected=""
edit this
$this->db->distinct();
to
$this->db->distinct('id');
if you want use distinct.
you can change "id" to "name" if your table don't use id
Try This Code :
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Invoice extends CI_Controller {
public function __construct()
{
parent::__construct();
//$this->load->model('person_model','person');
$this->load->model('client_model','client');
}
public function index()
{
$this->load->helper('url');
//$this->load->view('person_view');
$fetch_record['client_name'] = $this->Model->select('city');
$this->load->view('createinvoice',$fetch_record);
}
}
Model
public function fetch_client($table){
$this->db->distinct();
$this->db->select('name');
$this->db->from($table);
$r= $this->db->get($table);
$res = $r->result();
return $res;
}
View
<label>Select Customer</label>
<select name="name" id="name" class="form-control" required="true">
<option selected="">Select Customer</option>
<?php
if(!empty($client_name))
{
//var_dump($name);
foreach ($client_name as $c)
{
?>
<option value="<?php echo $c->name ?>"><?php echo $c->name; ?></option>
<?php
}
}
else
{
echo '<option selected="" value="Data Not found" >Error</option>';
}
?>
</select>
1.Check the query
var_dump($this->db->last_query());
2.Check the value in Controller
var_dump($data['client_name']);
3.Check the route is correct or not?
route["my-controller/my-method"] = "Invoice/index";
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 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
I can already data into the database, but I cannot retrieve it from the database into the dropdownlist. Can anyone help me out?
My view:
<body>
<div id="container">
<?php echo form_open('form/myform'); ?>
<select class="form-control" >
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->school.'">'.$row->school.'</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(); ?>
</div>
My Controller:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
$this->load->view('myform');
}
function drop() {
$this->load->model('school_model');
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
My Model:
function getAll() {
$query = $this->db->query('SELECT school FROM tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
I'm really confused on how to retrieve data from the database into the dropdownlist. I would really appreciate your help.
Are you accessing form/drop to generate the view? If you want to access this via form/myform you will have to change myform to include the call to $this->school_model->getAll();
public 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);
}
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);
}