I'm having a little trouble with Smart for-each loops - here's my code:
{* this example will print out all the values of the $custid array *}
{foreach from=$order_info item=amount}
amount: {$amount}<br>
{/foreach}
So basically I'm just trying to loop through and print the "amount" from $order_info - the problem is, it just prints the whole array and not just the "amount".
Here's a reduced number of items from the array that I got from the debug console:
{$order_info} Array (86)
order_id => "13"
is_parent_order => "N"
parent_order_id => "0"
company_id => "0"
user_id => "1"
total => "1077.87"
subtotal => "1049.87"
discount => "0.00"
subtotal_discount => "0.00"
payment_surcharge => "0.00"
shipping_ids => "1"
shipping_cost => "28.00"
timestamp => "1352380055"
status => "O"
notes => ""
details => ""
promotions => Array (0)
promotion_ids => ""
title => ""
firstname => "John"
lastname => "Doe"
company => ""
b_title => ""
b_firstname => "John"
b_lastname => "Doe"
b_address => "44 Main street"
b_address_2 => "test"
b_city => "Boston"
b_county => ""
b_state => "MA"
b_country => "US"
b_zipcode => "02134"
b_phone => ""
s_title => ""
s_firstname => "John"
s_lastname => "Doe"
s_address => "44 Main street"
s_address_2 => "test"
s_city => "Boston"
s_county => ""
s_state => "MA"
s_country => "US"
s_zipcode => "02134"
s_phone => ""
phone => ""
fax => ""
url => ""
email => "joe#burford.co.uk"
payment_id => "3"
tax_exempt => "N"
lang_code => "EN"
ip_address => "62.49.144.33"
repaid => "0"
validation_code => ""
localization_id => "0"
payment_method => Array (15)
payment_id => "3"
usergroup_ids => "0"
position => "30"
status => "A"
template => "check.tpl"
processor_id => "0"
params => ""
a_surcharge => "0.000"
p_surcharge => "0.000"
localization => ""
payment => "Check"
description => "Check payment"
instructions => "Type instructions to pay by visiting ..."
lang_code => "EN"
processor => ""
fields => Array (0)
items => Array (2)
2902390881 => Array (19)
item_id => "2902390881"
order_id => "13"
product_id => "1110"
product_code => "B0002I9OJS"
price => "549.99"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "eMachines T2958 Desktop PC (2.66 GHz ..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "549.99"
stored_price => "N"
product => "eMachines T2958 Desktop PC (2.66 GHz ..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "549.99"
original_price => "549.99"
cart_id => "2902390881"
tax_value => "0"
subtotal => "549.99"
display_subtotal => "549.99"
shipped_amount => "0"
shipment_amount => "1"
2278796194 => Array (19)
item_id => "2278796194"
order_id => "13"
product_id => "1120"
product_code => "B00086HZJA"
price => "499.88"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "HP Pavilion a1000n Desktop PC (Intel ..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "499.88"
stored_price => "N"
product => "HP Pavilion a1000n Desktop PC (Intel ..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "499.88"
original_price => "499.88"
cart_id => "2278796194"
tax_value => "0"
subtotal => "499.88"
display_subtotal => "499.88"
shipped_amount => "0"
shipment_amount => "1"
So what I'm really trying to do is get the amount from items within $order_info - I tried $order_info.items but this caused the site to crash.
Any ideas are much appreciated.
This solved it:
{foreach from=$order_info.items item=foo}
{$foo.product_id}
{/foreach}
Wouldn't it be something like
{foreach from=$order_info.items item=items}
{foreach from=$items item=item}
amount: {$item.amount}<br>
{/foreach}
{/foreach}
Assuming I understood your structure as $order_info -> items array -> (item item item item), and each item having an .amount property.
Related
I am using Ruby on Rails application. I want to combine 2 array of hashes with hash and to result in array of hashes.
Inputs:
first_array_of_hash = [{:name => "John", :age => 34, :mode => "nullable"},{:name => "Rose", :age => 30, :mode => "nullable"}]
second_hash = {:field_name => "", :field_age => nil, :field_nullable => false, :field_default => ""}
I want my result to be like below
result = [{:field_name => "John", :field_age => 34, :field_nullable => true, :field_default => ""},{:field_name => "Rose", :field_age => 30, :field_nullable => true, :field_default => ""}]
You can use a regular Array#map for this:
first_array_of_hash = [{:name => "John", :age => 34, :nullable => 'yes'},{:name => "Rose", :age => 30, :nullable => 'no'}]
second_hash = {:field_name => "", :field_age => nil, :field_nullable => false, :field_default => ""}
def transform(object)
{
field_name: object[:name],
field_age: object[:age],
field_nullable: object[:mode] == 'nullable'
}
end
result = first_array_of_hash.map do |object|
second_hash.merge(transform(object))
end
puts result
I got this query:
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_time"},
"month" => {"$month" => "$created_time"},
"year" => {"$year" => "$created_time"},
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1}
}},
{"$sort" => {
"created_at" => -1
}}
])
My user table has username and fullname which I need,
I did something like
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_at"},
"month" => {"$month" => "$created_at"},
"year" => {"$year" => "$created_at"},
"username" => "$username"
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1}
}},
{"$sort" => {
"created_at" => -1
}}
])
but it doesn't work and gives same result like above, doesn't print username, any ideas ?
My goal is get username, after grouping by created at date.
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_at"},
"month" => {"$month" => "$created_at"},
"year" => {"$year" => "$created_at"},
"username" => "$username"
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1},
"username" => {"$first" => "$username"}
}},
{"$sort" => {
"created_at" => -1
}}
])
Quick ruby question regarding the manipulation of a Hash in Ruby.
I actually have the following hash:
[2] project(#<V1::UsersController>) » error.info
=> {
:id => "914a24888-5e71-4d12-b9b0-10e2d98f516b",
:game => "vampotron",
:data => {
"private" => {
"name" => "Jean",
"logins" => 2300,
"foo" => "bar"
}
},
:revision => 1
}
I want the hash to become:
[2] project(#<V1::UsersController>) » error.info
=> {
:id => "914a24888-5e71-4d12-b9b0-10e2d98f516b",
:game => "vampotron",
:data => {
"name" => "Jean",
"logins" => 2300,
"foo" => "bar"
},
:revision => 1
}
I would like to remove the 'private' key by keeping the existing k,v pairs in my 'data' hash.
Thanks for your help,
M
The easiest way
hash[:data] = hash[:data]['private']
def self.group_by(field, format = 'day')
key_op = [['year', '$year'], ['month', '$month'], ['day', '$dayOfMonth']]
key_op = key_op.take(1 + key_op.find_index { |key, op| format == key })
project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten]
group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten]
pipeline = [
{"$project" => {"name" => 1, field => 1}.merge(project_date_fields)},
{"$group" => {"_id" => group_id_fields, "count" => {"$sum" => "$QtyUsed"}}},
{"$sort" => {"count" => -1}}
]
collection.aggregate(pipeline)<br>
end
when I execute that script, count result is 0.
How can I sum attributes QtyUsed?
you are not projecting the "QtyUsed" field
pipeline = [
{"$project" => {"name" => 1, field => 1, "QtyUsed' => 1}.merge(project_date_fields)},
{"$group" => {"_id" => group_id_fields, "count" => {"$sum" => "$QtyUsed"}}},
{"$sort" => {"count" => -1}}
]
I'm trying to pass the following ruby hash into an active resource(3.0.9) find(:from) call.
my_hash = {
:p => {:s => 100, :e => 2},
:k => "blah",
:f => [
{
:fl => :bt,
:tp => :trm,
:vl => "A::B"
},
{
:fl => :jcni,
:tp => :trm,
:vl => [133, 134]
},
{
:mnfl => :bmns,
:mxfl => :bmxs,
:tp => :rfstv,
:vl => 1e5
},
{
:fl => :bpo,
:tp => :rftv,
:op => :eta,
:vl => 1.months.ago.strftime("%Y-%m-%d")
}
]
}
Resource.find_by_x_and_y(:all, :from => :blah, params: my_hash)
On the server side action, when I print the params hash, its all messed up. ( last 3 hashes in the array mapped to :f )
{
"f" => [
{
"fl" => "bt",
"tp" => "trm",
"vl" => "A::B"
},
{
"fl" => "jcni",
"tp" => "trm",
"vl" => [
"133",
"134"
],
"mnfl" => "bmns",
"mxfl" => "bmxs"
},
{
"tp" => "rfstv",
"vl" => "100000.0",
"fl" => "bpo",
"op" => "eta"
},
{
"tp" => "rftv",
"vl" => "2013-01-25"
}
],
"k" => "blah",
"p" => {
"e" => "2",
"s" => "100"
},
"action" => "blah",
"controller" => "x/Y",
"format" => "json"
}
my_hash.to_query gives me
f[][fl]=bt&f[][tp]=trm&f[][vl]=A%3A%3AB&f[][fl]=jcni&f[][tp]=trm&f[][vl][]=133&f[][vl][]=134&f[][mnfl]=bmns&f[][mxfl]=bmxs&f[][tp]=rfstv&f[][vl]=100000.0&f[][fl]=bpo&f[][op]=eta&f[][tp]=rftv&f[][vl]=2013-01-25&k=blah&p[e]=2&p[s]=100
which doesn't have indices, hence the mixup.
Is there a name for this type of serialization using "[]" ? Is this guaranteed to serialize/deserialize arbitrarily nested hashes/arrays/primitives faithfully ? How do I make active resource behave sanely ?