Send Http.MultipartRequest as array in flutter? - ios

this is how i am post in data to server
var request = new http.MultipartRequest("POST", uri);
request.headers.addAll(headers);
request.fields['property_id'] = property_id; //value from textcontroller
request.fields['unit_id'] = unitlist; //value from textcontroller
request.fields['tenant_name'] = tenant_name; //value from textcontroller
request.fields['additional_terms'] = addistiona_tems;//value from textcontroller
var stream = new http.ByteStream(DelegatingStream.typed(file.openRead()));
var length = await file.length();
var tenantimage = new http.MultipartFile('Tenant_image', stream, length,
filename: basename(file.path));
request.files.add(tenantimage);
var response = await request.send();
this is my result when i return sending value as response
Array
(
[property_id] => 2
[unit_id] => 1463
[tenant_name] => aqws
[additional_terms] => awedswer
)
but i want achieve as "unit_id" and "additional_terms" as array?
like this
Array
(
[property_id] => 2
[unit_id] => Array
(
[0]
)
[tenant_name] => aqws
[additional_terms] => Array
(
[0]=> awedswer
[1]=> bilk
)
)

try this:-
request.fields['additional_terms'] = "[{"shop_id":"1","product_id":1,"unit_price":188.0,"quantity":1,"discounted_price":0.0}]"

Related

Google Sheets PHP API: 'valueInputOption' is required but not specified

I'm having trouble because my code suddenly doesn't work.
I get the error "'valueInputOption' is required but not specified", when using the batchUpdate() function of the Sheets API.
I worked fine until now, but suddenly I get the above mentioned error.
You can see the complete response here https://analyse.sengeguruen.dk/micro.php
This is my code
private function writeUrlData() {
$totals = array( 'clicks' => 0,
'sales' => 0,
'revenue' => 0
);
if(isset($this->microData[$this->landingpage]) && is_array($this->microData[$this->landingpage]) && count($this->microData[$this->landingpage]) > 0) {
$cell_values = array();
$service = new Google_Service_Sheets($this->sheets_client);
/* Clear columns first*/
$cellsToClear = 'Micro-niveau!A2:E99999';
$clearRequestBody = new Google_Service_Sheets_ClearValuesRequest();
$clearResult = $service->spreadsheets_values->clear($this->spreadsheetId, $cellsToClear, $clearRequestBody);
$row_index = 2;
foreach($this->microData[$this->landingpage] as $program_id => $url_data) {
$clicks = isset($url_data['clicks']) ? $url_data['clicks'] : 0;
$sales = isset($url_data['sales']) ? $url_data['sales'] : 0;
$revenue = isset($url_data['revenue']) ? $url_data['revenue'] : 0;
$epc = $clicks > 0 ? round($revenue / $clicks, 2) : 0;
$program_name = $url_data['program_name'] != '' ? $url_data['program_name'] : $program_id;
$totals['clicks'] += $clicks;
$totals['sales'] += $sales;
$totals['revenue'] += $revenue;
$cell_values[] = new Google_Service_Sheets_ValueRange(array('range' => 'Micro-niveau!A'.$row_index.':E'.$row_index, 'values' => [[
$program_name,
$clicks,
$sales,
$revenue,
$epc
]]));
$row_index++;
}
// Insert row with totals
$cell_values[] = new Google_Service_Sheets_ValueRange(array('range' => 'Micro-niveau!A'.($row_index+1 ).':E'.($row_index+1), 'values' => [[
'Totals',
$totals['clicks'],
$totals['sales'],
$totals['revenue'],
round(($totals['revenue'] / $totals['clicks']), 2)
]]));
$batchBody = new Google_Service_Sheets_BatchUpdateValuesRequest([
'valueInputOption' => 'USER_ENTERED',
'data' => $cell_values
]);
try {
$batchResult = $service->spreadsheets_values->batchUpdate($this->spreadsheetId, $batchBody);
} catch (Exception $e) {
dump($e);
}
echo 'Done!';
}
}
I've tried finding solutions in older questions without any luck.
So I found out, that the issue was encoding of the text string for the variable $program_name.
So I changed the code to
private function writeUrlData() {
$totals = array( 'clicks' => 0,
'sales' => 0,
'revenue' => 0
);
if(isset($this->microData[$this->landingpage]) && is_array($this->microData[$this->landingpage]) && count($this->microData[$this->landingpage]) > 0) {
$cell_values = array();
$service = new Google_Service_Sheets($this->sheets_client);
/* Clear columns first*/
$cellsToClear = 'Micro-niveau!A2:E99999';
$clearRequestBody = new Google_Service_Sheets_ClearValuesRequest();
$clearResult = $service->spreadsheets_values->clear($this->spreadsheetId, $cellsToClear, $clearRequestBody);
$row_index = 2;
foreach($this->microData[$this->landingpage] as $program_id => $url_data) {
$clicks = isset($url_data['clicks']) ? $url_data['clicks'] : 0;
$sales = isset($url_data['sales']) ? $url_data['sales'] : 0;
$revenue = isset($url_data['revenue']) ? $url_data['revenue'] : 0;
$epc = $clicks > 0 ? round($revenue / $clicks, 2) : 0;
$program_name = $url_data['program_name'] != '' ? $url_data['program_name'] : $program_id;
$totals['clicks'] += $clicks;
$totals['sales'] += $sales;
$totals['revenue'] += $revenue;
$cell_values[] = new Google_Service_Sheets_ValueRange(array('range' => 'Micro-niveau!A'.$row_index.':E'.$row_index, 'values' => [[
utf8_encode($program_name),
$clicks,
$sales,
$revenue,
$epc
]]));
$row_index++;
}
// Insert row with totals
$cell_values[] = new Google_Service_Sheets_ValueRange(array('range' => 'Micro-niveau!A'.($row_index+1 ).':E'.($row_index+1), 'values' => [[
'Totals',
$totals['clicks'],
$totals['sales'],
$totals['revenue'],
round(($totals['revenue'] / $totals['clicks']), 2)
]]));
$batchBody = new Google_Service_Sheets_BatchUpdateValuesRequest([
'valueInputOption' => 'USER_ENTERED',
'data' => $cell_values
]);
try {
$batchResult = $service->spreadsheets_values->batchUpdate($this->spreadsheetId, $batchBody);
} catch (Exception $e) {
dump($e);
}
echo 'Done!';
}
}

