show retrieve data in drop down list in codeigniter - php

Controller code
<?php
class Booking_Controller extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data['result']=$this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
}
}
And its model code
<?php
class Books_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_restaurants()
{
$sql = "SELECT id, names FROM restaurants ";
$query = $this->db->query( $sql );
return $query->result();
}
}
Plz guide me what code i written in view file that i get name list in dropdown form....

In your controller:
$result = $this->Books_model->get_restaurants();
$data['select'] = Array();
foreach($result as $r){
$data['select'][$r->id] = $r->names;
}
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
In your view:
<?php echo form_dropdown('restaurant', $select); ?>

write following code to show dropdown in views.
<select name="">
<?php foreach($result as $row){?>
<option value="<?php echo $row->id; ?>"><?php echo $row->names;?></option>
<? }?>
</select>
$data['result'] is converted to $result variable in view and hence you can use it as above mentioned.

Related

Display data from database with Select Option Codeigniter

first I want to apologize because I just learned Codeigniter, I have problems to display data from the database by using the Select Option, there is no error but the data does not appear, for your information, i have joined 3 tables.
Here's my Controller
class Harga extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_harga');
$this->load->helper('url');
$this->load->database();
}
function index(){
$this->load->helper('form');
$data['tabel_harga'] = $this->m_harga->tampil_data();
$this->load->view('v_harga',$data);
}
Here's my Model
class M_harga extends CI_Model{
function tampil_data(){
$this->db->order_by('id_harga','ASC');
return $this->db->from('tabel_harga')
->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
->get()
->result();
}
and Here's my Views
<select class="form-control">
<option value="">All</option>
<?php
foreach($tabel_harga as $u)
{
echo '<option value="'.$u['id_vendor'].'">'.$u['nama_vendor'].'</option>';
}
?>
</select>
I will be very grateful if you help me, thank you guys.
The data doesn't appear probably because you're using result() which returns object and you're getting data as array in your view.
Model
class M_harga extends CI_Model{
function tampil_data(){
$this->db->select('*');
$this->db->from('tabel_harga');
$this->db->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor', 'INNER');
$this->db->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari', 'INNER');
$this->db->order_by('id_harga','ASC');
$query = $this->db->get()->result_array(); // use result_array() instead of result() as you're getting value as an array in your view.
return $query;
}
}
Also, make sure to check $tabel_harga for values in your view ie
<select class="form-control">
<option value="">All</option>
<?php
if(!empty($tabel_harga)){
foreach($tabel_harga as $u){
?>
<option value="<?php echo $u['id_vendor']; ?>"><?php echo $u['nama_vendor']; ?></option>
<?php
}
}
?>
</select>
Hope this helps you.
try this
view
<select class="form-control">
<option value="">All</option>
<?php
foreach($tabel_harga as $u)
{
echo '<option value="'.$u->id_vendor.'">'.$u->nama_vendor.'</option>';
}
?>
</select>
Models
class M_harga extends CI_Model{
function tampil_data(){
$this->db-join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
$this->db-join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
$this->db->order_by('id_harga','ASC');
$sql = $this->db->get('tabel_harga');
return $sql->result(); // returns an array of objects
}
controller
class Harga extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('M_harga');
$this->load->helper(array('url','form'));
}
function index(){
$data['tabel_harga'] = $this->M_harga->tampil_data();
$this->load->view('v_harga',$data);
}
db->select("name, value");
$this->db->from('settings');
$query = $this->db->get();
if ($query->num_rows())
{
foreach ($query->result_array() as $row)
{ // Your data is coming from multiple rows, so need to loop on it.
$siteData[$row['name']] = $row['value'];
}
}
return $siteData;
}

How to pass result from Model to Controller in CodeIgniter?

I want to get data from the database and display it on a webpage using CodeIgniter. I coded my controller, model and view as follows.
Controller;
//HomeController
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model;
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
return $query->unbuffered_row('object');
}
}
?>
View;
//HomeView
<?php
echo "Recoeds from database<br>";
while($records)
{
echo $records->name." ".$records->age."</br>";
}
?>
But this code doesn't print anything on the screen.(echo "Records from database<br>"; )
So I tried the following code given in the CodeIgniter documentation and echoed the result in the model itself rather than return it to the controller and then to the views.It worked fine.
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
while ($row = $query->unbuffered_row())
{
echo $row->name;
echo $row->age;
}
}
}
?>
My question is how do we return the result of the unbuffered_row() method into the controller and then to the view as per MVC architecture? We can get the output by echoing result at the model itself but it is against the purpose of the MVC architecture.
You should use return $query->result(); and then get the right object in the view or controller (that's up to you).
Try This One
controller
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$data = $this->db->query('SELECT * FROM data');
return array('count'=>$data->num_rows(), 'data'=>$data->result(),'first'=>$data->row());
}
}
?>
View
<?php
echo "Recoeds from database<br>";
foreach($record['data'] as $row)
{
echo $row->name." ".$row->age;
}
?>
I simulated your code in local.Your mistake is here.$records is object .And your while loop has not condition for leaving the loop
<pre>
<?php
var_dump($records);
echo "Recoeds from database<br>";
foreach($records as $value) {
echo $value->name."<br>";
echo $value->age."<br>";
}
?>
</pre>

