dropdown list not getting populated in CI - php

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";

Related

CodeIgniter dropdown from multiple table

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

Display search result from multiple dropdown selection

I want to display search result from multiple dropdown selection. I have 4 dropdowns. I have one submit button, but I'm still confused how to display search result when I click that submit button. I want to display "institution" and "address" column from "courseplace" table, based on query in mymodel.php below, and display 'No Results' when no match. I want to display search result like on http://www.bhinneka.com/category/notebook___laptop_gaming.aspx (institution image, institution name, and address).
Do I need new page to display search result?
I have tried so many ways but still failed :(
Thank you very much for the answer!
Below is my code (using Codeigniter) :
Controller.php
class Controller extends CI_Controller
{
public function index()
{
$this->load->view('home');
}
}
mymodel.php
class Mymodel extends CI_Model
{
public function getinstitution()
{
include('application/views/home.php');
$institution = $_POST['DropdownInstitution'];
$course = $_POST['DropdownCourse'];
$location = $_POST['DropdownLocation'];
$price = $_POST['DropdownPrice'];
$this->db->select('*');
$this->db->from('courseplace');
$this->db->where('1 = 1');
if ($institution != '') {
$this->db->where('institution', $institution);
}
if ($course != '') {
$this->db->where('course', $course);
}
if ($location != '') {
$this->db->where('location', $location);
}
if ($price != '') {
$this->db->where('price', $price);
}
$data = $this->db->get()->result_array();
return $data;
}
}
home.php
<div class="MenuSearching" id="Searching">
<form action="" method="post">
<div class="Menu1" id="Menu1">
<p> Lembaga</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownInstitution" autofocus required id="DropdownInstitution">
<option value="no" selected="selected">---Choose one---</option>
<option value="FlashCom">FlashCom</option>
<option value="InterNusa">InterNusa</option>
<option value="HexaCompare">HexaCompare</option>
</select>
</div>
</div>
<div class="Menu2" id="Menu2">
<p> Paket Kursus</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownCourse" autofocus required id="DropdownCourse">
<option value="no" selected="selected">---Choose one---</option>
<option value="web">Kursus Web Design</option>
<option value="flash">Kursus Flash Animation</option>
<option value="marketing">Kursus Internet Marketing</option>
</select>
</div>
</div>
<div class="Menu3" id="Menu3">
<p> Lokasi</p>
<div style="margin-top:-20px;margin-left:-3px;">
<select name="DropdownLocation" autofocus required id="DropdownLocation">
<option value="no" selected="selected">---Choose one---</option>
<option value="timur">Surabaya Timur</option>
<option value="barat">Surabaya Barat</option>
<option value="utara">Surabaya Utara</option>
<option value="selatan">Surabaya Selatan</option>
</select>
</div>
</div>
<div class="Menu4" id="Menu4">
<p> Harga</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownPrice" autofocus required id="DropdownPrice">
<option value="no" selected="selected">---Choose one---</option>
<option value="harga1"> kurang dari Rp.750.000</option>
<option value="harga2">Rp.750.000 - Rp.1.500.000</option>
<option value="harga3">Rp.1.500.000 - Rp.2.500.000</option>
<option value="harga4">lebih dari Rp.2.500.000</option>
</select>
</div>
</div>
<div>
<input name="submitbutton" type="submit" id="submitbutton" formmethod="POST" value="Submit">
</div>
</form>
You can use jQuery + Ajax to solve this non-refresh problem.
In you home.php file, send request to your controller and after model return data to controller, data would be append on your webpage.
I would suggest you go through codeigniter demos of model, view and controller.
Please check below code
Controller.php
//make a call to getinstitution function
//load our Mymodel class defined in mymodel.php
//call your view home
class Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('Mymodel');
}
public function index()
{
$params = array(
'DropdownInstitution' => $this->input->post('DropdownInstitution'),
'DropdownCourse' => $this->input->post('DropdownCourse'),
'DropdownLocation' => $this->input->post('DropdownLocation'),
'DropdownPrice' => $this->input->post('DropdownPrice'),
);
$data['result'] = $this->mymodel->getinstitution($params);
$this->load->view('home',$data);
}
}
mymodel.php
//No need to include('application/views/home.php');//
class Mymodel extends CI_Model
{
public function getinstitution($params)
{
$institution = $params['DropdownInstitution'];
$course = $params['DropdownCourse'];
$location = $params['DropdownLocation'];
$price = $params['DropdownPrice'];
$this->db->select('*');
$this->db->from('courseplace');
$this->db->where('1 = 1');
if ($institution != '') {
$this->db->where('institution', $institution);
}
if ($course != '') {
$this->db->where('course', $course);
}
if ($location != '') {
$this->db->where('location', $location);
}
if ($price != '') {
$this->db->where('price', $price);
}
$data = $this->db->get()->result_array();
return $data;
}
}
home.php
Please add below code to view home.php
<div>
<?php if(!empty($result)){
foreach($result as $value){ ?>
<?php echo $value ?>
<?php }
}else{
echo 'No results';
} ?>
</div><!--Result div-->

