Display data from database with Select Option Codeigniter - php

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

Related

Codeignighter page data not displaying

I trying to get a very simple query results set to display on a page using Codeignighter.
For my controller i have this:
class Homepage extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('string');
$this->load->helper('text');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('Job_sectors_model');
}
public function index(){
$page_data['sectors'] = $this->Job_sectors_model->get_sectors();
$this->load->view('header');
$this->load->view('homepage', $page_data);
$this->load->view('footer');
}
}
and for my model file i have this:
class Job_sectors_model extends CI_Model {
function get_sectors() {
return $this->db->get('sectors');
}
}
On my view file i have a simple select:
<select class="form-control" name="job_sectors">
<option>Choose job sector...</option>
<?php foreach ($sectors->result() as $row) : ?>
<option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
<?php endforeach ; ?>
</select>
I have this setting in my autoload.php file:
$autoload['libraries'] = array('database', 'session');
The page loads and the select has the correct number of items from the db table. What I am getting is an error in each of the seelcts option when you open the select. I see thiswhen I inspect it:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$nc_id
Filename: views/homepage.php
Line Number: 68
So the nc_id is a table column name, its just not returning the value. How do i define the property to stop this error from happening?
Many thanks
M
Ok i now have it working but it doesnt feel right:
so my model is this:
class Job_sectors_model extends CI_Model {
function get_sectors() {
$query = $this->db->get('sectors');
return $query->result_array();
}
}
my view is this:
<select class="form-control" name="job_sector">
<?php foreach ($sectors as $sector): ?>
<option value="<?php echo $sector['NC_ID']; ?>"><?php echo $sector['TC_NAME']; ?></option>
<?php endforeach; ?>
</select>
and my controller is this:
class Homepage extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('string');
$this->load->helper('text');
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('Job_sectors_model');
}
public function index(){
$this->load->view('header');
$page_data['sectors'] = $this->Job_sectors_model->get_sectors();
$this->load->view('homepage', $page_data);
$this->load->view('footer');
}
When I say ity doesnt feel right is this the right way to code in codeigniter, feels like i'm hacking it together to make it work?
Thanks
2 possibilities. 1 the column name actually doesn't exist, 2 you don't have any rows.
This prevents the later from causing issues.
class Job_sectors_model extends CI_Model {
function get_sectors() {
$q = $this->db->get('sectors');
if ($q->num_rows > 0) {
return $q->result();
}
return false;
}
}
View:
<?php if ($sectors): ?>
<select class="form-control" name="job_sectors">
<option>Choose job sector...</option>
<?php foreach ($sectors as $row) : ?>
<option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
<?php endforeach ; ?>
</select>
<?php else: ?>
No sectors
<?php endif; ?>
you can simply pass the data from the model in $pagedata and pass it while loading the view it will generate the array like this
$this->load->view('homepage',['pagedata' => $pagedata] ) ;
on view you can use directly if single row $pagedata->nc_id
else you can simply use foreach loop like this
<?php foreach ($pagedata as $row) {?>
//your code inside and access db rows by $row->nc_id
<?php } ?>
//don't use colon after foreach loop its php
why are you sending db->get object to view instead result object/array ? is there any specific reason ???
instead the normal method should be like
class Job_sectors_model extends CI_Model {
function get_sectors() {
$result =array();
foreach($this->db->get('sectors')->result() as $row)
{
$result[$row->nc_id]=$row->tc_name ;
}
return $result;
}
}
and instead of :
<select class="form-control" name="job_sectors">
<option>Choose job sector...</option>
<?php foreach ($sectors->result() as $row) : ?>
<option value="<?php echo $row->nc_id ; ?>"><?php echo $row->tc_name ; ?></option>
<?php endforeach ; ?>
</select>
view file should be like :
<?php echo form_dropdown('job_sectors', $sectors, 'default_value',' class="form-control" ' ); ?>
after putting $this->load->helper('form'); in your controllers constructor

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>

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.

Why does this model return "non-object error", when it worked in the controller?

In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>

Display multiple rows in codeigniter

I have query where I fetch the content of about 5 rows each with different content, but i dont now to display them individually in my views.
When I pass the the query->result() from my model back to my controller as $query, and then load a view with the $query data, I don't know how to echo out the e.g. content field of row 1.
I only know how to do this: foreach($query as $row) { echo $row->content; }
But this doesn't allow me to select what row content data (out of the 5 retrieved) I wanna echo out.
Could someone please explain how this is done?
The CodeIgniter documentation includes a section titled Generating Query Results which has some examples of what you are looking for.
Here's an (obviously contrived) example:
model:
class Model extends CI_Model
{
public function get_data()
{
return $this->db->query('...');
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
$data['query_result'] = $this->model->get_data();
$this->load->view('index', $data);
}
}
view:
<?php foreach ($query_result->result() as $row): ?>
<?php echo $row->field_1; ?>
<?php echo $row->field_2; ?>
<?php // and so on... ?>
<?php endforeach; ?>
function a(){
$this->db->select('*');
$this->db->from('table');
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$this->db->where(array('batch_id'=>$batchid,'class_id'=>$classid));
$query=$this->db->get();
for($i=1;$i<=$query->num_rows();++$i){
$data[$i]['task'] = $query->row($i)->task;
$data[$i]['subject_id'] = $query->row($i)->subject_id;
$data[$i]['postdate'] = $query->row($i)->postdate;
$data[$i]['cdate'] = $query->row($i)->cdate;
}
}

Categories