how to check out of function in codeigniter - codeigniter

i am beginner in codeigniter and want to check the out put of a function in codeigniter so that i can debug it but i am unable to do so for example here is my function in my managementmodel and i have put the slashes before the print_r function. when i want to see the result i remove these but i do not know where to see the result if in view then how please help me. thanks
function department()
{
$sql = "SELECT * FROM department ORDER BY id
ASC";
$query = $this->db->query($sql);
// echo "<pre>";
// print_r($query->result_array());exit;
return $query->result();
}
function get_test_list_info()
{
$sql = "SELECT * FROM test_list ORDER BY id
DESC";
$query = $this->db->query($sql);
//print_r($query->result_array());exit;
return $query->result();
}

Related

Codeigniter get database value in controller

This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}

Codeigniter active query error

Here is my query
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientId'=>$user['UserID'],'year'=>$year));
On checking in browser its generating this query below
SELECT *, *, `amount` AS SavingAmount FROM (`cashman_trial_balance`) WHERE `category_id` = '176' AND `clientId` = '122' AND `year` = '2015/2016'
I dont know from where the two stars coming from?
Controller code
$som_var = $this->client->statistics();
Model code
function statistics()
{
/* Some other queries */
$b = $this->get_balances();
}
function get_balances()
{
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientI‌​d'=>$user['UserID'],'year'=>$year));
return $query->result();
}
use this
$query = $this->db->query("SELECT amount AS SavingAmount FROM table_name WHERE category_id = '176'
AND clientId = '$user['UserID']' AND 'year'= '$year') ");
$result = $query->result_array();
return $result;
You can use the simple way
$query = $this->db->query("SELECT amount AS SavingAmount FROM cashman_trial_balance WHERE category_id = '176' AND clientId=$user['UserID'] AND year=$year");

Codeigniter, how to pass COUNT result too my view

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

codeigniter LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}

get the last row in the db

What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?

Resources