calculate and display result from a row in codeignitor 3 - php

I have a table where i need to subtract tow values in the same column and display the difference in another view table. I need to record daily total hour meter readings from 5 compressors and display the difference as daily running hours.
My table:
image output of crudtabe
SELECT a.cuidad_id,
a.bc100a,
Coalesce(a.bc100a - (SELECT b.bc100a
FROM cuidadrun b
WHERE a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS
diffbc100a
FROM cuidadrun a
what is working great in phpmyadmin, please see image:
sql output
I cant workout my controller, model, and view to display the result.
also, can i save the result in another table?
My Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cactushrs extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Cactushrs_model');
}
public function index()
{
$this->load->view('template/header');
$data = $this->Cactushrs_model->cactushrs();
$this->load->view('pages/cactushrs_view', $data);
$this->load->view('template/footer');
}
}
My Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cactushrs_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function cactushrs()
{
$query = $this->db->get ('SELECT a.cuidad_id, a.bc100a, COALESCE(a.bc100a - (SELECT b.bc100a FROM cuidadrun b WHERE a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS differnce FROM cuidadrun a');
}
}
My view
<table id="cuidadhrs" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>cuidad_id</th>
<th>bc100a</th>
<th>diff100</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $this->db->get('differnce'); ?></td>
</tr>
</tbody>
DB Fiddle https://www.db-fiddle.com/f/vSM6hA2Wa3cpRJAqEoDMKZ/0

I did solved my question myself. The problem was in the model, controller and view.
controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cuidadhrs extends CI_Controller {
public function index()
{
$this->load->view('template/header');
$this->load->model('Cuidadhrs_model');
$hours['hours'] = $this->Cuidadhrs_model->cuidadhrs();
$this->load->view('pages/cuidadhrs_view', $hours);
$this->load->view('template/footer');
}
}
model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cuidadhrs_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function cuidadhrs()
{
$sql = "SELECT a.cuidad_id, a.bc100a, COALESCE(a.bc100a - (SELECT b.bc100a FROM cuidadrun b WHERE a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS diffbc100a FROM cuidadrun a";
$query = $this->db->query($sql);
return $query->result_array();
}
}
view.php
<table id="cuidadhrs" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<th>ID</th>
<th>BC-100a Hourmeter</th>
<th>Daily Hrs.</th>
</thead>
<tbody>
<?php foreach($hours as $hrs){ ?>
<tr>
<td><?php echo $hrs['cuidad_id']; ?></td>
<td><?php echo $hrs['bc100a']; ?></td>
<td><?php echo $hrs['diffbc100a']; ?></td>
</tr>
</tbody>
<?php } ?>
<?php $this->output->enable_profiler(TRUE); ?>
</table>

Related

undefined method in model in codeigniter

