Passing data from Controller to View with CodeIgniter - php

I'm having difficulty with display data from the db to dropdown.
This is what I have tried:
form_model.php
function getEmployee()
{
$this->db->select('username');
$query = $this->db->get('tbl_usrs');
return $query->result();
}
form_controller.php
public function evaluate()
{
$data['employee'] = $this->form_model->getEmployee();
$this->load->view('evaluate_view');
}
evaluate_view.php
<select class="form-control">
<?php
foreach($employee as $row)
{
echo '<option value="'.$row->username.'">'.$row->username.'</option>';
}
?>
</select>
It's giving me an error saying I have an unidentified variable employee in my view file. I've seen problems relating to this but so far all their solutions didn't work for me.

When you load the view you have to send the data like this:
$this->load->view('evaluate_view', $data);

Related

Data not displaying in View from controller codelgniter

I am learning CodeIgniter; in my program I am trying to populate a dropdown from mysql database but I seemed to be doing something wrong.
Please find below what I tried:
Model
In my model I tried to use different select methods I found online but all failed so I stick with the below method and still failed.
function getAllVendors()
{
$this->db->select('DISTINCT SUBSTRING(product_code,1,3) as vendor')
->order_by('vendor');
$q = $this->db->get('tec_sale_items');
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Controller
public function allVendors(){
$this->data['vendors'] = $this->vendors_sales_model->getAllVendors();
$this->page_construct('reports/vendors_sales_details', $this->data);
}
View
<select class="form-control">
<option value="">All</option>
<?php
foreach($vendors as $ven)
{
echo '<option value="'.$ven['vendor'].'">'.$ven['vendor'].'</option>';
}
?>
</select>
Could someone please point me to what I am doing wrong.
As I see you might be forget to call view into your controller. Load view in the controller $this->load->view("your view file name") and pass the data you got from your database.
First make sure that you are getting proper data from model. And as I see your are using current object of class $this->data['vendors'] so you have to use same in the code
<select class="form-control">
<option value="">All</option> <?php
foreach($this->data['vendors'] as $ven)
{
echo '<option value="'.$ven['vendor'].'">'.$ven['vendor'].'</option>';
}
?>
</select>
May be it's helpful 🙂.
And you should use ci4 it's 3rd version of ci and it's no longer maintained. For better security and performance change php server 7.4 and above.

How to show dynamic value exist in selectpicker?

<select class="selectpicker" multiple data-live-search="true">
<?php
$this->db->select('*');
$this->db->from('candidate_team');
$where = "candidate_id='".$id."' and member_type='Recruiter'";
$this->db->where($where);
$qqs = $this->db->get();
$result = $qqs->result_array();
foreach($result as $row)
{
echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
}
?>
</select>
I have a multi-select dropdown. Now, What I want if a value exists in the database then the value already show in select picker. So, How can I do this? Please help me.
Thank You
You need to get DB record in controller and then pass it to view to display with selectpicker
Controller
I'm supposing you've configured database
public function getselectpiker(){
$this->load->model('model_name');
$data['result'] = $this->model_name->get_data($id); // Pass candidate id here
$this->load->view('file_name', $data);
}
Model
public function get_data($id){
return $this->db->get_where('candidate_team', ['candidate_id' => $id, 'member_type' => 'Recruiter'])->result_array();
}
View
<select class="selectpicker" multiple data-live-search="true">
<?php
foreach($result as $row){
echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
}
?>

CodeIgniter Dropdown menu

I have a view (myView) in which there is a form. The form action is myController/myFunction1 which is used to validate the input variables in the form and insert it to the database by calling a model function. This works perfectly fine.
Now, I need a dropdown box inside the form, for which the values will be fetched from a table (called business) in the db.
This is the code I wrote in my model to fetch the values
public function get_dropdown_list() {
$this -> db -> select('business_name');
$result = $this -> db -> get('business');
if ($result -> num_rows() > 0) {
foreach ($result->result_array() as $row) {
$new_row['value'] = htmlentities(stripslashes($row['business_name']));
$row_set[] = $new_row;
}
}
return $row_set;
}
I'm not entirely sure if this is correct.
What I need to know is, if this is correct, what should be the code inside the controller and the view to display the result as a dropdown in the form in the myView.
And if this model itself is wrong, how do I get it working?
P.S. : I'm new to CodeIgniter. I have been going through S.O and various other sites to get this thing working for quite a bit of time now. This might seem to be a repeated question for which I'm really sorry, because I could not find a solution from the already available discussions dealing with the same issue. Any help is very much appreciated.
try Model :-
public function get_dropdown_list() {
$this -> db -> select('business_name');
$result = $this -> db -> get('business');
if ($result -> num_rows() > 0) {
return $result->result_array();
}
else {
return false;
}
}
Controller :-
1. include model in your controller
2. call the function and send data to view.
$this->load->model('model_name');
$this->data['dropdown'] = $this->model_name->get_dropdown_list();
$this->load->view('yourview', $this->data);
get value in view:-
print_r($dropdown)
Loop your data and make a dropdown
<select name="dropdown">
<?php foreach($dropdown as $d) {?>
<option value="<?php echo $d;?>"><?php echo $d;?></option>
<?php }?>
</select>
Call This function in controller for getting your records from DB
$data['records'] = $this->my_model->get_data();
In my_model.php
function get_data()
{
$query = "select * from my_tab";
$res = $this->db->query($query);
if ( $res->num_rows )
{
return $res->row_array();
}
return false;
}
In view.php
<select>
<?for($i=0;$i<count($records);$i++)
{
?>
<option>$records[$i]->name</option>
<?php } ?>
</select>

Displaying data from database using Codeigniter

I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.

Populating <select> with mysql data dynamically using CodeIgniter

I'm stacked on this matter. I have a model that retrieves data from a mysql database
class load_model extends CI_Model{
function __construct(){
parent::__construct();
}
Function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->SupplierID] = $row->Name;
}
return ($data);
}
}
?>
This model submits value to a function in my controller
public function getSupplier()
{
$this->load->model('load_model');
$data['unit'] = $this->load_model->loadsuppliers();
$this->load->view('SupplierMGT', $data);
}
and I want to display the retrieved data to my view as a combo box. I tried to check if I am able to retrieve database values using echo json_encode($data) and it returns {"unit":{"2":"test","3":"Delta"}} ,
Could you help me with this? I tried using
<?php foreach($unit as $result):
print_r($result);
endforeach;?>
to check if i am able to pass the value but i failed.
Small changes in the model:
function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
if($records->num_rows() > 0){
$data = $records->result_array();
}
return ($data);
}
In your view SupplierMGT.php write this:
<select name="" id="" multiple="">
<?php
if(isset($unit) && is_array($unit) && count($unit) > 0){
foreach($unit as $key=>$each){
?>
<option value="<?=$each['SupplierID']?>"><?=$each['Name']?></option>
<?php
}
}
?>
</select>

Categories