PHP mongo Driver skip and limit on inner objects - php

Hello I am using PHP driver for MongoDB and MY Array is something like this
stdClass Object
(
[_id] => stdClass Object
(
[$id] => 58e0af1b9c76847c7e8b4567
)
[prod_details] => stdClass Object
(
[1] => stdClass Object
(
[1] => stdClass Object
(
[prod_id] => 1
[asin] => xxxxxx
[sku] => 1
[prod_name] => Windows 98: Second Edition
[prod_price] => 13433
[prod_price_f] => 13433
[prod_currency] => INR
[prod_price_min] =>
[prod_price_max] =>
[prod_quantity] => 1
[prod_condition] => New
[prod_shipping] => 60.00
[prod_category] =>
[prod_added] =>
)
[11] => stdClass Object
(
[prod_id] => 11
[asin] => xxxxxxx
[sku] => 11
[prod_name] => The Coke Machine: The Dirty Truth Behind the World's Favorite Soft Drink [Paperback] [Sep 06, 2011] Blanding, Michael
[prod_price] => 2500
[prod_price_f] => 2500
[prod_currency] => INR
[prod_price_min] =>
[prod_price_max] =>
[prod_quantity] => 8
[prod_condition] => New
[prod_shipping] => 60.00
[prod_category] =>
[prod_added] =>
)
[12] => stdClass Object
(
[prod_id] => 12
[asin] => xxxxxxx
[sku] => 12
[prod_name] => In Other Words [Hardcover] [Feb 09, 2016] Jhumpa Lahiri
[prod_price] => 748.9
[prod_price_f] => 748.9
[prod_currency] => INR
[prod_price_min] =>
[prod_price_max] =>
[prod_quantity] => 8
[prod_condition] => New
[prod_shipping] => 60.00
[prod_category] =>
[prod_added] =>
)
when I run PHP query I get ALL result and not filtered result with skip and limit
$collection->find(array('_id'=>new MongoId($mongoid)), array('prod_details.1' => ''))->skip(0)->limit(2);
Here I am traversing to prod_details >> 1 and putting limit of 0 to 2 but I get all objects. and limit is not working.
Please help.

Related

Pass Json object to other class method

I'm trying to pass json decoded object to method of a class. But when I try to access properties of that object, it gives notice: Trying to get property of non-object php. But when I write that object variable to a file it shows all data.
I'm trying to update hubspot data using its api when a webhook event is sent by memberpress plugin.
Here is what i'm doing:
File: hubspotsync.php
require_once 'class-synchronize.php';
class HubspotSync{
public function init()
{
$encoded_data = file_get_contents('php://input');
if( $encoded_data != '' ){
$data = json_decode($encoded_data);
$this->perform_action($data);
}
}
private function perform_action($data)
{
$synchronize = new Synchronize();
$synchronize->perform_sync($data);
unset($synchronize);
}
}
$sync = new HubspotSync();
$sync->init();
unset($sync);
File : class-syncronize.php
class Synchronize{
public function perform_sync($eventobj)
{
// If I fwrite this $eventObj to a file, it looks like as below. but gives an notice on next code. why???
$event_type = $eventobj->event; // This line gives the notice.
}
}
$eventobj looks like this:
stdClass Object
(
[event] => subscription-created
[type] => subscription
[data] => stdClass Object
(
[coupon] =>
[membership] => stdClass Object
(
[id] => 16
[title] => Monthly Subscription
[content] =>
[excerpt] =>
[date] => 2016-09-16 19:22:07
[status] => publish
[author] => 4
[date_gmt] => 2016-09-16 19:22:07
[modified] => 2017-09-27 15:19:04
[modified_gmt] => 2017-09-27 20:19:04
[group] => 0
[price] => 28.00
[period] => 1
[period_type] => months
[signup_button_text] => Sign Up
[limit_cycles] =>
[limit_cycles_num] => 2
[limit_cycles_action] => expire
[trial] =>
[trial_days] => 0
[trial_amount] => 0.00
[trial_once] => 1
[group_order] => 0
[is_highlighted] =>
[pricing_title] => Monthly Subscription
[pricing_show_price] => 1
[pricing_display] => auto
[custom_price] =>
[pricing_heading_txt] =>
[pricing_footer_txt] =>
[pricing_button_txt] => Sign Up
[pricing_button_position] => footer
[pricing_benefits] => Array
(
[0] =>
)
[register_price_action] => default
[register_price] =>
[thank_you_page_enabled] => 1
[thank_you_message] => Thank you for signing up for a monthly subscription to The Prudent Speculator. Please contact info#theprudentspeculator.com with any questions regarding your subscription. Your login credentials should be e-mailed to you shortly.
[custom_login_urls_enabled] =>
[custom_login_urls_default] =>
[custom_login_urls] => Array
(
)
[expire_type] => delay
[expire_after] => 1
[expire_unit] => days
[expire_fixed] => 2017-09-27
[tax_exempt] =>
[allow_renewal] =>
[access_url] =>
[disable_address_fields] =>
[simultaneous_subscriptions] =>
[use_custom_template] =>
[custom_template] => custom/page-archive.php
[customize_payment_methods] => 1
[custom_payment_methods] => Array
(
[0] => owyg62-7fu
)
[customize_profile_fields] => 1
[custom_profile_fields] => Array
(
[0] => mepr_phone
[1] => mepr_username
)
[cannot_purchase_message] => You don't have access to purchase this item.
)
[member] => stdClass Object
(
[id] => 25
[email] => craig.andrews#allies4me.com
[username] => craigandrews
[nicename] => craigandrews
[url] =>
[message] =>
[registered_at] => 2017-10-16 20:56:16
[ip] => 24.27.9.104
[first_name] => Craig
[last_name] => Andrews
[display_name] => craigandrews
)
[id] => 7
[subscr_id] => mp-sub-59e7cdb3d47e0
[response] =>
[gateway] => stdClass Object
(
[name] => PayPal Payments Pro
[label] => Paypal
[use_label] => 1
[icon] => https://prudent2.wpengine.com/wp-content/plugins/memberpress/images/checkout/cards.png
[use_icon] => 1
[desc] => Pay with your credit card via PayPal
[use_desc] => 1
[id] => owyg62-7fu
[settings] => stdClass Object
(
[gateway] => MeprPayPalProGateway
[id] => owyg62-7fu
[label] => Paypal
[use_label] => 1
[icon] => https://prudent2.wpengine.com/wp-content/plugins/memberpress/images/checkout/cards.png
[use_icon] => 1
[desc] => Pay with your credit card via PayPal
[use_desc] => 1
[api_username] => kbroughton_api1.afamcapital.com
[api_password] => 48UDEBHJBB6RXNVY
[signature] => AQUF7rVduRDHYM-3AIBZaDFk-4ZYAZQgmcG7B9LdUmyA7fRkA9Mk1gPj
[sandbox] => on
[force_ssl] => on
[debug] =>
[saved] => 1
[url] => https://www.sandbox.paypal.com/cgi-bin/webscr
[api_url] => https://api-3t.sandbox.paypal.com/nvp
[api_version] => 69
)
[capabilities] => Array
(
[0] => process-payments
[1] => process-refunds
[2] => create-subscriptions
[3] => cancel-subscriptions
[4] => update-subscriptions
[5] => suspend-subscriptions
[6] => resume-subscriptions
[7] => subscription-trial-payment
)
)
[ip_addr] => 70.112.70.86
[price] => 28.00
[period] => 1
[period_type] => months
[limit_cycles] => 0
[limit_cycles_num] => 2
[limit_cycles_action] => expire
[prorated_trial] => 0
[trial] => 0
[trial_days] => 0
[trial_amount] => 0.00
[status] => pending
[created_at] => 2017-10-18 21:54:59
[total] => 28.00
[tax_rate] => 0.000
[tax_amount] => 0.00
[tax_desc] =>
[tax_class] => standard
[cc_last4] =>
[cc_exp_month] =>
[cc_exp_year] =>
[token] =>
[tax_compound] => 0
[tax_shipping] => 1
)
)
var_dump( $eventObj ) and make sure that the event property actually exists on that object. It might be $eventObj['event'] or something weird like that. The error is telling you that event doesnt exist on your $eventObj

Fitbit API unable to fetch data synced through Fitbit device i.e Blaze

I am trying to fetch data using the following link:- https://dev.fitbit.com/docs/activity/ and API :- GET https://api.fitbit.com/1/user/[user-id]/activities/date/[date].json and getting response array as below
Array(
[result] => Array
(
[activities] => Array
(
[0] => Array
(
[activityId] => 90013
[activityParentId] => 90013
[activityParentName] => Walk
[calories] => 25
[description] => Walking less than 2 mph, strolling very slowly
[distance] => 0.271426
[duration] => 355000
[hasStartTime] => 1
[isFavorite] =>
[lastModified] => 2016-11-29T19:11:42.000Z
[logId] => 5007419020
[name] => Walk
[startDate] => 2016-11-30
[startTime] => 00:34
[steps] => 342
)
)
[goals] => Array
(
[activeMinutes] => 30
[caloriesOut] => 2157
[distance] => 8.05
[floors] => 10
[steps] => 10000
)
[summary] => Array
(
[activeScore] => -1
[activityCalories] => 55
[caloriesBMR] => 1257
[caloriesOut] => 1300
[distances] => Array
(
[0] => Array
(
[activity] => Walk
[distance] => 0.271426
)
[1] => Array
(
[activity] => total
[distance] => 0.48
)
[2] => Array
(
[activity] => tracker
[distance] => 0.42
)
[3] => Array
(
[activity] => loggedActivities
[distance] => 0.271426
)
[4] => Array
(
[activity] => veryActive
[distance] => 0.09
)
[5] => Array
(
[activity] => moderatelyActive
[distance] => 0.11
)
[6] => Array
(
[activity] => lightlyActive
[distance] => 0.16
)
[7] => Array
(
[activity] => sedentaryActive
[distance] => 0.12
)
)
[elevation] => 0
[fairlyActiveMinutes] => 5
[floors] => 0
[heartRateZones] => Array
(
[0] => Array
(
[caloriesOut] => 37.9099
[max] => 98
[min] => 30
[minutes] => 25
[name] => Out of Range
)
[1] => Array
(
[caloriesOut] => 33.193
[max] => 137
[min] => 98
[minutes] => 9
[name] => Fat Burn
)
[2] => Array
(
[caloriesOut] => 0
[max] => 166
[min] => 137
[minutes] => 0
[name] => Cardio
)
[3] => Array
(
[caloriesOut] => 0
[max] => 220
[min] => 166
[minutes] => 0
[name] => Peak
)
)
[lightlyActiveMinutes] => 6
[marginalCalories] => 33
[sedentaryMinutes] => 1426
[steps] => 789
[veryActiveMinutes] => 3
)
)
[code] => 200
[content_type] => application/json;charset=UTF-8
)
but the problem is this array just provides me the data of the activity that has been done using Fitbit application. But I have to fetch complete data whether its by the Fitbit application or Fitbit device i.e blaze.
Just glancing at the API, I believe you need to use activities/tracker/. The API states that this represents the daily activity values logged by the tracker device only, excluding manual activity log entries. So you just need to add tracker to your GET.
Tracker Activities:
activities/tracker/calories
activities/tracker/steps
activities/tracker/distance
activities/tracker/floors
activities/tracker/elevation
activities/tracker/minutesSedentary
activities/tracker/minutesLightlyActive
activities/tracker/minutesFairlyActive
activities/tracker/minutesVeryActive
activities/tracker/activityCalories

PHP Accessing an item in an array

I have the following array:
Cart Object
(
[event_list] => Array
(
[15] => stdClass Object
(
[event_id] => 15
[event_name] => North Pole Express 2014
[event_date] => 2014-12-06
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[14] => stdClass Object
(
[event_id] => 14
[event_name] => North Pole Express 2014
[event_date] => 2014-11-30
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[13] => stdClass Object
(
[event_id] => 13
[event_name] => North Pole Express 2014
[event_date] => 2014-11-29
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
)
[cat_list] => Array
(
[138] => stdClass Object
(
[cat_id] => 138
[category_event_id] => 15
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[120] => stdClass Object
(
[cat_id] => 120
[category_event_id] => 14
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[102] => stdClass Object
(
[cat_id] => 102
[category_event_id] => 13
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
)
[disc_list] => Array
(
)
[items] => Array
(
[4] => PlaceItem Object
(
[id] => 4
[cart] => Cart Object
*RECURSION*
[event_id] => 14
[category_id] => 120
[seats] => Array
(
[26151] => stdClass Object
(
[seat_id] => 26151
[seat_row_nr] => 0
[seat_nr] => 0
[seat_ts] => 1388769219
[discount_id] => 0
)
)
[ts] =>
[created] => 2014-01-03T16:43:39+00:00
[expired] =>
)
)
[ts] => 1388769219
)
I need to be able to access the 'Items' Key.
However everything I have tried so far has failed. I suspect I am missing something really obvious.
What would be the best way to access these items?
Cart is not an array, it's an Object.
$cart = new Cart();
$cart->items; // <-- this is an array (of PlaceItems)
Since $cart is an object you need to use object notation to access it, the arrays are underneath that:
forach ($cart->items as $key=>$value) {
//...
}
Update from your comment:
$items=$_SESSION['_SMART_cart']->items;
foreach ($items as $key=>$value) {
//...
}

Getting the value of first array from the array

In php I have an array look like this.
Array ( [0] =>
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
[2] => Array ( [id] => 12 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
[3] => Array ( [id] => 14 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
)
From here I want to get the first value of array. For example I want to get the value of first array
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
So can someone kindly tell me how to get the value of 1st array?Any help and suggestions will be really appreciable. Thanks
say all your array was in a variable $myArray, then
myArray[1]
will give you your first array

how to get values from SimpleXml Object

SimpleXMLElement Object (
[results] => 2
[total_results] => 2
[plans] => SimpleXMLElement Object ( [plan] => Array ( [0] => SimpleXMLElement Object ( [id] => 1014 [type] => paid [name] => Silver [amount] => 1000.00 [interval] => 30 [notification_url] => http://www.google.com [free_trial] => 0 [occurrences] => 1 [num_customers] => 0 [status] => active ) [1] => SimpleXMLElement Object ( [id] => 1013 [type] => paid [name] => Gold [amount] => 500.00 [interval] => 30 [notification_url] => http://www.movebyu.com/ExpressEngine/index.php/?ACT=21 [free_trial] => 0 [occurrences] => 1 [num_customers] => 1 [status] => active ) ) )
),
How to get values of plans, amount from these, can any one help me?
Via XPath
http://php.net/manual/en/simplexmlelement.xpath.php

Categories