my total counts results looks like this now i want to echo it to get the display but its not getting
this is my controller
$data['present']= $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);
$data['presentss']=explode(',',$data['present']);
this is my view
<?php echo $presentss;?>
this is my model
public function present_report_by_empid($user_id = null,$date = null)
{
$temp = explode("-",$date);
$query='tbl_attendance.date_in';
$this->db->where('tbl_attendance.attendance_status', 1);
$this->db->where('tbl_attendance.user_id', $user_id);
$this->db->where("YEAR(tbl_attendance.date_in)",$temp[0]);
$this->db->where("MONTH(tbl_attendance.date_in)",$temp[1]);
$count=$this->db->count_all_results('tbl_attendance');
return $count;
}
when i change my code like this
$data['present'][]= $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);
and view as
<?php foreach($present as $pe =>$p){?>
<?php echo $p;?>
<?php }?>
i got like this
but i want it like this
please help to me to solve
You don't need this:
$data['presentss']=explode(',',$data['present']);
Your number of rows is already in
<?php echo $presents;?>
You can try to use to check your query:
echo $this->db->last_query();
Also switch to check the returned rows as well as the count:
$this->db->num_rows();
Try like this.Use $this->db->num_rows()to count total number of rows.
Model
public function present_report_by_empid($user_id = null,$date = null)
{
$temp = explode("-",$date);
$query='tbl_attendance.date_in';
$this->db->where('tbl_attendance.attendance_status', 1);
$this->db->where('tbl_attendance.user_id', $user_id);
$this->db->where("YEAR(tbl_attendance.date_in)",$temp[0]);
$this->db->where("MONTH(tbl_attendance.date_in)",$temp[1]);
$result = $this->db->get('tbl_attendance')->result_array();
$count=count($result); //counts number of rows
return $count;
}
Controller:
$data['present']= $this->attendance_model->present_report_by_empid($v_employee->user_id,$date);
And
In View
<?php echo $present;?>
Related
I have a question
Let say we have this 2 tables in our database
first table : category_lists
second table: data_category
so if list_data_id is equal to 1 meaning that it has 2 data if you look at data_category table which is 6 and 7
Now what I want is to query from category_list table so that 6 and 7 will echo Car Wreckers and Cash For Cars.
This is what my code looks like:
below is my controller:
public function index($listing_name)
{
$this->load->view('layouts/head_layout_seo');
$this->load->view('layouts/head_layout');
$data['main_view'] = 'listings/main_view';
$data['listing_data'] = $this->Listing_model->get_detail_listing($listing_name);
$list_id = $data['listing_data']->list_data_id; // this is what I get list_data_id is equal to 1
$data['category_ads'] = $this->Ads_model->get_ads($list_id); // this is what you need to look
$this->load->view('layouts/main', $data);
$this->load->view('layouts/footer_layout');
}
below is my model:
public function get_ads($list_id)
{
$this->db->where('list_data_id', $list_id);
$query = $this->db->get('data_category');
$query = $query->result();
//return $query;
if (count($query) > 0) {
for ($i=0; $i < count($query); $i++) {
foreach ($query as $value) {
$this->db->where('category_lists_id', $value->category_lists_id);
$querys = $this->db->get('category_lists');
//print_r($query);
return $querys->result();
}
}
}
}
below is my view:
foreach ($category_ads as $value) {
<p><?php echo $value->categories; ?></p> }
with the code above I get only 1 data which is Car Wreckers as you can see from the table, it supposes to show 2 data Car Wreckers and Cash For Cars
Can anyone help me with this?
Thank You
Try this, in your model so no need to call DB two times and no need to loop
public function get_ads($list_id)
{
$this->db->select('cl.category_lists_id,cl.categories');
$this->db->from('category_lists cl');
$this->db->join('data_category dc','cl.category_lists_id = dc.category_lists_id');
$this->db->where('dc.list_data_id',$list_id);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return array();
}
}
I have two tables with records. In the first table, I am displaying personal information and in the second table, I am adding the activities details.
Now I am trying to display the record using joins. So below code, I am using in the model
public function getMemberActivity($gotMemberId){
$getDetails = array('members.member_id'=>$gotMemberId,'member_activity.activity_status'=>1,'members.is_Approved'=>1);
$result = $this->db->where($getDetails)
->from('members')
->join('member_activity', 'members.member_id = member_activity.member_id','LEFT')
->get()
->result();
//echo $this->db->last_query();
//print_r($result);
if($result)
{
return $result;
}
else
{
return 0;
}
}
Controller
Note: I am getting multiple member id here because I have above some more logic. that's the reason I am using for each.
$ActivityData=[];
foreach ($data['getAllMember'] as $key => $m_id) {
$ActivityData[] = $this->Access_model->getMemberActivity($m_id->member_id);
}
$data['MemberActivity'] = $ActivityData;
Now If I found the records related the member id in the secondary table then I am getting the output but if not found a record in the second table then I am getting the error Message: Invalid argument supplied for foreach()
If I remove 'member_activity.activity_status'=>1 from the where clause then my join query is working. I mean I am getting the member records.
->join('member_activity', 'members.member_id = member_activity.member_id','LEFT')
View
$SActivity=$MemberActivity;
//print_r($SActivity);
if($SActivity){
foreach ($SActivity as $sec_1) {
foreach ($sec_1 as $sec_activities) {
//list here
}
}
}
else{echo"no data availalbe";}
So my expected output is, I have to display the records if found in the second table if not found then also display the member table records.
Would you help me out in this?
Move your where condition 'member_activity.activity_status'=>1 in JOIN as below,
$public function getMemberActivity($gotMemberId){
$getDetails = array('members.member_id'=>$gotMemberId,'members.is_Approved'=>1);
$result = $this->db->where($getDetails)
->from('members')
->join('member_activity', 'members.member_id = member_activity.member_id AND member_activity.activity_status = 1','LEFT')
->get()
->result();
//echo $this->db->last_query();
//print_r($result);
if($result)
{
return $result;
}
else
{
return 0;
}
}
Hope this will be help you.
Final query like this:
SELECT members.*,member_activity.* FROM members LEFT JOIN member_activity ON members.member_id = member_activity.member_id AND member_activity.activity_status = 1
WHERE 'members.member_id' = $gotMemberId AND 'members.is_Approved' = 1;
The problem is with your model function getMemberActivity. If there is no record then you returns 0 and when you foreach you will get error because its not an array. So you can simply return $result since codeigniter return default array.
public function getMemberActivity($gotMemberId)
{
$getDetails = array('members.member_id' => $gotMemberId, 'members.is_Approved' => 1);
$result = $this->db->where($getDetails)
->from('members')
->join('member_activity', 'members.member_id = member_activity.member_id AND member_activity.activity_status = 1', 'LEFT')
->get()
->result();
return $result;
}
Second option is you can check !empty before your foreach.
I hope this will help you, try
foreach ($SActivity as $sec_1) {
if (!empty($sec_1)):
foreach ($sec_1 as $sec_activities) {
//list here
print_r($sec_activities);
}
endif;
}
So I have this function that gets the total number of rows in a table.
This is my Code:
public function get_reservation()
{
$this->db->select('COUNT(*) as res_no ');
$this->db->from('reservation_details');
$query = $this->db->get()->result();
echo json_encode($query);
}
And it returns this JSON data
[{"res_no":"2"}]
What I want is to increment its value to 1 and return like this
[{"res_no":"3"}]
So far I have tried something like this
This is the my Code:
public function get_reservation()
{
$query = $this->db->select('COUNT(*) as res_no ')->from('reservation_details')->get();
$price = $query->row()->res_no;
$price = $price + 1;
echo json_encode($price);
}
But it only return 3
Hope this will help you :
Use CI query helper count_all to count all record of a table
public function get_reservation()
{
$count = $this->db->count_all('reservation_details');
$price = $count + 1;
$data['res_no'] = $price;
echo json_encode($data);
}
For more : https://www.codeigniter.com/userguide3/database/helpers.html#information-about-your-database
I have this code:
class SampleClass extends MainClass {
private $totalOrder;
//construct goes here
function get_total_number_of_order($buyerIndex) {
$totalSum = //sql to get sum of orders
return $totalSum;
}
function fetch_buyer() {
$qryBuyerInfo = //sql to get buyer info;
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder .'<br>';
}
}
I'm wondering why I only get 1 record wherein fact my table has at least 20 records. But if I comment out $this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);I will be able to generate all 20 records. Another thing I tried is to put get_total_number_of_order to other class an every loop, I initiate new class to use get_total_number_of_order function.
Anybody can have any better idea?
I think I found the issue in your code.
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder .'<br>';
}
with above code, you are only getting 1 record as you are re-writing the data into same object. You need to add this loop data into an array. Something like below:
$i=0;
while($rs = $qryBuyerInfo->fetch()) {
$this->totalOrder[$i] = $this->get_total_number_of_order($rs['buyer_index']);
echo $buyerInfo .'<br>';
echo $this->totalOrder[$i] .'<br>';
$i++;
}
I have a web application built using codeigniter. What I'm trying to do is show a summary of results on my view. In my controller I have the below code
function index() {
$data['summery'] = $this->Main_model->get_summery();
foreach ($data['summery'] as $d) {
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
}
}
The problem that im having is each summery record has total weekly count and a monthly count based on the i_id
in my view i loop through the summery and it display the summery but when i echo $total_week or echo $total_month its always returns NULL
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
When i do a var_dump on the above variables it shows the number, but when i try to echo it on the view it returns NULL
I hope I under stood correct Try something like this
function index() {
$this->load->model('main_model');
$data['summery'] = array();
$summery = $this->main_model->get_summery();
foreach ($summery as $d) {
$data['summery'][] = array(
'total_week' => $this->main_model->get_total_week($d['i_id']),
'total_month' => $this->main_model->get_total_month($d['i_id'])
);
}
$this->load->view('the_view', $data);
}
View
<?php foreach ($summery as $something) {?>
<?php echo $something['total_week'];?>
<?php echo $something['total_month'];?>
<?php }?>