Change dropdown based on another one in CodeIgniter

I am new to CI. I need to Change some dropdown list according to another dropdown.
My controller code:
public function index()
{
$data = array();
$data['from_list'] = $this->from->list_from();
$data['to_list'] = $this->to->list_to();
$data['fromwhere_list'] = $this->fromwhere->list_fromwhere();
//$data['fromwhere_from_list'] = $this->fromwhere->list_fromwhere_from();
$data['towhere_list'] = $this->towhere->list_towhere();
$data['header'] = array('view'=>'header','data'=>array());
$data['main_content'] = array('view'=>'home','data'=>array());
$data['footer'] = array('view'=>'','data'=>array());
$this->load->view('template',$data);
//$this->load->view('home');
}
model code
function list_fromwhere()
{
$this->db->select('fromwhere_id,from_id,from_from_name');
$this->db->from('mt_from_from');
$query = $this->db->get();
return $query->result();
}
function list_from()
{
$this->db->select('from_id,from_name');
$this->db->from('mt_from');
$query = $this->db->get();
return $query->result();
}
My View code
<select class="" name="from" id="from" style="width: 20%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item)
{
?>
<option value="<?php echo $from_item->from_id?>"><?php echo $from_item->from_name?></option>
<?php
}
?>
<!-- <option value="volvo">Airport</option>
<option value="saab">Town</option>
<option value="mercedes">Hotel</option> -->
</select>
<select class="" name="from_place" id="from_place" style="width: 56%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item){
foreach($fromwhere_list as $fromwhere_item)
{
if($fromwhere_item->from_id == $from_item->from_id){
?>
<option value="<?php echo $fromwhere_item->fromwhere_id?>"><?php echo $fromwhere_item->from_from_name?></option>
<?php
}
}
}
?>
</select>
When we select the town, country by from dropdown list (firstone), I need to automatically change from place dropdown (secondone).

How to search record from db by selecting search category in codeigniter?

I have list of record in my index page. Now i want to search a particular record/records by choosing the category (e.g phone No, email etc).How can i do this? help..
Here is my view:
<?php
$attributes = array('class'=>'searchform', 'id'=>'searchform');
echo form_open('crud/searchCrud', $attributes);?>
<div class="formelements">
<div class="formlbl">
Search:
</div>
<div class="forminput">
<input type="text" name="searchon" id ="searchon"/>
</div>
<div class="searchtyp">
<select class="searchby" name="searchby">
<option value="0">---Select--- </option>
<option value="name">Name</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
</div>
<div class="searchinput">
<input type="submit" name="search" value="Search" />
</div>
</div>
<?php echo form_close();?>
Here is My controller:
public function searchCrud()
{
$records = $this->crud_mdl->searchCrud();
foreach ($records as $record) {
echo $record->name;
}
}
Here is My Model:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$this->db->get('test')->result();
return $this->db->like($searchon, $searchby);
}
1) You should never access POST data directly in the Model.
2) Always collect them in Controller and then pass it to the Model.
3) Also always try to create your Model functions, re-usable.
Modified your code a bit. Try running it.
Controller:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$records = $this->crud_mdl->searchCrud($searchby, $searchon);
foreach ($records as $record)
{
echo $record->name;
}
}
Model:
public function searchCrud($searchby, $searchon)
{
$this->db->like($searchon, $searchby);
$query = $this->db->get('test');
if($query->num_rows() > 0)
return $query->result();
else
return FALSE;
}

Drop down menu in codeigniter displays nothing

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>

Categories