Codeignighter page data not displaying - php

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

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 use two tables data in a view in Codeigniter

I have two tables, first table is about topic name and second table shows sub-topics. I want to show from first table topic name then search their corresponding sub-topic and show it in view and so on till topic names are found in first table. But the problem is i can only get one table data in view i.e. main topic table. I have no idea how to get full data of both table and use it.
Database Structure:
Model:
<?php
class Topics_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_topics()
{
$this->load->database();
$this->db->select("*");
$this->db->from("topics");
$query=$this->db->get();
return $query->result();
}
}
?>
Controller :
<?php
class drag_drop_course_material extends CI_Controller {
function drag_drop_course_material()
{
parent::__construct();
}
function index()
{
$this->load->model('topics_model');
$data['query']=$this->topics_model->get_all_topics();
$this->load->helper('url');
$this->load->view('drag_drop_course_material', $data);
}
}
?>
View:
<?php
foreach ($query as $row) {
?>
<li> $row->topic_name </li>
<? } ?>
Required Output:
Try this query. We do left join of both tables.
$CI->db->select('*');
$CI->db->from('topic');
$CI->db->join('sub-topics', 'topic.id = sub-topics.sub_topic_id', 'left');
$query = $CI->db->get();
$result = $query->result();
You will get result in your model. return this result from model and access it in controller. Once you print return result in controller you will get idea that how to render it because you already did it above.
Your controller is perfect, your model is perfect just change your view:
In view:
foreach ($query as $row)
{ ?>
<ul>
<li><?php echo $row['topic_name'];?>
<ul>
<?php $this->db->where('sub_topic_id',$row['id'])
$r = $this->db->get('subtopics');
if($r->num_rows()>0)
{
foreach ($r -> result_array() as $row) {
$data1[] = $row;
}
}
foreach($data1 as $sub){?>
//print <li><?php echo $sub['subtopic_name']?></li>
<? }?>
</ul>
</li>
<? }?>
</ul>

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.

CodeIgniter - Undefined Property Error

I am trying to access all the bands in my table and print them out in a list, but when I run it I get this error:
Severity: Notice
Message: Undefined property: CI_Loader::$model_bands
Filename: views/band_view.php
Line Number: 16
band_view.php:
<h3>List of Bands:</h3>
<?php
$bands = $this->model_bands->getAllBands();
echo $bands;
?>
model_bands.php:
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
Could someone pleease tell me why it is doing this?
Why do you need to do this, the correct way is to use the model methods inside a controller, then passing it to the view:
public function controller_name()
{
$data = array();
$this->load->model('Model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('view_name', $data); // load the gathered data into the view
}
And then use $bands (loop) in the view.
<h3>List of Bands:</h3>
<?php foreach($bands as $band): ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php endforeach; ?>
Did you load your model in the Controller?
$this->load->model("model_bands");
You need to change your code like
Controller
public function AllBrands()
{
$data = array();
$this->load->model('model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('band_view', $data); // load the gathered data into the view
}
Then View
<h3>List of Bands:</h3>
<?php foreach($bands as $band){ ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php } ?>
Your Model is ok
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
You forgot to load the model on your controller:
//controller
function __construct()
{
$this->load->model('model_bands'); // load the model
}
BTW, why are you calling the model directly from your view? Should it be:
//model
$bands = $this->model_bands->getAllBands();
$this->load->view('band_view', array('bands' => $bands));
//view
<h3>List of Bands:</h3>
<?php echo $bands;?>

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