Array to string conversion in foreach - arrays

Hi,
I am getting an array for string conversion when using the code below:
$sort_order = array();
foreach (getAll() as $field) {
$sort_order[$field->name] = query("SELECT sort_order
FROM field_table
WHERE field_name = '$field->name'");
$o->tag[$field->name] = $field->title. $sort_order[$field->name];
}
The error says there is an Array to string conversion in line 6. Why is that?
Thank you.

query will return an array, so you need to do something like:
$result = query("SELECT sort_order
FROM field_table
WHERE field_name = '$field->name'");
$row = $result->fetch_assoc();
sort_order[$field->name] = $row['sort_order'];

Yes because the query returns an array, which is directly assigned to the variable. Please try this :
$sort_order = array();
foreach (getAll() as $field) {
$result = query("SELECT sort_order
FROM field_table
WHERE field_name = '$field->name'");
while($res = $result->fetch_array()) {
$sort_order = $res['sort_order'];
}
$o->tag[$field->name] = $field->title.$sort_order;
}

Related

Unable to find if one item exists in array of items and return the necessary message in Perl

I have array of IDs. I have one ID which I want to find if that ID exists in the array of IDs in Perl
I tried the following code:
my $ids = [7,8,9];
my $id = 9;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
} else {
print 'no';
}
}
I get the output as:
nonoyes
Instead I want to get the output as only:
yes
Since ID exists in array of IDs
Can anyone please help ?
Thanks in advance
my $ids = [7,8,9];
my $id = 9;
if (grep $_ == $id, #ids) {
print $id. " is in the array of ids";
} else {
print $id. " is NOT in the array";
}
You just need to remove the else part and break the loop on finding the match:
my $flag = 0;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
$flag = 1;
last;
}
}
if ($flag == 0){
print "no";
}
Another option using hash:
my %hash = map { $_ => 1 } #$ids;
if (exists($hash{$id})){
print "yes";
}else{
print "no";
}
use List::Util qw(any); # core module
my $id = 9;
my $ids = [7,8,9];
my $found_it = any { $_ == $id } #$ids;
print "yes" if $found_it;
The following piece of code should cover your requirements
use strict;
use warnings;
my $ids = [7,8,9];
my $id = 9;
my $flag = 0;
map{ $flag = 1 if $_ == $id } #$ids;
print $flag ? 'yes' : 'no';
NOTE: perhaps my #ids = [7,8,9]; is better way to assign an array to variable

Splatting - Input string was not in a correct format

I can't seem to get my splatting to work in my Invoke-WmiMethod command. I declare the hash table like so:
$HKU = 2147483651
$MyParams = #{
'Class' = 'StdRegProv';
'Name' = 'EnumKey';
'ArgumentList' = "$HKU,''";
'ComputerName' = '';
}
# additional code determining ComputerName... #
$MyParams['ComputerName'] = $MyComputer;
$Vals = Invoke-WmiMethod #MyParams
This line gives me the following error:
Invoke-WmiMethod : Input string was not in a correct format.
At C:\Users\Person\Desktop\tmp.ps1:160 char:20
+ $Vals = Invoke-WmiMethod #MyParams
Do you know what the problem could be?
Try this:
$HKU = 2147483651
$MyParams = #{
'Class' = 'StdRegProv';
'Name' = 'EnumKey';
'ArgumentList' = #($HKU,'');
'ComputerName' = '';
}
$MyParams['ComputerName'] = $MyComputer;
$Vals = Invoke-WmiMethod #MyParams

query with foreach loop

