How to autocomplete select option in CodeIgniter - codeigniter

I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>

Related

Content not showing in there correct module positions

I have a controller that shows modules for my each position
Array
(
[0] => Array
(
[layout_module_id] => 1
[layout_id] => 1
[module_id] => 1
[position] => column_left
[sort_order] => 1
)
[1] => Array
(
[layout_module_id] => 2
[layout_id] => 1
[module_id] => 2
[position] => column_left
[sort_order] => 2
)
)
Above currently I have only two modules set and the are in the position of column left.
Because the position views are out side of the foreach loop they are picking up that module even though not set for that position? As shown in image.
Question: How can I make sure that the module will only display in its set position view.
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$modules = $this->getlayoutsmodule($layout_id);
echo '<pre>';
print_r($modules);
echo "</pre>";
$data['modules'] = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
// Position Views
$data['column_left'] = $this->load->view('column_left', $data, TRUE);
$data['column_right'] = $this->load->view('column_right', $data, TRUE);
$data['content_top'] = $this->load->view('content_top', $data, TRUE);
$data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE);
// Main view
$this->load->view('welcome_message', $data);
}
public function getlayoutsmodule($layout_id) {
$this->db->select('*');
$this->db->from('layouts_module');
$this->db->where('layout_id', $layout_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
Each of the position views have the same foreach loop
<?php if ($modules) { ?>
<?php foreach ($modules as $module) { ?>
<?php echo $module;?>
<?php } ?>
<?php }?>
main view
<div class="container">
<div class="row">
<?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
Got it working I have had to use a foreach switch statement
<?php
class Welcome extends CI_Controller {
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$column_left = '';
$column_right = '';
$content_top = '';
$content_bottom = '';
$position = array('column_left', 'column_right', 'content_top', 'content_bottom');
foreach ($position as $key) {
switch ($key) {
case 'column_left':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_left');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_left = $this->load->view('column_left', $data, TRUE);
break;
case 'column_right':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_right');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_right = $this->load->view('column_right', $data, TRUE);
break;
case 'content_top':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_top');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_top = $this->load->view('content_top', $data, TRUE);
break;
case 'content_bottom':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_bottom');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_bottom = $this->load->view('content_bottom', $data, TRUE);
break;
}
}
$data['column_left'] = $column_left;
$data['column_right'] = $column_right;
$data['content_top'] = $content_top;
$data['content_bottom'] = $content_bottom;
$this->load->view('welcome_message', $data);
}
}

Not displaying correct out put on view

I have two database tables called forum and forum_category.
In the forum table "General" and "Vegetables"
On the forum_categories there are two forum categories for General and one in Vegetables.
But for some reason my forum Vegetables is showing multiple categories should be only displaying one.
My question is how can I get the correct categories to display in
correct forum.
Note: It shows the correct results in the print_r image
Preview
View
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php foreach ($forums as $forum) {?>
<div class="panel panel-custom">
<div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
<div class="panel-body">
<?php foreach ($forum['categories'] as $category) {?>
<table class="table">
<tbody>
<tr>
<td><?php echo $category['category_name'];?></td>
</tr>
</tbody>
</table>
<?php }?>
</div>
</div><!-- Panel -->
<?php }?>
</div>
</div>
Model Functions
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
Printed Results
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories_results = $this->get_result_categories($result['forum_id']);
echo "<pre>";
print_r($categories_results);
echo "</pre>";
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
}
Think I have the solution now
Solution
Placing $categories = array();
above $categories_results did the trick
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories = array();
$categories_results = $this->get_result_categories($result['forum_id']);
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
Image

A PHP error was encountered severity:trying to get non-object file name:

