Codeigniter organization chart PHP - php

Whats the best way to output a json like an organization chart? Seems my loop is not good. Can't really get whats the empUnd for every employees. How to make a good loop for this type of chart like get every empUnd over and over until its empty
Create endpoint that shows the organization structure.
/test/chart
Response Format:
{
"empNo: "123",
"name": "Jennifer Sy",
"jobDesc": "CEO",
"empUnd: [
{
"empNo": "456",
"name": "Mike Tolomia",
"jobDesc": "VP",
"empUnd": [{}]
}
]
}
Here's my controller:
public function chart() {
$employeeDetails = $this->api_model->fetch_employees();
foreach ($employeeDetails as $employeeDetail) {
$test = $this->api_model->checkEmployeeUnder($employeeDetail['employeeNumber']);
$result[] = [
'empNo' => $employeeDetail['employeeNumber'],
'name' => $employeeDetail['firstName'],
'empUnd' => $test
];
}
return $this->output->set_content_type('application/json')->set_output(json_encode($result));
}
My Model:
public function fetch_employees()
{
$qry = $this->db->get('employees');
$result = $qry->result_array();
return $result;
}
public function checkEmployeeUnder($reportsTo)
{
$this->db->select('empNo, CONCAT(firstName, '.' , " ", lastName) AS name');
$this->db->from('employees');
$this->db->where('reportsTo', $reportsTo);
$qry = $this->db->get();
$result = $qry->result_array();
return $result;
}

Related

php: combining variable values of the same type?

I am new to PHP coming over from Javascript. I have 2 PHP methods. I'd like to combine them into one endpoint call. Can I push them into the same Array so they can come back in one call with one payload?
public function incompleteOrders()
{
$orders = request()->user()->incompleteOrders();
$data = $orders->toArray();
return response($data, 200);
}
public function finishedOrders()
{
$orders = request()->user()->finishedOrders();
$data = $orders->toArray();
return response($data, 200);
}
You can set multiple keys on the resulting json object, for example:
public function orders()
{
$data = [
'incomplete' => request()->user()->incompleteOrders(),
'finished' => request()->user()-> finishedOrders(),
];
return response($data, 200);
}
The json will look something like this:
{
"finished": [
{
"id": 1
}
],
"incomplete": [
{
"id": 2
},
{
"id": 3
}
]
}
Hello you can fetch the two array and combine them in another array like what we have bellow
public function userOrders()
{
$finishedOrders = request()->user()->finishedOrders();
$incompleteOrders = request()->user()->incompleteOrders();
$data = ["finishedOrders" => $finishedOrders->toArray(), "incompleteOrders" => $incompleteOrders->toArray()];
return response($data, 200);
}
Or you can also use array_merge a php function to merge the two array in one

PHP using json_encode, must output the specific data