I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function regi_users(){
$q = $this->db->query("SELECT username FROM public");
return $q;
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
account.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$this->load->model('loginmodel');
$qresult = $this->loginmodel->regi_user();
foreach ($qresult as $row) {
echo $row->username;
}
?>
</body>
but when my admin logs in, he's shown the following error,
Fatal error: Call to undefined method LoginModel::regi_user() in
E:\wamp64\www\ci\application\controllers\account.php on line 11
what am i doing wrong here? I apologize if it is a silly question but i am kind of new to php
Thank you for your suggestions
Controller
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('loginmodel');
}
public function FunctionName($value='')
{
$this->data["users"] = $this->loginmodel->regi_user();
$this->load->view('account',$this->data);
}
}
Model
class Loginmodel extends CI_Model{
function __construct() {
parent::__construct();
}
public function regi_user() {
$query = $this->db->get('table_name')->result();
return $query;
}
}
View
<table class="table">
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Your query should be like this
public function regi_users(){
return $this->db->select('username')->from('table_name')->get()->result_array();
}
controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('YourModelName');
}
public function fetchUser()
{
$data['user']=$this->YourModelName->fetchUsers();
$this->load->view('yourViewClass',$data);
}
}
Model
class YourModelName extends CI_Model
{
function __construct()
{
$this->load->database();
}
public function fetchUsers()
{
$query = $this->db->select('*')->from('table_name');
$query=$this->db->get();
if($query->num_rows()>0)
{
$result=$query->result();
return $result;
}else{
return 0;
}
}
}
view
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($user as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Model name should be LoginModel.php

How to view record from DB with CodeIgniter

I try to view my record from DB by Code Igniter following the tutorial on this site but it show the error message
what can I try to make it identified?
Belows is view "view_inlist.php" code
<table class="table table-striped table-bordered">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Number</strong></td>
<td><strong>Type</strong></td>
<td><strong>Unit</strong></td>
<td><strong>Date</strong></td>
</tr>
<?php foreach($LIST as $list)
{?>
<tr>
<td><?=$list->ID;?></td>
<td><?=$list->Name;?></td>
<td><?=$list->Letter_Number;?></td>
<td><?=$list->Letter_Type;?></td>
<td><?=$list->Unit;?></td>
<td><?=$list->Date;?></td>
</tr>
<?php }?>
</table>
Here is controller "Main.php"
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function check_inList() {
$this->load->model('login');
$query = $this->login->check_inlist();
$data['LIST'] = null;
if($query){
$data['LIST'] = $query;
}
$this->load->view('form/view_inlist', $data);
}
}
Here is Model "login.php"
function check_inlist(){
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,Date");
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
try this
in controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login');
}
public function check_inList() {
$data['all_list'] = $this->login->check_inlist();
$this->load->view('form/view_inlist', $data);
}
}
in model
<?php
class Login extends CI_Model {
function check_inlist(){
$this->db->select('*');
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
}
in view page
<?php foreach($all_list as $list) {?>
<tr>
<td><?php echo $list->ID;?></td>
<td><?php echo $list->Name;?></td>
<td><?php echo $list->Letter_Number;?></td>
<td><?php echo $list->Letter_Type;?></td>
<td><?php echo $list->Unit;?></td>
<td><?php echo $list->Date;?></td>
</tr>
<?php }?>
first check your database structure because in model you fetch EMAIL column
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");
And you print Date column <td><?=$list->Date;?></td> in view
1.Controller
Main.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('login');
$this->load->helper('url_helper');
}
public function check_inList() {
$query = $this->login->check_inlist();
$data['LIST'] = null;
if($query){
$data['LIST'] = $query;
}
$this->load->view('view_inlist', $data);
}
}
?>
2)Views
view_inlist.php
<table class="table table-striped table-bordered">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Number</strong></td>
<td><strong>Type</strong></td>
<td><strong>Unit</strong></td>
<td><strong>Date</strong></td>
</tr>
<?php foreach($LIST as $list)
{?>
<tr>
<td><?=$list->ID;?></td>
<td><?=$list->Name;?></td>
<td><?=$list->Letter_Number;?></td>
<td><?=$list->Letter_Type;?></td>
<td><?=$list->Unit;?></td>
<td><?=$list->EMAIL;?></td>
</tr>
<?php }?>
</table>
3.Model Login.php
<?php
class Login extends CI_Model {
function HomeModel(){
parent::Model();
}
function check_inlist(){
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
}
?>

Codeigniter DATABASE Trouble

The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.
<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>
This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));

passing the input name (comes from database) from view to controller

I'm trying to learn codeigniter, the first thing i did is to make simple add edit delete function...
I'm having trouble calling the name of input button to the controller to delete an entire row of table..
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delete_model extends CI_Model{
public function delete($data){
$this->db->delete('record_tbl', $data);
}
}
controller
class Delete_controller extends CI_Controller {
public function index()
{
$this->delete();
}
public function delete()
{
$data = array();
$this->load->model('fetch');
$query = $this->fetch->getData();
if ($query)
{
$data['results'] = $query;
}
$this->load->view('delete', $data);
}
public function delete_data(){
$this->load->model('delete_model');
$where = $this->input->get('id');
$data['id'] = $where;
$this->delete_model->delete($data);
redirect('');
}
}
view
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center"><input name="<?=$row->id?>" type="submit" value="delete"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
Your form will be doing a POST rather than a GET request. So rather than doing
$where = $this->input->get('id');
You want to do
$where = $this->input->post('id');
Also why don't you pass the value of $row->id to your controller so that you know which input field to target. So in your view do
<?php echo form_open('delete_controller/delete_data/' . $row->id) ?>
Then in your controller
public function delete_data($id){
$this->load->model('delete_model');
$where = $this->input->post($id);
//...
It's going to be $this->input->post ('id') like #Pattle says and you are using the "name" attribute of the form for the ID. What I usually do is use a hidden field (see form_hidden () helper in CI documentation) with the name "id" and the value you want ($row->id).
<?php echo form_hidden ('id', $row->id); ?>
Instead of $this->input->get('id'); use $this->input->post('id');.
And to simplify things up: I probably recommend using <button></button> as control buttons like delete functionality (it also works on edit/update FYI).
Coz you can just add attribute to a button value = $id and name=delete. And it would be like an <input type='submit'> just by adding an attribute of type='submit'
So
VIEW
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center">
<?php
echo "<button type='submit' name='delete' value='" . $row->id . "'>" ;
echo "DELETE" ;
echo "</button>" ;
?>
</form>
</td>
</tr>
<?php } ?>
</table>
Then it would be very simple to fetch the id of the row you want to delete. Just do this in your controller $id = $_POST['delete']; or $id = $this->input->post('delete');

