i have data like this
kdae | name
1 | MBC
2 | BBC
3 | CDC
i want to display inside my form_dropdown
<1 ---- MBC>
<2 ---- BBC>
<3 ---- CDC>
this my code in controller
$kdaes = $this->master_order->get_list_kdaes();
$opt = array('' => 'All AE');
foreach ($kdaes as $kdae) {
$opt[$kdae] = $kdae;
}
$data['form_kdae'] = form_dropdown('',$opt,'','id="kdae" name="kdae" class="form-control"');
this is code in my model
public function get_list_kdaes()
{
$this->db->select('kdae','Nama');
$this->db->from($this->table1);
$this->db->order_by('kdae','asc');
$query = $this->db->get();
$result = $query->result();
$kdaes = array();
foreach ($result as $row)
{
$kdaes[] = $row->kdae;
}
return $kdaes;
}
and code for my view
<input name="kodecat" type="text" hidden>
<div class="form-group">
<label for="kdae" class="col-sm-3 control-label">Kode AE</label>
<div class="col-sm-3">
<?php echo $form_kdae; ?>
</div>
</div>
but if i run my form_dropdown just display like this
1
2
3
theres any solution?
syntax is from_dropdown(name,options,selected_option,extra attribute )
so it should be
form_dropdown('kdae',$opt,'','id="kdae" class="form-control"');
You are having an unnecessary foreach loop in your model, just do:
return $query->result();
in your controller you retrieve the data returned from the model:
foreach ($kdaes as $row) {
$opt[$row->kodeae] = $row->kodeae. ' --- '.$row->Nama;
}
hint: pay attention in your select clause, as you have columns with lowercase (kodae) and others with uppercase (Nama), check if this is corect...
Try this code:
public function get_list_kdaes()
{
$this->db->select('kdae','Nama');
$this->db->from($this->table1);
$this->db->order_by('kdae','asc');
$query = $this->db->get();
$result = $query->result_array();
$kdaes = array();
foreach ($result as $row)
{
$kdaes[$row['kdae']] = $row['Nama'];
}
return $kdaes;
}
Related
I have login page but i am dont know where i can take this alerts ,
i will take my script like this
echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('Your Login Succesfully ,'); window.location.href='home'; </SCRIPT>");
but i dont know how to make if else condition on my query in the model and contoller .I using num_rows but its didnt work .
my controller
public function login()
{
$u = $this->input->post('username');
$p = $this->input->post('password');
$data = $this->app_model->getlogindata($u,$p);
return $data;
}
my model
public function getlogindata($username,$password)
{
$u = $username;
$p = md5($password);
$cek_login = $this->db->get_where('login', array('username' => $u,'password'=>$p));
if(count($cek_login->result())>0)
{
foreach ($cek_login->result() as $qck)
{
if($qck->level=='puskesmas')
{
// $ambil_data = $this->db->get_where('puskesmas',array('id_puskesmas' => $u));
$this->db->select('*');
$this->db->from('puskesmas');
$this->db->join('login', 'puskesmas.id_puskesmas=login.id_puskesmas');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_puskesmas'] = $qad->id_puskesmas; //
$sess_data['nama_puskesmas'] = $qad->nama_puskesmas;
$sess_data['alamat_puskesmas'] = $qad->alamat_puskesmas;
$sess_data['nama_petugas'] = $qad->nama_petugas;
$sess_data['nomor'] = $qad->nomor;
$sess_data['email'] = $qad->email;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'puskesmas');
} //xammp mu aktif?
elseif($qck->level=='dinas')
{
//$ambil_data = $this->db->get_where('dinas',array('id_dinas' => $u));
$this->db->select('*');
$this->db->from('dinas');
$this->db->join('login', 'dinas.id_dinas=login.id_dinas');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_dinas'] = $qad->id_dinas;
$sess_data['nama_dinas'] = $qad->nama_dinas;
$sess_data['alamat_dinas'] = $qad->alamat_dinas;
$sess_data['kode_pos'] = $qad->kode_pos;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'dinas');
}
elseif($qck->level=='admin')
{
//$ambil_data = $this->db->get_where('admin',array('id_admin' => $u));
$this->db->select('*');
$this->db->from('admin');
$this->db->join('login', 'admin.id_admin=login.id_admin');
$this->db->where('username', $u);
$ambil_data = $this->db->get();
foreach ($ambil_data->result() as $qad)
{
$sess_data['logged_in'] = 'yes';
$sess_data['id_admin'] = $qad->id_admin;
$sess_data['nama_admin'] = $qad->nama_admin;
$sess_data['alamat_admin'] = $qad->alamat_admin;
$sess_data['status'] = $qad->status;
$sess_data['level'] = $qad->level;
$this->session->set_userdata($sess_data);
}
header('location:'.base_url().'admin');
}
else{
echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('Record Updated Successfully'); window.location.href='web'; </SCRIPT>");// i add this and still didnt work
}
}
}
}
in this script i just didnt know where i can take this alerts and what a parameter can i use to add this alerts . like if num_rows = 1 , echo = blablabla
thanks you sir
you have to use flash data in code igniter or you can use ajax callback in success you can alert what you want and redirect to whatever you like.
Controller
$this->session->set_flashdata('error_message', 'Incorrect Username or Password ! Please try again.');
redirect(URL.'backend/login');
View
<?php
if ($this->session->flashdata('error_message'))
{
?>
<div class="alert alert-danger" style="color: #fff;">
<!-- <button class="close" data-close="alert"></button> -->
<span><?php echo $this->session->flashdata('error_message'); ?></span>
</div>
<?php
}
if ($this->session->flashdata('ok_message'))
{
?>
<div class="alert alert-success">
<!-- <button class="close" data-close="alert"></button> -->
<span><?php echo $this->session->flashdata('ok_message'); ?></span>
</div>
<?php
}
?>
try this.i hope it will help you.
else{
echo '<script language="javascript">';
echo 'alert("Record Updated Successfully")';
window.location.href = 'web';
echo '</script>';
}
I have two tables state and city
state table has two column
state_id | state_name
---------|-----------
1 | Alabama
2 | Alaska
3 | Arizona
and City table has two column as well
state_name | city_name
-----------|----------
1 |
1 |
1 |
2 |
2 |
3 |
Controller
function city()
{
$data = array();
$data['states']=$this->state_model->state_query();
$data['cities']=$this->cities_model->cities_query();
$this->load->view('city', $data);
}
Model
public function state()
{
$this->db->select('state_name');
$this->db->from('state');
$this->db->join('city', 'city.state_name = state.state_name');
$result = $this->db->get();
}
View
<?php foreach ($cities as $city) { ?>
<?php echo $city->city_name; ?>
<?php echo $city->state_name; ?>
<?php } ?>
the above code is the controller, model, and view respectively. The issue is that I want to echo state_name but not working, the city name is displaying as expected. Please, I need suggestion on what to do.
Your Model Should Be:
public function state()
{
$this->db->select('s.state_name as state,c.city_name as city, c.state_name as state_id');
$this->db->from('state as s');
$this->db->join('city as c', 'c.state_name = s.state_id ');
$result = $this->db->get();
$data = $result->result_array();
if(isset($data) && !empty($data))
{
return $data;
} else {
return FALSE;
}
}
Your Controller Should be:
public function city()
{
$this->load->model('your_model');
$city_state_data = $this->your_model->state();
// I have just added example of getting data from model to controller
// and then passing it to view. You need to use it as you need
$this->load->view('your_view', ['city_state_data' => $city_state_data]);
}
View:
<?php foreach ($city_state_data as $single_city_state) { ?>
<?php echo $single_city_state['state']; ?>
<?php echo $single_city_state['city']; ?>
<?php } ?>
Model
public function state()
{
$this->db->select('state.state_name as state_tbl1, city.state_name as state_tbl2, city.city_name');
$this->db->join('city', 'city.state_name = state.state_name');
$result = $this->db->get('state');
return $result->result();
}
Controller
function city()
{
$data['states']=$this->state_model->state();
$this->load->view('city', $data);
}
View
<?php foreach ($states as $city) {
echo $city->city_name;
echo $city->state_tbl1;
echo $city->state_tbl2;
} ?>
have a form like this. This is an update form, i just need to update qty foreach product in cart.
But i have tried to explode each results and not work... it return object2array conversion error... its the first time that i get this error How i can save this in DB?
<form action="update">
#foreach($products as $product)
<input type="text" name="products[]">
<input type="text" name="qty[]">
#endforeach
<input type="submit" calss="btn btn-primary">
This is my controller:
Route::post('aggiorna', function() {
$quantita = Input::all();
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$i = $i++;
}
return Redirect::to('carrello');
});
Thanks in advance.
you do the iteration for each $carreli that exist on your database, however you never save the value.
Route::post('aggiorna', function() {
$quantita = Input::all();
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$i = $i++;
$carrelo->save();
}
return Redirect::to('carrello');
});
add the $carrelo->save(); after you update the value in order to save it on db.
Also careful when you use Input::all();. That means that your data array contains both products and quantities. I would suggest using the following code:
Route::post('aggiorna', function() {
$quantita = Input::get('qty');
$products = Input::get('products');
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get();
$quantita = explode(',', $quantita);
$products = explode(',', $products);
$i = 0;
foreach($carrelli as $carrello) {
$carrello->quantita = $quantita[$i];
$carrello->products = $products[$i];
$i = $i++;
$carrelo->save();
}
return Redirect::to('carrello');
});
However since i do not know what you are trying to achieve, I posted both solutions here.
In this codeigniter model i have this query testing if value entered in input exists in database..
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Behold the controller i'd like to display value grabbed in input in view inscription.php
function search()
{
$data['row'] = $this->site_model->get_search_form();
$this->load->view('acceuil/aside');
$this->load->view('acceuil/inscription', $data);
}
My issue is how to display in that view input value and a form if this value exists in database ?
I have tried like this but i need help :
inscription view:
<?=form_open('navigation/search');?>
<input type="text" name="search1" id="search1" required />
<input type='submit' value='Display' />
<?=form_close();?>
I try to display the form like this but i don't know how to display as well the input value entered
<?php
if( $row > 0 )
{
?>
les champs du formulaire ici....
<?php
}else
{ }
?>
In your view you can check if the variable $row is not null (because it will be null when no rows are found):
if ($row !== null) {
// do stuff
}
You can modify the model function to return some other value if no rows are found, for example, by setting the $data to an empty array:
function get_search_form() {
$match = $this->input->post('search1');
$this->db->where('numero',$match);
$this->db->where('inscris','non');
$q = $this->db->get('transaction');
$data = array(); // <--- here
if($q->num_rows()>0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
And then in the view you can simply loop the data:
foreach($row as $r) {
// do stuff
}
or you can implode the array to use as the input value:
<input type="text"
name="search1"
id="search1" required
value="<?php echo implode(' ', $row); ?>" />
You could also use html entities here, in case double quotes are possible to appear in what your model function returns (I have no idea what it returns).
you can access $data global object like this check
<?php
if( isset($row))
{
foreach($row as $v){
//Do the operations here
}
}else
{
//Do the operations here
}
?>
I have two table in my database, One is Categories and the other is Sub_Categories, I want to display them like this:
Categorie 1
sub categoie 1
sub categoie 2
sub categoie 3
sub categoie 4
Categorie 2
sub categoie 1
sub categoie 2
sub categoie 3
sub categoie 4
But i don't know how to do this.
In my Database table i have this fields :
Categories: ID, Name, Icon.
Sub_Categories: ID, Categ_id, Name
This should work;
public function get_categories()
{
$query = $this->db->get('Categories');
$return = array();
foreach ($query->result() as $category)
{
$return[$category->id] = $category;
$return[$category->id]->subs = $this->get_sub_categories($category->id); // Get the categories sub categories
}
return $return;
}
public function get_sub_categories($category_id)
{
$this->db->where('Category', $category_id);
$query = $this->db->get('Sub_Categories');
return $query->result();
}
All this does is get's all the categories, but then gets all the subcategories for each of the categories. Calling the get_categories() function should return an object in the format you want.
I hope this helps.
Edit
You would call the get_categories function from your controller and pass it to the view;
$data['categories'] = $this->your_model->get_categories();
$this->load->view('view_file', $data);
Then within your view you would display them like this;
<ul>
<?php
foreach ($categories as $category)
{
?>
<li><?php echo $category->name; ?>
<?php
if(!empty($category->subs)) {
echo '<ul>';
foreach ($category->subs as $sub) {
echo '<li>' . $sub->name . '</li>';
}
echo '</ul>';
}
?>
</li>
<?php
}
?>
</ul>
Database
image for database table
Try this
in Model
public function getCategories()
{
$query = $this->db->query('select * from categories where cat_parent=0');
return $query->result_array();
}
public function getCategoriesSub($parent)
{
$query = $this->db->query("select * from categories where cat_parent='$parent'");
return $query->result_array();
}
in Controller
public function categories()
{
$data['mcats'] = $this->admin_model->getCategories();
foreach($data['mcats'] as $key =>$val){
$subcats = $this->admin_model->getCategoriesSub($val['cid']);
if($subcats){
$data['scats'][$val['cid']] = $subcats;
}
}
$this->load->view('admin/header');
$this->load->view('admin/category_list', $data);
$this->load->view('admin/footer');
}
In view
<ul>
<?php
foreach ($mcats as $key =>$val)
{
?>
<li><?php echo $val['cat_name']; ?>
<ul>
<?php
foreach ($scats[$val['cid']] as $sub) {
echo '<li>' . $sub['cat_name'] . '</li>';
}
?>
</ul>
</li>
<?php
}
?>
</ul>
its already working code - i think it is usefull