I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. Once I fix this, things will be much smoother.
He is the model function:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1($numstudent)
{
$query = $this->db->query("SELECT * FROM Student WHERE Student.JRNOption ='1'");
$numstudent = $query->num_rows()/25;
if($query->num_rows() > 0){
return $numstudent;
}
return null;
}
?>
Here is the controller.
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1($numstudent);
}
public function view($page = 'Statsview')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
//$data['title'] = ucfirst($page);
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
?>
Here is the view
<html>
<h1> Various Statistics for the Journalism department</h1>
<?php
echo "Percent of students that are undecided:";
echo $Test; ?>
</html>
What am I doing wrong? I been trying to get it right for over an hour and I just want to output the single result from the database query. Any help you can provide would be great.. please be respectful. Thanks!
Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.
Update Model:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1()
{
$query = $this->db->query("SELECT (SELECT COUNT(s2.student_id)
FROM Student s2
WHERE s2.JRNOption='1')*100/COUNT(s1.student_id) AS percentage
FROM Student s1");
$result = $query->row_array();
return $result['percentage'];
}
?>
Updated controller:
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1();
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
}
?>
Hope this will help you.
Related
could ya'll help me on this one please. I'm trying to count how many are there in my table as it doesn't show up the result on my view.
Controller
public function enrollment_summary()
{
$data['title'] = 'Enrollment Summary';
$this->load->model('report_model');
$data['query'] = $this->report_model->get_students();
$this->load->view('report_enrollment_summary', $data);
}
Model
<?php
class report_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_students()
{
$query = $this->db->count_all('students');
}
}
View
<body>
<h1>Enrollment Summary</h1>
<?php echo $query; ?>
</body>
Your get_students() is not returning anything?
So you need to add in a return.
Your current method...
public function get_students()
{
$query = $this->db->count_all('students');
}
becomes
public function get_students()
{
$query = $this->db->count_all('students');
return $query;
}
OR
public function get_students()
{
return $this->db->count_all('students');
}
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}
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