I want to execute this query in a foreach loop to use the table in the elemant orWhere Requette (depending on the number of array elements).
foreach ( $mesDeparts as $mesDepart)
{
$holidays_rh_test = holidaystate::find()
->leftJoin('holidays', '`holidays`.`id` = `holidayState`.`holiday_id`')
->leftJoin('user', '`user`.`id` = `holidays`.`user_id`')
->leftJoin('space_membership', '`user`.`id` = `space_membership`.`user_id`')
// ->where(['holidayState.user_id'=>0, 'holidayState.user_position_id'=>0, 'holidayState.stat'=>'2'])
->where(['space_membership.space_id'=>$mesDepart->space_id])
->andFilterWhere(['or',['holidayState.user_id'=>Yii::$app->user->id,'holidayState.user_position_id'=>$user_position->id ],['holidayState.user_id'=>0, 'holidayState.user_position_id'=>0, 'holidayState.stat'=>'2']])
->orderBy('holiday_id DESC')
->all();
}
You are repeating the query
You should build an array and use in clause
foreach ( $mesDeparts as $key =>$mesDepart) {
$my_array[$key] = $mesDepart->space_id;
}
$holidays_rh_test = holidaystate::find()
->leftJoin('holidays', '`holidays`.`id` = `holidayState`.`holiday_id`')
->leftJoin('user', '`user`.`id` = `holidays`.`user_id`')
->leftJoin('space_membership', '`user`.`id` = `space_membership`.`user_id`')
->andFiilterWhere(['in', 'space_membership.space_id', $my_array])
->orderBy('holiday_id DESC')
->all();

Cloud Datastore API - php - GqlQuery - Binding args

I'm trying to bind some arguments to my GQL query string.
Everything's fine when I run the query without binding anything:
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = 4
LIMIT 1");
But I don't know how to bind some arguments. This is how I'm trying to do so:
function query_entities($query_string, $args=[]){
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString($query_string);
$gql_query->setAllowLiteral(true);
if( !empty($args) ){
$binding_args = [];
foreach ($args as $key => $value) {
$binding_value = new Google_Service_Datastore_Value();
$binding_value->setIntegerValue($value);
$arg = new Google_Service_Datastore_GqlQueryArg();
$arg->setName($key);
$arg->setValue($binding_value);
$binding_args[] = $arg;
}
$gql_query->setNameArgs($binding_args);
}
$req = new Google_Service_Datastore_RunQueryRequest();
$req->setGqlQuery($gql_query);
return $this->dataset->runQuery($this->dataset_id, $req, []);
}
$exampleValue = 4;
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :fieldName LIMIT 1", ["fieldName" => $exampleValue]);
Or:
function query_entities($query_string, $args=[]){
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString($query_string);
$gql_query->setAllowLiteral(true);
if( !empty($args) ){
$binding_args = [];
foreach ($args as $value) {
$binding_value = new Google_Service_Datastore_Value();
$binding_value->setIntegerValue($value);
$arg = new Google_Service_Datastore_GqlQueryArg();
$arg->setValue($binding_value);
$binding_args[] = $arg;
}
$gql_query->setNumberArgs($binding_args);
}
$req = new Google_Service_Datastore_RunQueryRequest();
$req->setGqlQuery($gql_query);
return $this->dataset->runQuery($this->dataset_id, $req, []);
}
$exampleValue = 4;
$result = $DB->query_entities("SELECT * FROM kind WHERE fieldName = :1 LIMIT 1", [$exampleValue]);
This is what I get:
'Error calling POST https://www.googleapis.com/datastore/v1beta2/datasets/age-of-deployment/runQuery: (400) Lexical error at line 1, column 52. Encountered: ":" (58), after : ""' in ...
Cloud Datastore GQL and Python GQL (App Engine) handle argument binding slightly differently.
In this case, you need to use a # instead of a :, for instance:
SELECT * FROM kind WHERE fieldName = #fieldName LIMIT 1
or
SELECT * FROM kind WHERE fieldName = #1 LIMIT 1
Here's the reference documentation for argument binding in Cloud Datastore GQL.

cakephp: not understanding a statement similar to if else

I found the following code in the cupcake forum plugin's forum_app_model.php:
/**
* Validates two inputs against each other
* #access public
* #param array $data
* #param string $confirmField
* #return boolean
*/
public function isMatch($data, $confirmField) {
$data = array_values($data);
$var1 = $data[0];
$var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : '';
//== matches the values. Whereas === matches the values and the data type of the values
//eg
return ($var1 === $var2);
}
Can someone tell me on what is $var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : ''; in the above function? It looks like an if else stmt but i'm not understanding it.
thank you.
That's similar to:
if(isset($this->data[$this->name][$confirmField])) {
$var2 = $this->data[$this->name][$confirmField];
}
else {
$var2 = '';
}
See: Ternary operator php
you can consider this statement as
if(isset($this->data[$this->name][$confirmField]))
{
$var2 = $this->data[$this->name][$confirmField]
}else{
$var2 = '';
}
thanks

Resources