Dynamic drop down from database in codeigniter - php

I have two dropdowns, I want to fill (deviceType) from table device_typt_table with two columns id, device_type and (brand) from brand_table with id, brand_name, device_type_id.
I try to do that and print data with controller but can't print it in select box
but can print in my view Except select box
My view:
<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>
Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type:<select name="deviceType" >
<? foreach $devices as $post { ?>
<option value="<?php echo $post->device_type;?>"><?php echo $post->device_type;?></option>
<? }?>
</select><br/>
Brand:<select name="brand" >
</select><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="submit" value="save" />
<?php foreach($devices as $post){?>
<?php echo $post->device_type;?>
<?php }?>
<?php echo form_close(); ?>
</body>
</html>
Model:
<?php
class add_model extends CI_Model {
public function insert_into_db(){
$post=$this->input->post();
$data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
$this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
}
function all_devices()
{
$this->db->select("id,device_type");
$this->db->from('device_type_table');
$query = $this->db->get();
return $query->result();
}
}
controller:
<?php
class Speed extends CI_Controller {
function insert_to_db()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('branch', 'Branch', 'trim|required|xss_clean');
$this->form_validation->set_rules('buinessUnit', 'BuinessUnit', 'trim|required|xss_clean');
$this->form_validation->set_rules('deviceType', 'DeviceType', 'trim|required|xss_clean');
$this->form_validation->set_rules('brand', 'Brand', 'trim|required|xss_clean');
$this->form_validation->set_rules('deviceModel', 'DeviceModel', 'trim|required|xss_clean');
$this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean|is_unique[hardware_assets.SN]');
$this->form_validation->set_rules('status', 'Status', 'trim|required|xss_clean');
$this->form_validation->set_rules('department', 'Department', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('notes', 'Notes', 'trim|required|xss_clean');
$this->form_validation->set_rules('computerName', 'ComputerName', 'trim|required|xss_clean');
/*
*/
$this->data['devices'] = $this->add_model->all_devices(); // calling Post model meAthod all_devices()
$this->load->view('pages/home', $this->data);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pages/home');//loading success view
}
else
{
$formSubmit = $this->input->post('submit');
if( $formSubmit == 'save' ){
$this->add_model->insert_into_db();
//loading success view
redirect('speed/insert_to_db');
}
}
}
}

You have to fetch your all devices data and brand data from controller then pass it in view.
In view you have to print all devices and band in foreach loop and create options.
For Example:
In Cotroller:
$this->load->model('add_model');
$data['devices']=$this->add_model->all_devices();
$data['brand']=$this->add_model->all_brands();
$this->load->view('your view for insert this data');
In Model:
function all_devices()
{
code for fetch all devices
}
function all_brand()
{
code for fetch all brands
}
In view :
Device Type:<select name="deviceType" >
<? foreach ($devices as $d) { ?>
<option value="<?php echo $d['device_id']?>"><?php echo $d['device_name']?></option>
<? }?>
</select><br/>
Brand:<select name="brand" >
<? foreach($brands as $b) { ?>
<option value="<?php echo $b['brand_id']?>"><?php echo $b['brand_name']?></option>
<? }?>
</select><br/>
Hope this will Help you.

Related

get option values from database codeigniter

I have a php form where I want to select options for a field Email from a list of values saved in a table emailaddress in the database. The table emailaddress has 2 fields 1. idemailaddress and 2. emailaddress, but I am still getting error trying to pull data, please help
Agreement_ctrl
$data['department_list'] = $this->Main_model->get_department_list();
$data['unit_list'] = $this->Main_model->get_unit_list();
$data['terms'] = $this->Main_model->get_terms_list();
$data['emailaddress_list'] = $this->Main_model->get_emailaddress_list();
$data['managers_list'] = $this->Main_model->get_managers_list();
$data['mtnlocation_list'] = $this->Main_model->get_mtnlocation_list();
$data['institution_list'] = $this->Main_model->get_institution_list();
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('gender', 'Gender', 'alpha|xss_clean');
$this->form_validation->set_rules('department', 'Department');
$this->form_validation->set_rules('unit', 'Unit', 'numeric|xss_clean');
Main Model File
}
function get_emailaddress_list()
{
$this->db->order_by('emailaddress', 'ASC');
$query = $this->db->get('emailaddress');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$result [$row->id] = $row;
}
return $result;
}else{
return FALSE;
}
}
Form Page
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="Email">Email Address:</label>
<div class="col-sm-9">
<select name="Email" class="Email form-control" id="Email">
<option value="" <?php echo set_select('Email', ''); ?> >-Select-</option>
<?php if($emailaddress_list){
foreach($emailadress_list as $emailaddress){ ?>
<option value="<?php echo $emailadress->idemailaddress?>" <?php echo set_select('Email', $emailaddress->idemailaddress); ?>><?php echo $emailaddress->emailaddress?></option>
<?php }
}?>
</select>
</div>
</div>
You don't need the foreach loop in your model, just return the query result.
function get_emailaddress_list(){
$this->db->order_by('emailaddress', 'ASC');
$query = $this->db->get('emailaddress');
$data = ($query->num_rows())? $query->result():false;
return $data;
}
Then you loop through this results in your view to create the select options:
<select name="Email" class="Email form-control" id="Email">
<?php if($emailaddress_list):?>
<option value="" disabled selected>-Select-</option>
<?php foreach($emailadress_list as $row): ?>
<option value="<?=$row->idemailaddress?>">
<?=$row->emailaddress?>
</option>
<?php endforeach;?>
<?php else:?>
no records found
<?php endif;?>
</select>

