I have two table maintenance_owner and maintenance_users
there are my table structure bellow
This is maintenance_users table
and
This is maintenance_owner table
now i can get all user with this code
$this->db->select('mu.*, mo.id_land_owner as mu_owner, mo.group_name, mo.id_mu');
$this->db->from('maintenance_users as mu');
$this->db->join('maintenance_owner as mo','mu.id_maintenance = mo.id_mu');
$this->db->where('mo.id_land_owner', $land_owner_id);
return $this->db->get()->result();
it returns
[0] => stdClass Object
(
[id_maintenance] => 170
[full_name] =>
[username] => abt2050+m3#gmail.com
[password] =>
[token] => 914c001251a1ab018e5eb51923e8f6cc
[mu_owner] => 152
[group_name] => Cleaner
[id_mu] => 170
)
[1] => stdClass Object
(
[id_maintenance] => 176
[full_name] =>
[username] => bii#fatafati.net
[password] =>
[token] => 579faa456520656fcc24be047ca6a3bf
[mu_owner] => 152
[group_name] =>
[id_mu] => 176
)
[2] => stdClass Object
(
[id_maintenance] => 175
[full_name] =>
[username] => iamnow78+m4#gmail.com
[password] =>
[token] => d2f6b180a6a7bb32ecd26ffe0297f8f5
[mu_owner] => 152
[group_name] =>
[id_mu] => 175
)
but i need to get the result like this
[0] => stdClass Object
(
[id] => 30
[id_land_owner] => 152
[group_name] => Cleaner
[group_users] => abt2050+m1#gmail.com,abt2050+m2#gmail.com,abt2050+m3#gmail.com,abt2050+m4#gmail.com
)
[1] => stdClass Object
(
[id] => 29
[id_land_owner] => 152
[group_name] => Gardener
[group_users] => abt2050+a1#gmail.com,abt2050+a2#gmail.com,abt2050+a3#gmail.com
)
need to do this with sql query only
I'm using codeigniter fraework
You can try this as you are using codeigniter
there is a mysql function called GROUP_CONCAT
learn more about it and you will understand
$this->db->select('mo.id, mo.id_land_owner as id_land_owner, mo.group_name, GROUP_CONCAT(mu.username SEPARATOR ",") as group_users', false);
$this->db->from('maintenance_users as mu');
$this->db->join('maintenance_owner as mo','mu.id_maintenance = mo.id_mu');
$this->db->group_by('mo.group_name');
$this->db->where('mo.id_land_owner', 152);
return $this->db->get()->result();
Related
First off, I'm new to PHP and coding in general, so this might be quite an obvious answer.
I'm currently working with the Strava API, and I'm trying to extract data from an Array/Object which is the result of the following API call:
$recentactivities = $api->get('athlete/activities', array('per_page' => 100));
which returns:
Array (
[1] => stdClass Object (
[id] => XXXX
[resource_state] => 2
[external_id] => XXXX
[upload_id] => XXXX
[athlete] => stdClass Object (
[id] => XXXX
[resource_state] => 1
)
[name] => Let\'s see if I can remember how to do this cycling malarkey...
[distance] => 11858.3
[moving_time] => 1812
[elapsed_time] => 2220
[total_elevation_gain] => 44
[type] => Ride
[start_date] => 2014-07-12T13:48:17Z
[start_date_local] => 2014-07-12T14:48:17Z
[timezone] => (
GMT+00:00
) Europe/London
[start_latlng] => Array (
[0] => XXXX
[1] => XXXX
)
[end_latlng] => Array (
[0] => XXXX
[1] => -XXXX
)
[location_city] => XXXX
[location_state] => England
[location_country] => United Kingdom
[start_latitude] => XXXX
[start_longitude] => XXXXX
[achievement_count] => 4
[kudos_count] => 1
[comment_count] => 0
[athlete_count] => 1
[photo_count] => 0
[map] => stdClass Object (
[id] => a164894160
[summary_polyline] => XXXX
[resource_state] => 2
)
[trainer] =>
[commute] =>
[manual] =>
[private] =>
[flagged] =>
[gear_id] => b739244
[average_speed] => 6.544
[max_speed] => 10.8
[average_cadence] => 55.2
[average_temp] => 29
[average_watts] => 99.3
[kilojoules] => 179.9
[device_watts] =>
[average_heartrate] => 191.2
[max_heartrate] => 200
[truncated] =>
[has_kudoed] =>
)
This repeats for the most recent activities.
I'm attempting to extract average_heartrate, which I can do for a single object using the following:
$recentactivities[1]->average_heartrate;
but I'd like to extract all instances of average_heartrate from the Array. I've tried to use a foreach statement, but to be honest, I have no idea where to start.
Any help would be much appreciated.
It's actually pretty simple you can indeed use a foreach loop:
foreach($myArray as $obj){
//$obj is an object so:
if(isset($obj->average_heartrate)){
echo $obj->average_heartrate;
}
}
With this code you iterate through your array with objects and save the wanted array within an array so you can work further with it.
$heartrateArray = array(); // create a new array
//iterate through it with an foreach
foreach($recentactivities as $activity){
// save the average_heartrate as a value under a new key in the array
$heartrateArray[] = $activity->average_heartrate;
}
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
Hi friends,
I have an array of products that I need to create manually next stdclass Object.These array are generated by pushing value.
I'm trying to solve this for more than an hour now, but I don't get it to work. I know it should be easy...but anyway - I don't get it :D
I have working for an hour to create manually next stdclass Object. I want like this.
How do i create this.
Can anyone help with this?
Thanks in Advance,
J
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
[2] => stdClass Object
(
[id] => 16
[user_id] => 1
[following_id] => 5
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
One way of doing this is to use a function like so :
function create($id, $user_id, $following_id, $type, $created_on){
$obj = new stdClass;
$obj->id = $id;
$obj->user_id = $user_id;
$obj->following_id = $following_id;
$obj->type = $type;
$obj->created_on = $created_on;
return $obj;
}
And assuming your array is $array, you add items to it like this:
$array[] = create(16, 1, 5, 'user', '2014-05-27 11:55:17');
Hope this helps
$object = json_decode(file_get_contents('https://api.500px.com/v1/photos/?feature=user&username=nsz&consumer_key=xxx'), true);
This object have the following structure:
Array
(
[0] => Array
(
[id] => 176621
[name] => ***
[description] =>
[times_viewed] => 1411
[rating] => 47.7
[created_at] => 2010-10-08T14:39:43-04:00
[category] => 11
[privacy] =>
[votes_count] => 65
[favorites_count] => 8
[comments_count] => 7
[nsfw] =>
[store_width] => 643
[store_height] => 915
[image_url] => http://pcdn.500px.net/176621/6f2932b14d545f9664e9472a01ae74596f8d4588/2.jpg
What i would like to know is: How to make a foreach loop that saves each file from the [image_url] to my server.
This seems to be working, but i dont know how to make the loop:
copy('http://path/to/image.jpg', 'images/flower.jpg');
Thanks in advance!
I'm trying to use the stackoverflow API and I want to get answers of a question in a php array. So far here is my php code:
<?php
//KEY
$string = "key=my_key";
//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";
//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);
?>
I want to store the answers of a question in an array called answers so that I can access them with a for loop.
The answer i get is :
stdClass Object
(
[total] => 2618591
[page] => 1
[pagesize] => 30
[questions] => Array
(
[0] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => ssh
[2] => openssh
[3] => rsacryptoserviceprovider
)
[answer_count] => 1
[favorite_count] => 0
[question_timeline_url] => /questions/9164203/timeline
[question_comments_url] => /questions/9164203/comments
[question_answers_url] => /questions/9164203/answers
[question_id] => 9164203
[owner] => stdClass Object
(
[user_id] => 311966
[user_type] => registered
[display_name] => simonc
[reputation] => 301
[email_hash] => 021f3344004f0c886d715314fa02037d
)
[creation_date] => 1328548627
[last_edit_date] => 1328611688
[last_activity_date] => 1328611688
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 25
[score] => 0
[community_owned] =>
[title] => Format of PKCS private keys
)
[1] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => .net
[2] => combobox
)
[answer_count] => 3
[favorite_count] => 0
[question_timeline_url] => /questions/9174765/timeline
[question_comments_url] => /questions/9174765/comments
[question_answers_url] => /questions/9174765/answers
[question_id] => 9174765
[owner] => stdClass Object
(
[user_id] => 1194399
[user_type] => registered
[display_name] => Goxy
[reputation] => 1
[email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
)
[creation_date] => 1328611202
[last_activity_date] => 1328611686
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 15
[score] => 0
[community_owned] =>
[title] => WPF: Bind simple List<myClass> to Combobox
)
....
Not sure exactly which property you want to extract, but I assume it's the 'question_answers_url'.
$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
//assuming it is the 'question_answers_url' property that you want
array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}
Ought to do it.
I have some XML that is being returned to be as an object, like this:
SwitchvoxResponse Object
(
[apiStatus:private] => success
[apiErrors:private] => Array
(
)
[apiResult:private] => Array
(
[calls] => Array
(
[page_number] => 1
[total_pages] => 1
[items_per_page] => 50
[total_items] => 1
[call] => Array
(
[0] => Array
(
[id] => 14301
[origination] => outgoing
[start_time] => 2011-06-17 13:40:58
[from] => CALLER_NAME <4485>
[from_account_id] => 1120
[from_name] => CALLER_NAME
[from_number] => 4485
[to] => CALLEE_NAME <6534>
[to_account_id] => 1101
[to_name] => CALLEE_NAME
[to_number] => 6534
[total_duration] => 47
[talk_duration] => 43
[events] => Array
(
[event] => Array
(
[0] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => OUTGOING
[display] => Dialed number (6534)
)
[1] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => INTERNAL
[display] => Rang CALLEE_NAME <6534>
)
[2] => Array
(
[start_time] => 2011-06-17 13:41:02
[type] => TALKING
[display] => Talked to CALLEE_NAME <6534> for 43 seconds
)
[3] => Array
(
[start_time] => 2011-06-17 13:41:45
[type] => HANGUP
[display] => Call was hung up by CALLER_NAME <4485>
)
)
)
)
)
)
)
)
How do I pull out the values of these variables?
The results can be accessed via the SwitchvoxResponse::getResult() method. Given $object is the SwitchvoxResponse object quoted in the question, the example below loops over each call and prints the from values.
$result = $object->getResult();
foreach ($result['calls']['call'] as $call) {
echo $call['from'];
}
Similarly, the response status is fetched via $object->getResponseStatus() and any errors via $object->getErrors().
The response statuses can be one of SV_RESPONSE_SUCCESS, SV_RESPONSE_FAULT or SV_RESPONSE_FAILED.
Edit re. comments
To get the items for the first call only, simply do:
$result = $object->getResult();
$call = $result['calls']['call'][0];
// And access the values like
echo $call['from_name'];
All three properties are marked private, so they would have to be obtained through accessor methods marked public