Codeigniter, how to pass COUNT result too my view - codeigniter

i have problem with passing data from model to my view. In Database i have Project with a few tasks i need to COUNT it. My problem is: How to pass the results to the view with foreach.
(echo $this->db->count_all_results(); in MODEL show good results)
Project 1: 5 tasks
Project 2: 10 tasks
Project 3: 1 task
MODEL:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
CONTROLLER
function WszystkieProjekty(){
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty();
$arrayid = array();
foreach( $data['moje'] as $row ){
$row->id;
$aaa = $row->id;
$arrayid[] = $this->Todo_model->wyswietl($aaa);
}
$data['tabid'] = $arrayid;
$this->load->view('Projekty.html', $data);
}
MODEL:
function wyswietlWszystkieProjekty(){
$query = $this->db->query("SELECT * FROM todo");
return $query->result();
}

You're passing back to your controller a result set, so all you need to do is iterate over the result set, generating actual rows. It's not too hard, and you've got the right idea.
You have:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
As your model function that is supposed to return the count, right? Easiest way to do this is right from the model to keep your controller from having to do the legwork. When you run the count function in mysql, you'll only get back one row in your case, so using num_rows() isn't going to help because this query will always generate 1 row.
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT(id_zadania) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
$rs = $query->result();
return $rs[0]['COUNT(id_zaedania)'];
}

In your query, you have counted the data, so i think you can get the total data in the model from $query->rows()->zlics. And in the controller, just pass it to the view like usual
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty(); $this->load->view('Projekty.html', $data);

Related

How to get data from 2 related tables in the same controller in codeigniter?

I'm trying to get data from 2 DB tables that are related. Departments and Functions: Each Department has multiple Functions.
I want to show all data in an accordion on the same page.
Until now, the code works like this:
Declare the variable departments in the controller, and in view, in the foreach loop, I have called the Functions model in a variable.
Controller:
public function index(){
//Data variable declarations
$data['page_title'] = "Departments and Functions";
$data['page_subtitle'] = "choose an occupation for the list of users assigned";
//Data variable declaration - from models
$data['departments'] = $this->Department->get_all('departments');
$this->load->view('layouts/header');
$this->load->view('layouts/title', $data);
$this->load->view('layouts/aside');
$this->load->view('departments/index', $data);
$this->load->view('layouts/footer');
}
Here I want to add the Function->get_by_department_id() method. How to get the ID dynamically?
The occupation model methond:
public function get_by_fk($table = 'occupations', $fk = "department_id" , $fk_value=$department->department_id){
$this->db->select('*');
$this->db->from($table);
$this->db->where($fk, $fk_value);
$query = $this->db->get();
return $result = $query->result();
}
Try using a left join
function get_departments_and_functions() {
$sql = 'SELECT `deptartments`.*, `functions`.*
FROM `departments` LEFT JOIN `functions`
ON `functions`.`deptartments_id` = `departments`.`id`';
return $this->db->query($sql)->result();
}
You could also query the departments and then foreach over the departments and query to get the functions. Then you would manually build the result set.

how to add information from difrent table?

i am new in CI i have a data the result from a process and i want to additional information on that from difretnt table like this picture.
both table are linked to an id (idhn). i make query join in model like this
function gabung(){
$this->db->select('*');
$this->db->from('metode');
$this->db->join('hasil_inden','hasil_inden.idhn=metode.idhn');
$query = $this->db->get();
return $query->result();
}
i called $metode in controller like this
' $metode = $this->Proses_model->gabung();'
but it is not work undefined variablev $metode please give me solution to fix it

how to use activerecord count_all_result()? with specific column?

I want to get count of specific column of table using codeigniter active record, something like this:
$this->db->count_all_results('t.column1');
so how can i change this db_active_record function? for any help thanks.
To count a specific column, You would do this;
$query = $this->db->get_where('table', array('column' => $column);
return $query->num_rows(); // This is what you're after...
create the query in your model, load it in your controller and then just count the array items in the view.
for example:
Model:
function getAll() {
$this->db->select('*');
$this->db->order_by('user_ad', 'asc');
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result();
}
}
controller:
$this->load->model('Users');
$data['allusers'] = $this->Users->getAll();
this->load->view('users', $data);
and in your view you can use
count($allusers);

Get first and last row from result in codeigniter

public function get_task_threads_first_last($task_id)
{
$this->db->select("t.nt_id, t.nt_responsed_by, t.nt_detail 'task_detail', t.nt_response_date, u.ui_full_name 'user_name', u.ui_designation 'user_designation', u.ui_phone 'extension', nt_response_date 'response_date'");
$this->db->from('tm_task_detail t');
$this->db->order_by("nt_response_date");
$this->db->join('ac_user_info u', 'u.ui_id = t.nt_responsed_by');
$this->db->where('t.nt_task_id', $task_id);
$query = $this->db->get();
return $result = $query->result();
}
I need to fetch the first and last row from above query. How is this possible ?
Also i am thinking that it is useless to select all rows than get the first and last rows out from it. I'm new in programming need advice thanks.
Thanks

CI pagination syntax for getting results

Assume that the config is done and it is intialized, then time for the results to be paginated.
On this line of code.
$data['records'] = $this->db->get('tablename', $config['per_page'], $this->uri->segment(3));
Is there anyway of changing the above line of code. Since I have 3 tables with ERD relationship. So in my 3rd table consists lots of rows that are connected to the other 2 tables. I want to make a model function where i will manually query the results. How am i supposed to write it?
$data['records'] = $this->load->model('manual_querying'), $config['per_page'], $this->uri->segment(3);
Im not sure if its correct, i am still unable to test it if its correct since im not done with my pagination yet.
In model, write a function
public function get_records($per_page,$page)
{
$this->db->select("*");
$this->db->from("table1 as t1");
$this->db->join("table2 as t2","t1.id = t2.t1_id");
$this->db->join("table3 as t3","t1.id = t3.t1_id");
$this->db->limit($per_page,$page);
$res = $this->db->get();
return $res;
}
In controller,
$this->load->model('your_model');
$data['records'] = $this->your_model->get_records($config['per_page'], $this->uri->segment(3));

Resources