Codeigniter - Can't update data in MySQL Database

I have some problem with CodeIgniter, I can't update data in a record in CodeIgniter. 'id_user' is the primary key of my user table. I have tried to solve this problem but I couldn't. I hope someone can help with this problem. Thanks a lot :)
I have posted the code below :
HTML5 code :
<form action="<?php echo base_url(). 'crud/update'; ?>" method="post">
<input name="id_user" type="text" class="form-control" value="<?php echo $id_usernya ?>">
<div class="form-group">
<label>Alamat :</label>
<textarea name="alamat_user" class="form-control" rows="5" placeholder="Alamat" ><?php echo $usernya->alamat_user ?></textarea>
</div>
<div class="form-group">
<input name="kodepos_user" type="text" class="form-control" value="<?php echo $usernya->kodepos_user ?>">
</div>
<div class="form-group">
<input type="hidden" name="provinsi_user">
<select name="provinsi_id_user" class="form-control">
<option value="">Pilih Provinsi</option>
<option selected value="<?php echo $usernya->provinsi_user ?>"><?php echo $usernya->provinsi_user ?></option>
<?php echo $provinsi ?>
</select>
</div>
<div class="form-group">
<input type="hidden" name="kota_user">
<select name="kota_id_user" class="form-control">
<option value="">Pilih Kota</option>
<option selected value="<?php echo $usernya->kota_user ?>"><?php echo $usernya->kota_user ?></option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Simpan">
</form>
Controller :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
function edit($id_user){
$where = array('id_user' => $id_user);
$data['user'] = $this->m_data->edit_data($where,'user')->result();
$this->load->view('back/user/v_beranda',$data);
redirect('beranda/index');
}
function update(){
$id_user = $this->input->post('id_user');
$alamat_user = $this->input->post('alamat_user');
$kodepos_user = $this->input->post('kodepos_user');
$provinsi_id_user = $this->input->post('provinsi_id_user');
$kota_id_user = $this->input->post('kota_id_user');
$data = array(
'alamat_user' => $alamat_user,
'kodepos_user' => $kodepos_user,
'provinsi_id_user' => $provinsi_id_user,
'kota_id_user' => $kota_id_user
);
$where = array(
'id_user' => $id_user
);
$this->m_data->update_data($where,$data,'user');
redirect('beranda/index');
}
Model code :
<?php
class M_data extends CI_Model{
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
Initialize your Model like what your have done on your Controller
class M_data extends CI_Model{
public function __construct() {
parent::__construct();
$this->load->database();
}
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
try to print query to check what exact query getting executed
$this->db->last_query():
and which error its shows when you execute above query

codeigniter: Update table from another table with session

I'm having some challenge updating table a form joined with session userdata from another table. Each time I run, it displays undefined function. Please let me know where I'm getting it wrong so could fix it.Please find time to review it
I'm a newbie.
Here's the controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
Model
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
View
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
Error message
Message: Undefined property: Userinfo::$Userinfo_model
and
Message: Call to a member function update_user() on null
Thanks, I'm grateful
change your view as follows :
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
form will send data to server only if the element has a name
and you cannot submit a form multiple times. The above code will create update button for each row. So if you want to update all the records in a single updation, use update_batch() in codeigniter. and change view as follows :
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
for reference : https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
In your update_user function you didn't include the usermain_info model. That is why the error is coming.
Please create constructor function and include the model in it. I have updated your code. Try with this.
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>

update multiple form using codeiginiter

I want to update data for multiple users using codeginiter, I used the bellow script but it only updates for one user.
<?php echo form_open(base_url() . 'get/save_payment', array('role' => 'form')); ?>
<?php foreach ($mature as $key => $value) { ?>
<input type="text" class="form-control" value="<?php echo $value->id; ?>" readonly name="id[]">
<input type="text" class="form-control" value="<?php echo $value->name; ?>" readonly name="name[]">
<input type="text" class="form-control" value="0" readonly name="amount[]">
<?php } ?>
<button type="submit" class="btn btn-success">Confirm Request</button>
<?php echo form_close(); ?>
CONTROLLER
public function save_payment() {
$this->load->library('form_validation');
$this->form_validation->set_rules('id[]', 'id', 'required|trim');
$this->form_validation->set_rules('name[]', 'name', 'trim');
$this->form_validation->set_rules('amount[]', 'Amount', 'required|max_length[4]|trim|strip_tags');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('id', set_value('id'));
$this->session->set_flashdata('name', set_value('name'));
$this->session->set_flashdata('amount', set_value('amount'));
$this->payment();
} else {
$id = set_value('id[]');
$data['amount'] = set_value('amount[]');
$data['name'] = set_value('id[]');
$this->dashboard->update_ph($data,$id);
}
}
Here is the MODEL :
public function update_ph($data,$id)
{
$this->db->where('id', $id);
$this->db->update('gw_ph', $data);
}
Kindly explain on how I can go through this code and get it working without error.
Thanks
Codeigniter use 'name' attributte for get data from 'input' and you use same 'name' attributte for all of your readonly input

Update the database with codeigniter and display the updation in same page

I am trying to update the data to the database by php CodeIgniter and display the result in the same page during update. Below is my code.The problem is it didn't show any error message and didn't update the values in the database and It is not displaying any values in the page after update. Please help.
View Page
<?php
echo form_open('Welcome/service1');
?>
<input type="hidden" name="servicer_id" value="<?php echo $value['service_id']?>">
<input type="hidden" name="servicer_name" value="<?php echo $value['servicer_name']?>">
<input type="hidden" name="servicer_email" value="<?php echo $value['servicer_email']?>">
<input type="hidden" name="servicer_checkin" value="<?php echo $value['servicer_checkin']?>">
<input type="hidden" name="servicer_checkout" value="<?php echo $value['servicer_checkout']?>">
<input type="hidden" name="servicer_requestdate" value="<?php echo $value['servicer_requestdate']?>">
<input type="hidden" name="servicer_detail" value="<?php echo $value['servicer_detail']?>">
<input type="hidden" name="servicer_contact" value="<?php echo $value['servicer_contact']?>">
<input type="hidden" name="servicer_priority" value="<?php echo $value['servicer_priority']?>">
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Complete") {?> checked="checked"<?php } ?> name="current_status" value="Complete">Complete
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Ongoing") {?> checked="checked"<?php } ?> name="current_status" value="Ongoing">Ongoing
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Rejected") {?> checked="checked"<?php } ?> name="current_status" value="Rejected">Rejected
</label>
<button type="submit" class="btn btn-default">
<span>Status</span>
</button>
<?php
echo form_close();
?>
Controller page
public function service1()
{
$this->load->helper('url');
$page_id =$this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status();
$data['s']=$this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service.php',$data);
}
Model page
public function add_status()
{
this->load->database();
this->load->helper('url');
hotel_id=1;
servicer_name= $this->input->post('servicer_name');
servicer_email= $this->input->post('servicer_email');
servicer_checkin= $this->input->post('servicer_checkin');
servicer_checkout= $this->input->post('servicer_checkout');
servicer_requestdate= $this->input->post('servicer_requestdate');
servicer_detail= $this->input->post('servicer_detail');
servicer_contact= $this->input->post('servicer_contact');
servicer_priority= $this->input->post('servicer_priority');
status= $this->input->post('status');
service_id= $this->input->post('servicer_id');
data=array('hotel_id'=>$hotel_id,'servicer_name'=>$servicer_name,'servicer_email'=>$servicer_email,'servicer_checkin'=>$servicer_checkin,'servicer_checkout'=>$servicer_checkout,'servicer_requestdate'=>$servicer_requestdate,'servicer_detail'=>$servicer_detail,'servicer_contact'=>$servicer_contact,'servicer_priority'=>$servicer_priority,'status'=>$status);
this->db->where('service_id',$service_id);
this->db->update('service_hotel1',$data);
}
You are passing inputs to the model but they are getting passed to the controller function.. please update
Controller:
public function service1()
{
/** you are posting the form to this function and not to the model.
* which is why all input->post must be here
*/
$servicer_name = $this->input->post('servicer_name');
$servicer_email = $this->input->post('servicer_email');
$servicer_checkin = $this->input->post('servicer_checkin');
$servicer_checkout = $this->input->post('servicer_checkout');
$servicer_requestdate= $this->input->post('servicer_requestdate');
$servicer_detail = $this->input->post('servicer_detail');
$servicer_contact = $this->input->post('servicer_contact');
$servicer_priority = $this->input->post('servicer_priority');
$status = $this->input->post('status');
$service_id = $this->input->post('servicer_id');
$this->load->helper('url');
$page_id = $this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id);
$data['s'] = $this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service',$data); //also view must be loaded without .php so $this->load->view('App_stay/pages/hotel1_service',$data);
}
Model:
public function add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id)
{
$this->load->database();
$this->load->helper('url');
$hotel_id =1 ;
$data = array(
'hotel_id' => $hotel_id,
'servicer_name' => $servicer_name,
'servicer_email' => $servicer_email,
'servicer_checkin' => $servicer_checkin,
'servicer_checkout' => $servicer_checkout,
'servicer_requestdate' => $servicer_requestdate,
'servicer_detail' => $servicer_detail,
'servicer_contact' => $servicer_contact,
'servicer_priority' => $servicer_priority,
'status' => $status
);
$this->db->where('service_id',$service_id);
$this->db->update('service_hotel1',$data);
}

Categories