view part where the error occured
<select name="standard" class="form-control">
<option <?php if($course->medium =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course->medium =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>
</select>
model part
public static function getStandard($id){
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
foreach($res->result() as $value) {
return $value;
}
}
controller part
public function add_standard($action='',$id='') {
$this->load_syles();
$course = array();
if ($action == 'edit' and $id != '' and is_numeric($id)) {
$course = $this->news_model->getStandard($id);
}
$this->data['course'] = $course ;
$this->data['title'] = 'Add Standard';
$this->data['page_header'] = "Add Standard";
$this->data['page_header_desc'] = "Add Standard";
$details=$this->news_model->viewAllStandard();
$this->data['details']=$details;
$this->data['count']=count($details);
$this->data['is_edit']=$action;
You wrote
$course = array()
and then used
$course->medium
as if it was an object
you should use $course['medium'] in the view(html)
Change your model like
public static function getStandard($id)
{
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
return $res->row_array();
}
Change $course->medium to $course['medium']
<select name="standard" class="form-control">
<option <?php if($course['medium'] =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course$course['medium'] =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>

Load table from database to dropdown CodeIgniter

I have trouble with loading the table to the form_dropdown. I have tried any solution from the same trouble with me but its not working.
Here's the controller code:
<?php
class Registrasi extends Superuser_Controller
{
public $data = array(
'halaman' => 'registrasi',
'main_view' => 'program/administrasi/registrasi_list',
'title' => 'Data Registrasi',
);
public function __construct()
{
parent::__construct();
$this->load->model('program/administrasi/Registrasi_model', 'registrasi_model');
}
public function index()
{
$registrasi = $this->registrasi_model->get_all_registrasi_data();
$this->data['registrasiData'] = $registrasi;
$this->load->view($this->layout, $this->data);
}
public function tambah()
{
$this->data['namaNegara'] = $this->registrasi_model->get_nama_negara();
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
}
Here's the model code:
<?php
class Registrasi_model extends MY_Model
{
protected $_tabel = 'tb_registrasi';
protected $form_rules = array(
array(
'field' => 'Negara_Tujuan',
'label' => 'Negara Tujuan',
'rules' => 'trim|xss_clean|required|max_length[50]'
)
);
public $default_value = array(
'Negara_Tujuan' => ''
);
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
return $query->result();
}
}
Here's the view code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>
The trouble I'm having is: Invalid argument supplied for foreach()
Your controller code must be :-
public function tambah()
{
$namaNegara[''] = 'select a option';
$namaNegaras = $this->registrasi_model->get_nama_negara();
foreach($namaNegaras as $namaNegaranew)
{
$namaNegara[$namaNegaranew->id] = $namaNegaranew->Nama_Negara ;
}
$this->data['namaNegara'] = $namaNegara;
$this->data['main_view'] = 'program/administrasi/registrasi_form';
$this->data['form_action'] = site_url('program/administrasi/registrasi/tambah');
// Data untuk form.
if (! $_POST) {
$registrasi = (object) $this->registrasi_model->default_value;
} else {
$registrasi = $this->input->post(null, true);
}
// Validasi.
if (! $this->registrasi_model->validate('form_rules')) {
$this->data['values'] = (object) $registrasi;
$this->load->view($this->layout, $this->data);
return;
}
}
And in view in place of
<?php
foreach($namaNegara as $row)
{
echo form_dropdown('NegaraTujuan', $row->Nama_Negara);
}
?>
Simply use
<?php echo form_dropdown('NegaraTujuan', $namaNegara , set_value('NegaraTujuan')); ?>
I think i have the answer for my problem:
Controller code (still same, no change whatsoever)
Model code: change the function into
public function get_nama_negara()
{
$query = $this->db->query('SELECT Nama_Negara FROM tb_negara_tujuan');
$dropdowns = $query->result();
foreach ($dropdowns as $dropdown)
{
$dropdownlist[$dropdown->Nama_Negara] = $dropdown->Nama_Negara;
}
$finaldropdown = $dropdownlist;
return $finaldropdown;
}
View code:
<div class="container">
<h2>Form Registrasi</h2>
<hr>
<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
<div class="container">
<div class="row">
<div class="form-group has-feedback <?php set_validation_style('Negara_Tujuan')?>">
<?php echo form_label('Negara Tujuan', 'negara_tujuan', array('class' => 'control-label')) ?>
<?php echo form_dropdown('NegaraTujuan', $namaNegara, set_value('NegaraTujuan'), 'class="dropdown"'); ?>
<?php set_validation_icon('Negara_Tujuan') ?>
<?php echo form_error('Negara_Tujuan', '<span class="help-block">', '</span>');?>
</div>
<?php echo form_button(array('content'=>'Simpan', 'type'=>'submit', 'class'=>'btn btn-primary', 'data-confirm'=>'Anda yakin akan menyimpan data ini?')) ?>
</div>
</div>
<?php echo form_close() ?>
</div>

create a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>

Resources