I'm receiving data in the following format from a multi-checkbox
["Ethnicity"]=>
array(3) {
["Maori"]=>
string(5) "Maori"
["Pacific Peoples"]=>
string(15) "Pacific Peoples"
["Other European"]=>
string(14) "Other European"
}
I'm trying to get this into a multi-checkbox in Jira via an API call using the following segment
'customfield_11337' => [
"value" => $data["Ethnicity"]
],
But this returns an error string(21) "data was not an array"
So I've tried to massage the data into a single array using
$ethnicityArray = array();
foreach ($data["Ethnicity"] as $eth => $value) {
array_push($ethnicityArray, $value);
}
But this returns the same error. I should note that I've got no problem populating radio buttons, text fields etc in Jira via the same method. It just seems to be checkboxes I can't get right.
How do I go about solving this using PHP?
The correct solution was to get the data into a format that looks like:
'customfield_11333' => [["value" => "Asian"], ["value" => "Other"]]
Related
I have a returned string result from an API and it looks like this.. for the life of me, I cannot retried the WorkID value!
The returned string is a json string:
{"notes":"","RecordsStatus":"{\"0\":{\"WorkID\":\"0090210\",\"Message\":\"Record Created\"}}"}
It has two parts:
“notes” and “RecordStatus”.
If message is empty, means the batch is imported without error.
In RecordStatus, there are two parts too.
First is the index number of the record, and it has second part that the key for the record created(in my case it’s the WorkID) and a message tells the record is created or updated(in my case, it’s created).
Array
(
[notes] =>
[RecordsStatus] => {"0":{"WorkID":"0090210","Message":"Record Created"}}
)
Do a Var_dump() of decoded_json results in this:
array(2) {
["notes"]=>
string(0) ""
["RecordsStatus"]=>
string(52) "{"0":{"WorkID":"0090210","Message":"Record Created"}}"
}
I tried
foreach($decoded_json as $item) {
$uses = $item['RecordsStatus'][0]['WorkID']; //etc
}
but does not work
I'm getting data from multiple API requests and storing each in a separate MySQL table. For each request I have an associated table, with field names matching the JSON API response. Since I'm not using all of fields from the API, I'm finding the fields in the MySQL table and using that to create the prepared statement with PDO, then feeding the results array into that for execution. Here's the function that accepts the statement and the results array:
function insert_array($sql,$args)
{
$this->connect();
$q = $this->con->prepare($sql);
foreach($args as $record) {
$q ->execute($record);
echo "<pre>";var_dump($record);echo "</pre>";
$arr = $q->errorInfo();
print_r($arr);
}
$this->disconnect();
return $q;
}
The last three lines in the foreach loop are just for debugging.
This worked fine for my first request, but no records are inserted, and I receive HY093, for others.
For the one that works, the prepared statement ($sql) comes out as
INSERT INTO fs_dynamicagents (agent_id, firstname, lastname) VALUES (:agent_id, :firstname, :lastname) ON DUPLICATE KEY UPDATE firstname=:firstname, lastname=:lastname
I'm finding unique fields first, so that's why agent_id isn't in the update statement. This inserts successfully, even though I'm not using all the fields. Here's the output of the var_dump and errorInfo:
array(4) {
["agent_id"]=>
string(3) "002"
["firstname"]=>
string(9) "Bill"
["lastname"]=>
string(5) "Murray"
["password"]=>
string(4) "1212"
}
Array ( [0] => 00000 [1] => [2] => )
Now here's an example of one that doesn't work:
INSERT INTO fs_queue (name, record) VALUES (:name, :record) ON DUPLICATE KEY UPDATE record=:record
And part of the first API record:
array(79) {
["name"]=>
string(7) "Choice1"
["fc_name"]=>
string(7) "Choice1"
["friendlyname"]=>
string(7) "Choice1"
["record"]=>
string(1) "1"
["agent_announcement_file"]=>
string(0) ""
["play_agent_announcement_file"]=>
string(1) "0"
["incoming_call_script"]=>
string(0) ""
["caller_agent_match"]=>
string(1) "0"
["survey_id"]=>
NULL
}
Array ( [0] => HY093 [1] => [2] => )
You can see I haven't included all 79 of the fields, but I've tried to include at least the fields with "name" in the label, and an empty string and a null value. Nothing but "name" and "record" should be bound, so I don't see those as a problem.
Every instance I've found online for this error code was due to a type (or case sensitivity). I've tried defining "record" as an int and a varchar.
Well, I had hoped that the process of typing this out would expose the problem to me, but no such luck. If a night's sleep doesn't help, I'd love to hear thoughts.
Edit: Something else I have tried is removing the ON DUPLICATE UPDATE section (and emptied the table so there will not be any duplicates) so that each parameter is only bound once. It sounds like that was a bug a few years ago that has been fixes, but even without that I receive the same error.
Edit2: Hmm, even stranger, removing the ON DUPLICATE UPDATE causes some of my previously working statements to fail with the same error. Not all of them, and of course those that don't fail for that reason will fail if it runs into a duplicate.
Edit3: Something else I have tried is removing the binding-by-key for the update statement, and changing this to
INSERT INTO fs_queue (name, record) VALUES (:name, :record) ON DUPLICATE KEY UPDATE record= VALUES(record)
I didn't think that would fix it, because it succeeds the first way on other tables, and this does in fact still fail.
Edit4: I was able to make one of these work by adding fields to the MySQL table so that all the columns from the input array were being used. However, I don't think that's what really solved the problem, because I have others succeeding without all columns being used, even in the middle of the array.
Ok, I figured it out. First, I was not setting ATTR_EMULATE_PREPARES at all, which means it would default to the database preparation engine unless the PDO engine was required. Since MySQL cannot re-use placeholders, it was using the PDO engine. Setting that to false would force the MySQL engine, and all requests would fail.
So the PDO engine can re-use placeholders, but however that happens it's not very good at finding the values. Even trying to find 2 out of 3 columns it would sometimes fail. So rather than let PDO sort it out, I'm throwing out everything I don't need before I send it to be inserted.
I'm using a function to delete columns that I found here.
<?php
function delete_col(&$array, $key) {
return array_walk($array, function (&$v) use ($key) {
unset($v[$key]);
});
}
$table_fields = array("id","fruit");
$insert_data = array(
array(
"id" => "1",
"fruit" => "Apple",
"color" => "Red"
),array(
"id" => "2",
"fruit" => "Apple",
"color" => "Green"
),array(
"id" => "3",
"fruit" => "Pear",
"color" => "Green"
)
);
foreach($insert_data[0] as $key=>$value) {
if(!in_array($key, $table_fields)) {
delete_col($insert_data, $key);
}
}
echo "<pre>";
print_r($insert_data);
echo "</pre>";
?>
This assumes that the first record will have an entry for every column. I do have some where that's not true, but so far it hasn't caused problems, but I will probably end up rewriting this to go through each row.
I have a html form with checkboxes. Someone selects one or more checkboxes and hit the delete button then it will delete the files references out of the database and delete the files out of Amazon S3. This is the code I used to find all the checkboxes
$checkbox_select = JRequest::getVar('checkboxselect', '', 'POST'); //just a Joomla way of doing a $_POST with extra security
var_dump($checkbox_select); //this returns: array(2) { ["video_1.mp4"]=> string(2) "on" ["video_2.mp4"]=> string(2) "on" ["video_3.mp4"]=> string(2) "on"}
// Localize and sanitize each individual value
foreach (array_keys($checkbox_select) as $element) {
$deleteNames[] = $db->quote($element);
}
var_dump($deleteNames); //array(3) { [0]=> string(13) "'video_3.mp4'" [1]=> string(13) "'video_2.mp4'" [2]=> string(13) "'video_1.mp4'" }
My problem is with Amazon S3 and multiple file deletion. The format I need to put S3 deletion in is quite confusing:
$s3->delete_objects('mybucket', array(
'objects' => array( // accepts a *list* of one or more *hashes*
// a *hash* that contains a "key" key with a value, and maybe a "version_id" key with a value
array('key' => 'object (file) name'),
// a second hash representing a file
// a third hash representing a file
// and so on...
),
));
As far as I understand (from S3 delete_objects function) the final associated array has key as the actual key value. With the last var_dump I've got all video names in an array now I just need to convert that array to a bunch of arrays in this format:
array ('key' => 'video_1.mp4'),
array ('key' => 'video_2.mp4'),
array ('key' => 'video_3.mp4'),
...and so on
How can I create these arrays? Should I be using the first var_dump I have or the second (they both have the video file names listed)? Thanks in advance.
You can use foreach to loop over your array and create a new array in the desired format.
Example:
<?php
foreach (array_keys($checkbox_select) as $element) {
$deleteNames[] = array('key' => $db->quote($element));
}
Untested, may contain errors
have a look at this post in the php documentation for array_push(). You'll get a brief idea of how to achieve it.
I registered an object an I'm trying to get the following below:
stdClass Object (
[test] => test
[users] => stdClass Object (
[createSave_email_subject] => - New
User Account
[createSave_email_pass] => The user
was created, and an email was sent to
them!
) )
Smarty Code:
Works:
{language->test}
Doesn't Work:
{language->users->createSave_email_subject}
{language[users]->createSave_email_subject}
{language.users->createSave_email_subject}
{language->users.createSave_email_subject}
{language->users[createSave_email_subject]}
I built a test case for this using this code:
$test = json_decode('{"test":"test","users":{"createSave_email_subject":"new user account","createSave_email_pass":"The user was created, and an email was sent to them!"}}');
$smarty->assign('testing',$test);
$test is this when var_dumped
object(stdClass)#8 (2) {
["test"]=>
string(4) "test"
["users"]=>
object(stdClass)#7 (2) {
["createSave_email_subject"]=>
string(16) "new user account"
["createSave_email_pass"]=>
string(52) "The user was created, and an email was sent to them!"
}
}
In the tpl I placed
{$testing->users->createSave_email_subject}
And it worked without issue. This was using Smarty 2.6.23
You're missing the dollar signs, you should be using
{$language->test}
Not sure if that's the problem though, as I didn't think smarty would output anything with the syntax you gave. Your test case isn't very reliable either, it's safer to use something like
array('test' => 'worked');
where the key and value are different. With your test, smarty could be printing the key and you wouldn't know the difference.
Use debug_print_var to help identify your problem. e.g.
$language: {$language|#debug_print_var}
users: {$language->users|#debug_print_var}
cse_subject: {$language->users->createSave_email_subject|#debug_print_var}
I am trying to perform a Twitter search using the PEAR package Services_Twitter.
Unfortunately this only returns an array of status ids, for example (var_dump):
object(stdClass)#88 (2) {
["statuses"]=>
array(11) {
[0]=>
int(49497593539)
[1]=>
int(49497593851)
[2]=>
int(49497598001)
[3]=>
int(49497599055)
[4]=>
int(49497599597)
[5]=>
int(49497600733)
[6]=>
int(49497602607)
[7]=>
int(49497607031)
[8]=>
int(49497607453)
[9]=>
int(49497609577)
[10]=>
int(49497610605)
}
["created_in"]=>
float(0.008847)
}
The script I'm using is similar to this test script I wrote:
<?php
//$oAuth = new HTTP_OAuth_Consumer( /** Supply oAuth details here **/ );
$Twitter = new Services_Twitter();
//$Twitter->setOAuth($oAuth);
try {
$Response = $Twitter->search(array(
"q" => "#FF -RT OR #FollowFriday -RT",
"rpp" => 10,
"since_id" => 23982086000,
"result_type" => "recent"
));
var_dump($Response);
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
}
?>
Since I want to scan the tweets for certain words and want to know when it was posted and by whom, I would need to request all these statuses one by one.
But according to the example response in the Twitter API documentation they already return all the necessary information about the tweets (which is kinda obvious).
So, the question is: How can I access this information using Services_Twitter?
Kind Regards,
Arno
So as I said ->search() is wrapped through Services_Twitter::__call().
But here's the mis-understanding!
Two searches:
http://api.twitter.com/1/search.json?q=#noradio
http://search.twitter.com/search.json?q=#noradio
This is confusing as search.twitter.com returns the results as you'd expect them and the other API method just the status IDs.
For some reason only when you search for trends search.twitter.com is used. Otherwise it's the API methods. If you want to help, please open a ticket on PEAR and I can try to implement this for you.
A quickfix for you is this script:
<?php
$uri = 'http://search.twitter.com/search.json?';
$uri .= http_build_query(
array(
"q" => "#FF -RT OR #FollowFriday -RT",
"rpp" => 10,
"since_id" => 23982086000,
"result_type" => "recent"
));
$response = file_get_contents($uri);
if ($response === false) {
fwrite(STDERR, "Could not fetch search result.");
exit(1);
}
$data = json_decode($response);
var_dump($data);
Are you using a custom Services_Twitter, I just did a search through the class via Pear Documentation and was unable to find the search function. However, it seems like most of the returns for that class is a simple_xml object. Given that I would look through the documentation there and see how you can pull that data out. It would also help looking at how Twitter returns the response in XML format.