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
Related
public function subchapt()
{
$result = $this->recursive_subchapter_id($level_title);
}
public function recursive_subchapter_id($level_title)
{
$level_data = $this->db->query("SELECT * FROM presto_project_level WHERE level_title ='". $level_title."'")->result_array();
if(count($level_data)>0){
$parentid1 = '';
$leveltitle1 = '';
$parentid1 = trim($level_data[0]['parent_id']);
$leveltitle1 = trim($level_data[0]['level_title']);
if($parentid1 == '0') {
return $leveltitle1;die;
}
else
{
$res = $this->recursive_subchapter_id($parentid1);
return $res;die;
}
}
else{
return $level_title;die;
}
}
every time getting result array is null. i tried directly in url so it was give result array and its count. but while call from other function it was give count of zero.
instead of checking the parentid in the recursive_subchapter_id(), check in subchapt() and then call again recursive_subchapter_id() if it not satisfy the condition.
I am trying to fetch a data for each chkey with a related value as a group in codeigniter. When there are a 2 or more chkey then code is work properly.
But when there is a only 1 chkey then it shows value of that chkey but it is shows extra chkey:null,value:null in json.
Related json for chkey is 1 is as follows'
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"},{"chkey":"null","list":"null"}]}]
Expected is,
[{"unit_id":"8","CHGRAPHUpdatetime":{"time":"2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00,2018-03-15 00:00:00"},"channelGraph":[{"chkey":"ch1","list":"2,-30,12,20"}]}]
Could you please help me to resolve this issue.
model code-
public function chGraph($unitid){
$this->db->select("unit_id");
$this->db->from("device_unit");
$this->db->where('unit_id', $unitid);
$query = $this->db->get();
$unit = $query->result_array();
$j = 0;
foreach($unit as $row) {
$unit[$j]['CHGRAPHUpdatetime'] = $this->UnitCHGRAPHUpdatetime($row['unit_id']);
$unit[$j++]['channelGraph'] = $this->UnitCHGRAPHDetails1($row['unit_id']);
}
return $unit;
}
public function UnitCHGRAPHDetails1($unit_id)
{
$this->db->select('distinct(chkey)','chvalue');
$this->db->from('channel_info');
$query = $this->db->get();
$channelgraphData = $query->result_array();
$m = 0;
foreach($channelgraphData as $row) {
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .'';
$response = $this->db->query($chvalueQuery)->row();
$channelgraphData[$m++] = $response;
}
return $channelgraphData;
}
controller code-
public function chGraph()
{
$unitid = $this->uri->segment('3');
$GraphDetails = $this->device_model->chGraph($unitid);
echo json_encode($GraphDetails);
}
You could add a GROUP BY at the end of the statement to eliminate the null results :
$chvalueQuery = 'SELECT chkey, GROUP_CONCAT(chvalue)as list FROM channel_info where chkey= "'. $row['chkey'] .'" and unit_id='. $unit_id .' GROUP BY chkey';
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;?>
I have a functiona within a function. Function B gets a value from an API and passes it to function A. However, when I echo the value within Function B it works but it is null when called in function A.
Should I be storing the value in a session variable or DB between loops?
function getFBLikes($postid) {
//Get total number of likes per post
$query = '/likes?summary=1&filter=stream';
$request_likes = BASE_URL
.$postid
.$query
.ACCESS_TOKEN;
$result_likes = json_decode(file_get_contents($request_likes), true);
foreach ($result_likes as $a => $b) {
if(isset($b['total_count'])) {
$likes = $b['total_count'];
echo $likes /* THIS APPEARS AS A CORRECT VALUE */
}
}
}
function getPostDetails($array){
foreach ($array as $a => $b) {
if(isset($b['type'])) {
if(isset($b['object_id'])){
$postid = $b['object_id'];
$shares = $b['shares']['count'];
$type = $b['type'];
getFBLikes($postid);
echo $likes; /* THIS IS NULL */
}
}
}
}
Add
return $likes;
to getFBLikes and
use it like this in getPostDetails
$likes = getFBLikes($postid);
function getFBLikes($postid) {
//Get total number of likes per post
$query = '/likes?summary=1&filter=stream';
$request_likes = BASE_URL
.$postid
.$query
.ACCESS_TOKEN;
$result_likes = json_decode(file_get_contents($request_likes), true);
foreach ($result_likes as $a => $b) {
if(isset($b['total_count'])){
$likes = $b['total_count'];
//ADDED
return $likes;
}
}
}
function getPostDetails($array){
foreach ($array as $a => $b) {
if(isset($b['type'])) {
if(isset($b['object_id'])){
$postid = $b['object_id'];
$shares = $b['shares']['count'];
$type = $b['type'];
//CHANGED
$likes = getFBLikes($postid);
echo $likes; /* THIS SHOULD NO LONGER BE NULL */
}
}
}
}
Should do the trick.
The problem you have here is getPostDetails is taling to getFBLikes,
but getFBLikes is not 'responsing' (returning) anything to you're getPostDetails so when you tried echoing the number of likes it was going to be null because essentially it wasnt being told how many likes there were
You need to have getFBLikes() return a value.
From the docs:
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call.
Sample solution:
in getFBLikes() add:
echo $likes /* THIS APPEARS AS A CORRECT VALUE */
return $likes
in getPostDetails() you can access the value with:
$likes = getFBLikes($postid); /* getFBLikes() will be return a value in $likes
I want to take a value from a query in one function and use it for multiplication within another function.
Here i have a function producing the number 3:
function insert() {
$siteid = 1;
$field = 3;
$time = "2011-10-11 15:04:56";
$this->db->select('Offset_value', 1)
->from('offset')
->where('siteid', $siteid)
->where('Offset_field', $field)
->where('time <', $time);
$query = $this->db->get()->result_array();
foreach ($query as $row) {
echo $row['Offset_value'];
}
return $query;
}
At the moment this is just set to display the number 3 but i need that value to be stored in a variable of some sort and load it into this function:
function get_sap_estimate() {
$kwp = 5;
$direction = 34;
$tilt = 60;
$month = 4;
$sap_shadingid = 2;
$this->db->select('derating_factor')
->from('sap_shading')
->where('id', $sap_shadingid);
$query = $this->db->get()->result_array();
$query = $query[0];
$dr = $query['derating_factor'];
$this->db->select("kwh * $dr * $kwp AS kwhdata")
->from('expected_kwh')
->where('direction', $direction)
->where('tilt', $tilt)
->where('month', $month);
$query2 = $this->db->get()->result_array();
return $query2;
}
Can this be done and how??
Thanks
function A(){
return "value";
}
functionB(){
var value = A(); // here is your array
var value_you_need = value[0]['Offset_value'];
}
If they are both in the same class (read: model file) write
var $_var;
before any methods are made. Then inside one of your methods type
$this->_var = $stuff;
to give this var a value. Then to use that value type
$new_stuff = $this->_var;
If they're not in the same class, you could try calling the first method inside of the second.