how to display content from database in codeigniter without calling controller from view?

I am new to codeigniter. I followed a video tutorial and successfully created a login registration system. After registration or login the users reach a page called members.php.
members.php is a view.
my controller is main.php and model is model-users.php.
I want members.php to show/have content from the database so that the users can read it once the login or register?
I followed a few resources on the web that said not to call controller from view. How can I accomplice the above without doing so?
Thanks
I think the CodeIgniter documentation is actually very clear, and very easy to follow. Nevertheless, here's an example of how you might structure your app.
Controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('members_model');
}
function index() {
$data['members'] = $this->members_model->get_members();
$this->load->view('header', $data);
$this->load->view('members');
$this->load->view('footer');
}
}
Model:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members_model extends CI_Model {
function get_members() {
$query = $this->db->get('members');
return $query->result_array();
}
}
View:
<?php
print_r($members);
To access the above page, you would visit http://yoursite.com/index.php/members.
You can use a .htaccess file to remove the index.php segment from the URI. The index() method is automatically called when you run a controller.
I don't have any example of the code, but I write one simple example of HTML when you can show name and surname of members.
Controller:
function members(){
$members = $this->model-user->get_list(); //array of members
$this->load->view('members.php', $members); // load members in view
}
View members.php
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
<?php foreach($members as $m): ?>
<tr>
<tr><?php echo $m['name']; ?>
<tr><?php echo $m['surname']; ?>
</tr>
<?php endforeach; ?>
<table>
</body>
</html>
Model:
function get_list() {
// query in database
return $query = $this->db->get('members'); //table name members
}
This is the simple example in a short form. I hope that you will understand it.
I think i know what you mean, this is definitely not the right way to go about this but if you really must you could call a model from a view like this.
In the view
<div>
<?php
#You could load the model here or autoload it in config->autoload->models(i believe)
$data = $this->model_name->model_function();?>
foreach($data as $row):
echo '<pre>';
print_r($row);
echo '</pre>';
endforeach;
?>
</div>
here is the example of my codes:
views:
<?php foreach($ecms as $i){ ?>
<tr>
<td><?php echo $i->accnum; ?></td>
<td><?php echo $i->crdnum; ?></td>
<td><?php echo $i->fname; ?></td>
<td><?php echo $i->bday; ?></td>
</tr>
<?php } ?>
model:
function masterHide()
{
$sql =" select * from ecms";
$query = $this->db->query($sql);
return($query->num_rows() > 0) ? $query->result(): NULL;
}
controller:
function search() {
// $this->MasterListAccess();
$this->load->model('navi_model');
$query = $this->navi_model->masterHide();
//check results or returned value of model
//print_r($query);
$data['ecms'] = $query;
$data['main_content'] = 'search';
$this->load->view('includes/template',$data);
}
At first connect the database in your project file and connect the model page in your view page and try this code.
Go to the view page and try this code
View
20
21
<?php
$get_data=$this->MyModel->get_table_data();
?>
<table class="table table-bordered">
<thead>
<tr>
<th> Name </th>
<th> Email </th>
<th> Phone No </th>
</tr>
</thead>
<tbody>
<?php foreach($get_data as $val){ ?>
<tr>
<td><?php echo $val->name; ?></td>
<td><?php echo $val->email; ?></td>
<td><?php echo $val->phone; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Model
public function get_table_data(){ $qry=$this->db->select('*')->from('Table Name')->get()->result(); return $qry; }

Categories