LINQ query with sub-query list

The query below should be selecting a list of information from different tables, and should also bring back a list of VehicleNames that are related to each ID coming back from db.Reservations
The query brings back data, but the list of VehicleNames only has one record, and it should bring back anywhere up to 5 records, depending on how many vehicles were reserved for that specific instance. I have tried adding a foreach to the VehicleName line in the select but I don't think I am using it right. Does anyone have any ideas as to how I can retrieve the list of values I need?
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where v.ID == rtv.VehicleID
select v.VehicleName).ToList()
};
EDIT: Updated query, still uncertain how to get back list of Vehicles that have been reserved
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID).ToList()
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where v.ID == rtv.VehicleID
select v.VehicleName).ToList()
};
rtv.VehicleID does not exist anymore, now rtv comes back with a list of IDs....I need to be able to get into that list to find where v.ID is IN the rtv list
Looks like people already told you this in the comments, but here's the code that ought to work:
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID)
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where rtv.Contains(v.ID)
select v.VehicleName).ToList()
};
I'd also recommend looking at setting up navigation properties between your entities. Your query could be much simpler, something like this:
var query = from r in db.Reservations
let e = r.Requestor
let rs = r.ReservationStatus
let rst = rs.ReservationStatusType
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = r.Vehicles.Select(v => v.VehicleName).ToList()
};

Can i use array with lambda notation?

