I am facing problem getting the JSON object array from the following code.
public function staffStatusFromSameDept($deptID)
{
$sameDeptEmployee=StaffsModel::where("DepartmentID","=",$deptID)->get();
$count=$sameDeptEmployee->count();
if ($count>0)
{
foreach ($sameDeptEmployee as $value)
{
$data=DB::table('status')
->join('staffs','status.StaffPin', '=', 'staffs.StaffPin')
->select('staffs.StaffName','staffs.DesignationName',
'status.staffStatus', 'status.currentLocation',
'staffs.EmailID','staffs.MobileNO','status.returnTime','staffs.Photo' )
->where('staffs.StaffPin','=',$value->StaffPin)
->get();
$response[]=$data;
}
}
else{
$response=["error" => "Invalid Department ID"];
}
header('Content-type: application/json');
echo json_encode($response);
}
Actual output:
[[{"StaffName":"Mamun hosen","DesignationName":"PO(MF)","staffStatus":"working","currentLocation":"rangpur","EmailID":"mamun#brac.net","MobileNO":"01716340278","returnTime":"04:30","Photo":"helal.jpg"}],[{"StaffName":"nahid
hasan","DesignationName":"PO(MF)","staffStatus":"working","currentLocation":"rangpur","EmailID":"nahid#brac.net","MobileNO":"01716340278","returnTime":"04:30","Photo":"helal.jpg"}]]
Expected output:
[{"StaffName":"Mamun hosen","DesignationName":"PO(MF)","staffStatus":"working","currentLocation":"rangpur","EmailID":"mamun#brac.net","MobileNO":"01716340278","returnTime":"04:30","Photo":"helal.jpg"},{"StaffName":"nahid
hasan","DesignationName":"PO(MF)","staffStatus":"working","currentLocation":"rangpur","EmailID":"nahid#brac.net","MobileNO":"01716340278","returnTime":"04:30","Photo":"helal.jpg"}]
The problem in my code is that each person details taking JSON object array. But I want one JSON object array and each of the person's details I will get from this JSON object array.
Because ->get() will return an array of records found.
So if your query always has 1 record, change ->get() to ->first(), otherwise use $response = array_merge($response, $data) instead of $response[] = $data
You can simply change the $response[]=$data; to $response=$data;
Related
I have following data array returned by Item_model. This array included some values of MySQL tables columns such as 'r_qty' and 'ap_qty'.
$this->data['issueData']=$this->Item_model->ItemRequestfromHDData($id);
Item_model
function ItemRequestfromHDData($id)
{
$this->db->select('store_update_stock.*,store_update_stock_details.*,tbl_user.username,store_item.*,
sum(qty) as avqty, sum(store_update_stock_details.r_qty) as r_qty,
sum(store_update_stock_details.ap_qty) as ap_qty');
$this->db->from('store_update_stock_details');
$this->db->join('store_update_stock', 'store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('tbl_user', 'store_update_stock.supplier=tbl_user.userId');
$this->db->join('store_item', 'store_update_stock_details.item=store_item.item_id', 'left');
$this->db->where(array('store_update_stock.update_stock_id' => $id, 'store_update_stock_details.status' => 1));
$this->db->group_by('store_item.item_id');
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
I want to assign these two columns / values to variables. I tried following assignments.
$r_qty = $data['r_qty'];
$ap_qty = $data['ap_qty'];
but didn't get the expected result. What may be going wrong ? Can anyone help ?
As per codeigniter documentation,
result()
This method returns the query result as an array of objects, or an
empty array on failure.
Typically you’ll use this in a foreach loop, like this:
$query = $this->db->query("YOUR Q enter code here QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
So, your code should be
foreach ($this->data['issueData'] as $data)
{
$r_qty = $data->r_qty;
$ap_qty = $data->ap_qty;
}
I want return the json data from my controller.
I have written belo code.
$this->loadModel('Users');
$query = $this->Users->find();
$users = $query->select(['id', 'name']);
echo json_encode($users);
This return data in below format:
[{"id":1,"news_type":1,"name":"hoge"},{"id":2,"news_type":1,"name":"hoge1"}]
but I want it in below format:
{"categories":[{"id":1,"news_type":1,"name":"hoge"},{"id":2,"news_type":1,"name":"hoge1"}]}
Found solution.
I have used this:
echo json_encode(array("users" => $users));
I have to post data as json object format in codeigniter. how can it make possible.
My model function looks like:
public function signAction()
{
$name=$this->input->post('name',TRUE);
$dob=$this->input->post('dob',TRUE);
$gender=$this->input->post('gender',TRUE);
$country=$this->input->post('country',TRUE);
$city=$this->input->post('city',TRUE);
$mobile=$this->input->post('mobile',TRUE);
$email=$this->input->post('email',TRUE);
$password=$this->input->post('password',TRUE);
$this->db->select("*");
$this->db->where('email',$email);
$this->db->or_where('mobile',$mobile);
$query = $this->db->get('dr_signup');
$value=$query->num_rows();
if($value==0)
{
$sql = "INSERT INTO dr_signup (name,dob,gender,country,city,mobile,email,password)
VALUES(".$this->db->escape($name).",".$this->db->escape($dob).",".$this->db->escape($gender).",".$this->db->escape($country).",
".$this->db->escape($city).",".$this->db->escape($mobile).",".$this->db->escape($email).",".$this->db->escape($password).")";
$value1=$this->db->query($sql);
$insert_id = $this->db->insert_id();
$details = array(
'status'=>'success',
'message'=>'registered sucessfully',
'id' => $insert_id ,
'name' => $name,
'dob'=>$dob,
'gender'=>$gender,
'country'=>$country,
'city'=>$city,
'mobile'=>$mobile,
'email'=>$email,
);
echo json_encode($details);
}
else
{
$detail = array(
'status'=>'unsuccess',
'message'=>'already registered',
);
echo json_encode($detail);
}
}
I have to post the data as json format. My json objects looks like:
{"name":"jon","dob":"1jan2015","gender":"male","country":"India","city":"Mumbai","mobile":"86064 70000","email":"sales#green.tech","password":"1234"}
I am new to this. How can it make possible. what all modification should I have to do in code. thanking in advance.
say for example you get the json in $this->input->post('json_string',true); you can use the code like this..
$jsonstring = $this->input->post('json_string');
$json_data = json_decode($jsonstring,true); //remove true if you need object or keep it for array
The $json_data will give you the key value of the posted json string like $json_data['name'] ...
You try just like this
return json_encode($details);
delete your code "echo json_encode($details)"
I think it's working
I am using laravel4 query builder . I have following query which returns json data .
$query=DB::connection('mysqlMagento')->
table('magento_catalog_category_flat_store_1')->
join('sohyper_region_activity', 'magento_catalog_category_flat_store_1.entity_id', '=', 'sohyper_region_activity.activity_id')->
select("magento_catalog_category_flat_store_1.entity_id" , "magento_catalog_category_flat_store_1.parent_id" , "magento_catalog_category_flat_store_1.name as label" ,"magento_catalog_category_flat_store_1.url_key as name")->
get();
The above query returns json format data , I want to convert this to arrat format . I know Toarray() is used in eloquent to convert this but here it is in query builder , please help me on this .
Thanks.
$query=DB::connection('mysqlMagento')->
table('magento_catalog_category_flat_store_1')->
join('sohyper_region_activity', 'magento_catalog_category_flat_store_1.entity_id', '=', 'sohyper_region_activity.activity_id')->
select("magento_catalog_category_flat_store_1.entity_id" , "magento_catalog_category_flat_store_1.parent_id" , "magento_catalog_category_flat_store_1.name as label" ,"magento_catalog_category_flat_store_1.url_key as name")->
get();
$array = json_decode($query);
Though i'm fairly sure that the query builder is not returning JSON.
The query builder returns an array of objects. For example the query:
$users = DB::table('users')->select('name')->get();
will return an array which you can handle like this
foreach($users as $user) {
echo $user->name;
}
EDIT
If you want to convert it to array you have to do it on your own.
Here is a quick way
$users = json_decode(json_encode($users), true);
Now you can handle it like this
foreach($users as $user) {
echo $user['name'];
}
How can I return a value of 'no data found' if my query returns no results???
This is my code:
function getTerms($letter) {
$this->db->select('term, definition');
$this->db->from('glossary');
$this->db->where(array('letter' => $letter));
$query = $this->db->get();
foreach ($query->result() as $row) {
$data[] = array(
'term' => $row->term,
'definition' => $row->definition
);
}
return $data;
}
It currently returns the $data variable even if the query returns no results which is giving me php errors. How can I check that there are results before returning the $data array.
Simply check that the query returns at least one row:
if ($query->num_rows() > 0) {
// query returned results
} else {
// query returned no results
}
Read more in the docs
It currently returns the $data variable even if the query returns no results which is giving me php errors.
It's a good habit to initialize the array that you intend to build:
$data = array();
// Loop through possible results, adding to $data
If there are no results you'll get an empty array returned, then check that in your controller.
This way, at least the variable is defined and you won't get notices.
What about to give initial empty array value for $data
Just put
$data = array();
before foreach
<?php
return is_array($data) ? $data : array();
}