displaying data from database using select options codeigniter PHP

my controller function
public function add_books() {
$this->load->model('User');
$data['books'] = $this->User->getAuthor();
$this->load->view('vadd_books',$data);
}
my user_model model
function getAuthor() {
$this->db->select('author_firstname, author_lastname');
$this->db->from('authors');
$query = $this->db->get();
return $query->result();
}
Change in controller Modelname
public function add_books() {
$this->load->model('user_model');
$data['books'] = $this->user_model->getAuthor();
$this->load->view('vadd_books',$data);
}
Use $this->db->last_query() to print query in model to debug MySQL Query
You can try with below code
<select class="form-control">
<?php
foreach($books as $row)
{
echo '<option value="'.$row->author_firstname.'">'.$row->author_firstname.'</option>';
}
?>
</select>
For more reference refer url : http://stackoverflow.com/questions/19922143/display-data-from-database-to-dropdown-codeigniter
Try this one code. i try to explain step by step.
Model
public function getAuthor() {
$this->db->select('author_firstname, author_lastname');
$this->db->from('authors');
$query = $this->db->get();
return $query->result();
}
Controller
public function add_books() {
$this->load->model('User');
$data['books'] = $this->User->getAuthor();
$this->load->view('vadd_books',$data);
}
View
<select class="form-control my-class">
<?php
foreach($books as $row)
{
echo '<option value="'.$row->author_firstname.'">'.$row->author_firstname.'</option>';
}
?>
</select>

displaying data in table using codeigniter

table is not displaying any value in table. When i did var_dump($results). It printed NULL. Since, i am new to codeigniter, i am not able to solve this problem.
I'm not getting, where i did mistake. Help Me.
View Page
<tbody>
<?php
if(!empty($results) ) {
foreach($results as $row) {
echo '<tr>';
echo '<td>'." ".'</td>';
echo '<td>'.$row->FileTitle.'</td>';
echo '<td>'.$row->UploadedFile.'</td>';
echo '<td>'.$row->CreationDate.'</td>';
echo '<td>'.$row->UpdateDate.'</td>';
echo '<td>'." ".'</td>';
echo '</tr>';
}
}?>
</tbody>
Controller
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function member_CAttachments()
{
$data['results'] = $this->news_model->member_MAttachments();
$this->load->view('member/templates/header');
$this->load->view('member/index',$data);
$this->load->view('member/templates/footer');
}
}
Model
class News_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function member_MAttachments()
{
$results = array();
$query = $this->db->get('MemberFileDetails');
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
}
Instead you can do it in fewer lines with inbuilt CI Table library,
supports both 2.2 and 3.x versions.
Use it like,
$this->load->library('table');
$this->table->set_heading('FileTitle', 'UploadedFile', 'CreationDate', 'UpdateDate');
$query = $this->db->query('SELECT * FROM MemberFileDetails');
echo $this->table->generate($query);
do you have records in your table ? if yes then please try to remove the
if($query->num_rows() > 0) condition in the model and try.
remove $results = array(); in your code

How to show retrieve data from database into text field in codeigniter

it's my model code:
<?php
class Books_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_restaurants()
{
$sql = "SELECT id, names FROM restaurants ";
$query = $this->db->query( $sql );
return $query->result();
}
}
controller code:
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$this->user_data['result']=$this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$this->user_data);
}
}
What code I written in view file that the data show in text field?
Try the following :
In the Controller
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data['results'] = $this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
}
}
In the View:
<?php
foreach ($results as $result)
{?>
<label>Restaurant Name : </label>
<input type="text" value="<?php echo $result->names;?>" />
<?php } ?>
in the controller
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data["result"]=$this->Books_model->get_restaurants();
//$this->load->helper(array('form','url')); not needed
$this->load->view('restaurants/booking',$data["result"]);
}
}
now in your view
<?php
// notice that CI strip the key "result" from the array $data to become a variable $result in the view
foreach ($result as $row)
{
echo $row->id."<br>";
echo $row->name."<br>";
echo "----";
}
?>
Note:
there is no member like this "$this->user_data['result']" in
codeigniter but there is "$this->session->user_data("data_name")" if
you want to store some data in the session, but then, no need to pass
it to the view as an argument you can call the session data from the
view directly

Categories