I'm currently stuck at this scenario, now the other developer wants to output the API structure as seen on attached image.
json_required_format
But I tried as far as I can but I only got these result:
"all_projects": {
"TEST TOWNHOMES": {
"unit_types": [
{
"unit": "TOWNHOUSE 44.00"
}
]
},
"TEST HOMES": {
"unit_types": [
{
"unit": "DUPLEX WITH OUT GARAGE 44.50"
}
]
},
"TEST HOMES II": {
"unit_types": [
{
"unit": "DUPLEX WITH OUT GARAGE 44.50"
}
]
},
"TEST VILLAGE": {
"unit_types": [
{
"unit": "TOWNHOUSE 44.00"
},
{
"unit": "DUPLEX WITHOUT GARAGE 52.30"
}
]
}
I am using MVC framework,
This is my model looks like:
public function all_south_projects()
{
$this->db->distinct();
return $this->db->select('Project as project_name')->from('lots')
->where('available','YES')
->get()->result();
}
public function get_unit_types($projName)
{
$this->db->distinct();
return $this->db->select('UnitType as unit')->from('lots')
->where('Project',$projName)
->where('Available','YES')
->get()->result();
}
And then my controller is:
$resp = $this->MyModel->all_south_projects();
$test_array = array();
foreach ($resp as $value) {
$units = $this->MyModel->get_unit_types($value->project_name);
$allunits = array("unit_types"=>$units);
$allunits = (object) $allunits;
$test_array[$value->project_name] = $allunits;
}
//var_dump($test_array);
$stat = 200;
$message = 'Successfully fetched.';
if(empty($test_array)){
$empty=json_decode('{}');
json_output2($stat,'all_projects',$message,$empty);
}else{
json_output2($stat,'all_projects',$message,$test_array);
}
json_output2 is on my helper to customize json format:
Here is my code:
function json_output2($statusHeader,$responseName,$message,$response)
{
$ci =& get_instance();
$ci->output->set_content_type('application/json');
$ci->output->set_status_header($statusHeader);
$ci->output->set_output(json_encode(array('status' =>
$statusHeader,'message' => $message,$responseName =>$response)));
}
NOTE: Scenario is:
The API must give all the projects having available units,
if the project is available, then it needs to get its corresponding available units to view. I know I can make another API call but this time, we need to improve the UX.
Can someone enlighten me to get through this? Thank you!
Change this part :
foreach ($resp as $value) {
$units = $this->MyModel->get_unit_types($value->project_name);
$allunits = array("unit_types"=>$units);
$allunits = (object) $allunits;
$test_array[$value->project_name] = $allunits;
}
To :
foreach ($resp as $value) {
$units = $this->MyModel->get_unit_types($value->project_name);
$test_array[] = [
"project_name" => $value->project_name,
"unit_types" => $units
];
}
You don't have to cast your associative array to object like you did there : $allunits = (object) $allunits; because an associative array will always be serialized as a JSON object (associative arrays do not exist in JSON).

Integrating mailchimp with CRM

Need to get campaign_id with the list_id that I've got. My goal is to get all the campaign data and then sort out using the list_id. I have been able to retrieve the campaign response body, but somehow failing to get the campaign list_id. Any help or a different approach would be appreciated. Sharing my code and mailchimp api related reference.
MailChimp api ref:
"campaigns": [
{
"id": "42694e9e57",
"type": "regular",
"create_time": "2015-09-15T14:40:36+00:00",
"archive_url": "http://",
"status": "save",
"emails_sent": 0,
"send_time": "",
"content_type": "template",
"recipients": {
"list_id": "57afe96172", // this is required
"segment_text": ""
},
My Progress:
public static function getCampaignID($list_id){
$MCcampaigninfo = self::$mc_api->get("/campaigns"); // gives a response consisting 3 rows, required value is in 1st row, which is an array
foreach ($MCcampaigninfo as $key => $value) {
if ($value[8]->'list_id' == $list_id) { //under the 'campaign'array, we need the 9th position property 'recipient'
$campaign_id = $value[12]->'id';
}
}
}
This code assumes the response of $mc_api->get is equal to the JSON you showed in your example
public static function getCampaignID($list_id) {
$campaigns = json_encode(self::$mc_api->get("/campaigns"), true);
$campaignIds = [];
foreach ($campaigns as $campaign) {
//if the list_id matches the current campaign recipients['list_id'] add to the array
if ($campaign['recipients']['list_id'] === $list_id) {
$campaignIds[] = $campaign['id'];
}
}
//return an array with campaignIds
return $campaignIds;
}
Got it working. The api structure seems different in reality from their documentation. Thanks for all the help. Posting my updated code.
public static function getCampaignID($list_id){
$MCcampaigninfo = self::$mc_api->get("/campaigns");
foreach ($MCcampaigninfo as $key => $campaign) {
if($key == campaigns){
foreach ($campaign as $key2 => $clist) {
foreach ($clist as $key3 => $recip) {
if($key3 == id){
$campaign_id = $recip;
}
elseif($key3 == recipients){
foreach($recip as $key4 => $listid){
if($key4 == list_id){
if($listid == $list_id){
return $campaign_id;
}
}
}
}
}
}
}
}
}

json style mongodb php query

I need to create mongodb query using json format like I do in studio 3T.
For example i exceute this "where" in studio 3t and it works fine
{ $and: [ { "message": { $not: /.*status=sent.*/i } }, { "message": { $not:
/.*status=bounce.*/i } }, { "message": { $not: /.*message-id.*/i } } ] }
But I don't know how to use it in php.
I can create queris this way:
public function connect(...)
{
...
self::$connect = new MongoDB\Driver\Manager('mongodb://'.$data);
}
public function query($table, $filter = array(), $options = array())
{
if(!self::$connect)
{
self::$connect = $this->connect();
}
$query = new \MongoDB\Driver\Query(
$filter, // query (empty: select all)
$options // options, example: ['limit' => 40 ]
);
return self::$connect->executeQuery($this->dbName.'.'.$table, $query);
}
$result = $mongo->query('messages', $filter, array('limit' => 1000));
How to make it using json-style ?

php | dynamic api call

I am trying to create a dynamic endpoint for a API I am creating in order to include some data but only if it is required so I can use it in multiple places.
The idea is to have api.domain.com/vehicle to bring back basic vehicle information but if I did api.domain.com/vehicle?with=owners,history then the idea is to have a function which maps the owners and history to a class which will return data but only if it is required.
This is what I currently have.
public static function vehicle()
{
$with = isset($_GET['with']) ? $_GET['with'] : null;
$properties = explode(',', $with);
$result = ['vehicle' => Vehicle::data($id)];
foreach ($properties as $property) {
array_push($result, static::getPropertyResponse($property));
}
echo json_encode($result);
}
Which will then call this function.
protected static function getPropertyResponse($property)
{
$propertyMap = [
'owners' => Vehicle::owner($id),
'history' => Vehicle::history($id)
];
if (array_key_exists($property, $propertyMap)) {
return $propertyMap[$property];
}
return null;
}
However, the response I'm getting is being nested within a index, which I don't want it to be. The format I want is...
{
"vehicle": {
"make": "vehicle make"
},
"owners": {
"name": "owner name"
},
"history": {
"year": "26/01/2018"
}
}
But the format I am getting is this...
{
"vehicle": {
"make": "vehicle make"
},
"0": {
"owners": {
"name": "owner name"
}
},
"1": {
"history": {
"year": "26/01/2018"
}
}
}
How would I do this so it doesn't return with the index?
Vehicle::history($id) seems to return ['history'=>['year' => '26/01/2018']], ...etc.
foreach ($properties as $property) {
$out = static::getPropertyResponse($property) ;
$result[$property] = $out[$property] ;
}
Or your methods should returns something like ['year' => '26/01/2018'] and use :
foreach ($properties as $property) {
$result[$property] = static::getPropertyResponse($property) ;
}

Categories