Here is my Code :
string[] aa = new string[] { "Title", "Image", "Description" };
for (var i = 0; i <= 3; i++)
{
aa[i] = ems.SupportAdds.Where(x => x.ArticleId == id).Select(x => x.aa[i]).ToArray();
ViewBag.Collection[i] = aa[i];
}
I am getting error near x.aa[i].It is saying Table doesn't contain aa[i]. I want to use like that to short code. Please help me. I have its long version too which is working with my code.
Here is that one:
var Title = ems.SupportAdds.Where(x => x.ArticleId == id).Select(x => x.Title).ToArray();
ViewBag.Collection1 = Title;
var Description = ems.SupportAdds.Where(x => x.ArticleId == id).Select(x => x.Description).ToArray();
ViewBag.Collection2 = Description;
var Image = ems.SupportAdds.Where(x => x.ArticleId == id).Select(x => x.Image).ToArray();
ViewBag.Collection3 = Image;
And In view :
#Html.Raw(ViewBag.Collection2[i]
#Html.Raw(ViewBag.Collection2[i]
#Html.Raw(ViewBag.Collection3[i] // with increasing forloop which is working fine.
But I want to short it so I thought to use an array but it is not working with array. Can you guys suggest me the better way to write this code with lambda notation? Help will be appreciated.Thanks
I think you can improve performance by making changes like this
var allRecords= ems.SupportAdds.Where(x => x.ArticleId == id);
ViewBag.Collection1 = allRecords.Select(x => x.Title).ToArray();
ViewBag.Collection2 = allRecords.Select(x => x.Description).ToArray();
ViewBag.Collection3 = allRecords.Select(x => x.Image).ToArray();

Twitter API call not working

Hi Friends i am trying to run twitter apis to get tweets for a hashtag using below code. When i tried get the user timeline it's not giving any error for authentication but when it tried to search for tweets which contains hahstag it's giving authentication error.
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path
$query = array( // query parameters
'screen_name' => 'twitterapi'
);
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_token' => $token,
'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0'
);
$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);
$arr = array_merge($oauth, $query); // combine the values THEN sort
asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)
// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));
$url = "https://$host$path";
// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);
$url=str_replace("&","&",$url); //Patch by #Frewuill
$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it
// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);
// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
echo $auth;exit;
// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
echo "<pre>";print_r($twitter_data);
When i run this code i am successfully able to get the user time line so i wnt for next step to get tweets for a particular hashtag by chnaging code like below
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/search/tweets.json'; // api call path
$query = array( // query parameters
'q' => '#Polls2013'
);
But now it's giving a weird error like below.
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => Could not authenticate you
[code] => 32
)
)
)
The query you are posting for search should be url encoded in the manner specified by twitter,
See this documentation (https://dev.twitter.com/docs/auth/percent-encoding-parameters)

Wordpress if custom field value is null previous posts' value is displayed

I have this code with a loop that gives me some trouble and strange results.
This particular post type has a few custom fields which I put in an array. Everything works as expected, but if a field has no value it will take the value from the previous post within the loop.
Let's say I have these posts in Wordpress:
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = null
custom_3 = null
If I run my loop I get these results
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = 20 (instead of null)
custom_3 = 30 (instead of null)
Here's the short version of that loop:
$query = new WP_Query($query_arg);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$result[] = array(
"custom_1" => get_post_meta($post->ID, 'custom_1', true),
"custom_2" => get_post_meta($post->ID, 'custom_2', true),
"custom_3" => get_post_meta($post->ID, 'custom_3', true)
);
}
}
wp_reset_postdata();
I can't seem to wrap my head around this one.. tried pretty much everything, but nothing seams to work.
Does anyone know what's happening here?
UPDATE:
Fixed it by changing the loop to this.
$query = new WP_Query($query_arg);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
//set vars to "" which 'resets' the value with every new post in the loop
$custom_1 = "";
$custom_2 = "";
$custom_3 = "";
//set vars to the value of the custom fields
$custom_1 => get_post_meta($post->ID, 'custom_1', true);
$custom_2 => get_post_meta($post->ID, 'custom_2', true);
$custom_3 => get_post_meta($post->ID, 'custom_3', true);
$result[] = array(
"custom_1" => $custom_1,
"custom_2" => $custom_2,
"custom_3" => $custom_3
);
}
}
wp_reset_postdata();
It might be due to how the MYSQL handles null. Null means an absence of value, so they do not get a space in the database, not even an empty one. An empty string however does show in the database.
Try the following
Post ID 10
custom_1 = 10
custom_2 = 20
custom_3 = 30
Post ID 20
custom_1 = 40
custom_2 = ""
custom_3 = ""

Resources