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);
}
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 am trying to select and update the data from database. But when I click on update, it show message "Undefined variable: data". What is the problem regarding $data. ? I am not able to link with testing3.php to update my data.
//views/testing2.php
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo anchor('testing3/view', 'Update'); ?></td>
</tr>
<?php endforeach; ?>
//views/testing3.php
<html>
<h1>Update</h1>
<form method="post" >
<?php foreach ($data as $row): ?>
<input type="hidden" name="id" value ="<?php echo $row->id; ?>">
<label>ID: </label><br>
<input type="text" name="name" value ="<?php echo $row->name; ?>">
<label>ID: </label><br>
<input type="text" name="email" value ="<?php echo $row->email; ?>">
<label>ID: </label><br>
<input type="submit" id="submit" name="submit" value ="update">
<label>ID: </label><br>
<?php endforeach; ?>
</form>
</html>
//controllers/testing3.php
<?php
//page name
class Testing3 extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('testing3_model');
}
function showid()
{
$data['data']=$this->testing3_model->getsitemodel();
$data['data']=$this->testing3_model->showid($id);
$this->load->vars($data);
$this->load->view('testing3', $data);
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
}
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->testing3_model->updateid($id, $data);
$this->showid();
redirect('testing');
}
public function view($page = 'testing3') //about us page folder name
{
if ( ! file_exists('application/views/testing3/'.$page.'.php')) //link
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = 'Testing3';
//$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header', $data);
$this->load->view('testing3/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
//models/testing3_model.php
<?php
class Testing3_model extends CI_Model{
public function __construct()
{
$this->load->database(); // to authorize connection to the database
}
//fetch record
public function getsitemodel()
{
$data = $this->db->get('testing'); //pass data to query
return $data->result();
}
//fetch selected record
public function showid($data)
{
$this->db->select('*');
$this->db->from('testing');
$this->db->where('id', $data);
$data = $this->db->get();
return $data->result;
}
//update record
public function updateid($id, $data)
{
$this->db->where('id', $id);
$this->db->update('testing', $data);
}
}
?>
Change this:
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->testing3_model->updateid($id, $data);
$this->showid();
redirect('testing');
}
to this:
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->db->where('id', $this->input->post('id'));
$this->db->update('yourtablename', $data);
redirect('testing');
}
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 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'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>