displaying data from database using select options codeigniter PHP - 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>

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

I want to get max id value from table in codeigniter

in model file i write this code:
public function maxcode()
{
$this->db->select (max(ClassID)+1 as ClassID)-> from ('class');
$query=$this->db->get();
if($query->num_rows>0)
{
$row = $query->row_array();
return $row;
}
echo $row['ClassID'];
}
in controller function i write.....this code
public function maxcode()
{
$this->insert->maxcode();
}
Now I understand what your problem is.
You need to transfer this value to the template. This is done in the page controller
Example
$data['ClassID'] = $this->insert->maxcode();
$this->view('tenplate Name', $data);
And used in template
<?php echo $ClassID; ?>

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

show retrieve data in drop down list in codeigniter

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.

REST app + codeigniter + database

I did a REST app + codeigniter + database following: https://github.com/chriskacerguis/codeigniter-restserver , works perfect, wonderful.
Right now my json file getting static data, how can i get data from database?
i wrote a file and i can get from database the data, but i have no idea how to get the data from database in json, can you guys help me? thank you.
That file return to me that static json, works perfect, I would like get data from database.
/controllers/hello.php
<?php
include (APPPATH.'/libraries/REST_Controller.php');
class Hello extends REST_Controller {
function world_get(){
$data = new stdClass();
$data->name = 'Mark ';
$this->response($data, 200);
}
}
These files below I can get data from database but in html, how can I get data in json format?
/controllers/site.php
<?php
Class Site extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function index(){
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home', $data);
}
}
/models/data_model.php
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data [] = $row;
}
return $data;
}
}
}
views/home.php
<htmL>
<body>
<pre>
<?php foreach ($rows as $r)
{
echo '<h1>' . $r->title . '</h1>';
}
?>
</pre>
<?php foreach ($rows as $r) : ?>
<h1> <?php echo $r->author; ?></h1>
<h1> <?php echo $r->contents; ?></h1>
<?php endforeach; ?>
</body>
</htmL>
You are making API.To return data you don't need to call view.You can do it simply this way
function index()
{
$this->load->model('data_model');
$data = $this->data_model->getAll();
$this->response($data, 200);
}
Your model function is OK but you can make it simple.
function getAll()
{
return $this->db->get('data')->result();
}
just use json_encode in your controller and little change in your model file
Controller
function index(){
$this->load->model('data_model');
$result= $this->data_model->getAll();// change this line
$data['rows']= json_encode($result);// add this line
$this->load->view('home', $data);
}
Model
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
return $q->result();
}else{
return false;
}
}
}

Categories