search for multiple keywords in the same field in Yii2 - yii2

I need to search several key words that appear in the same database field. For example if field contains "The quick brown fox jumps over the lazy dog" and someone searches for "quick" and "dog" this should be returned as a match.
so I take the search field and explode it into an array based on spaces:
$terms = explode( " ", $this->search_term );
Then I thought I'd throw this into a loop:
foreach ($terms as $key) {
$query->andFilterWhere( [
'or',
[ 'like', 'item.name', $key ],
] );
}
However this isn't working, plus it's not very elegant.

You should simply build properly the condition parameter, e.g. :
$condition = ['or'];
foreach ($terms as $key) {
$condition[] = ['like', 'item.name', $key];
}
$query->andWhere($condition);

I think you have to join the conditions by using or conditions.
foreach ($terms as $key) {
$query->orFilterWhere( [
'or',
[ 'like', 'item.name', $key ],
] );
}
This is how you have to add another or conditions. See here for further details.
I guess there is no other ways to do this without doing for loop.

Related

Perl: hash from import JSON data, Dumper Outputs right data, However I can not access it

I have the following data in .json; actual values substituted.
{ "Mercury": [
{
"Long": "0.xxxxxx",
"LongP": "0.xxxxx",
"Eccent": "0.xxxx",
"Semi": "0.xxxx",
"Inclin": "0.xxxx",
"ascnode": "0.xx.xxxx",
"adia": "0.xxx",
"visual": "-0.xx"
}
]
}
This works fine:
my %data = ();
my $json = JSON->new();
my $data = $json->decode($json_text);
my $planet = "Mercury";
print Dumper $data; # prints:
This is all fine:
$VAR1 = {
'Mercury' => [
{
'Inclin' => '7.',
'Semi' => '0.8',
'adia' => '6.7',
'LongP' => '77.29',
'visual' => '-0.00',
'Long' => '60.000',
'Eccent' => '0.0000',
'ascnode' => '48.0000'
}
]
};
However when I try to access the hash:
my $var = $data{$planet}{Long};
I get empty values, why?
Problem 1
$data{$planet} accesses hash %data, but you populated scalar $data.
You want $data->{$planet} instead of $data{$planet}.
Always use use strict; use warnings;. It would have caught this error.
Problem 2
$data->{$planet} returns a reference to an array.
You want $data->{$planet}[0]{Long} (first element) or $data->{$planet}[-1]{Long} (last element) instead of $data->{$planet}{Long}. Maybe. An array suggests the number of elements isn't always going to be one, so you might want a loop.

json search using laravel query builder issues

["php", "css", "Mysql"]
["html", "css", "js"]
["js","css"]
This is value for 'keywords' field in 3 records
I need search result for this
["php","css"]
ie, result should contain the above 3 record as there is 'php' in 1st record and 'css' in other 2 records
SELECT * FROMjob_postsWHERE JSON_CONTAINS(keywords, '["php","css"]')
only giving the 1st record
array:2 [
0 => "php"
1 => "css"
]
Made use of array instead of json_encoded format($request['keywords] itself)
DB::table('job_posts')
->when($keywords, function ($query) use ( $keywords) {
foreach ($keywords as $key => $value) {
$query->orWhere('keywords', 'LIKE', '%' . $value . '%');
}
}
->get();

Getting key value pair array in Yii2

$data = User::find()
->select('id, name')
->where(['status' => 'active'])
->orderBy('id DESC')
->asArray()
->all();
[
[0]=>[
id=>1
name="test"
]
[1]=>[
id=>2
name="test1"
]
]
What I want is array which looks similar to this. Mapping the id with name so it can be accessed and checked.
[
[1]=>'test'
[2]=>'test1'
]
Instead of using the ArrayHelper you can directly achieve the desired output by using indexBy() and column() within your query:
$data = User::find()
->select(['name', 'id'])
->where(['status' => 'active'])
->orderBy(['id' => SORT_DESC])
->indexBy('id')
->column();
indexBy() defines the array key, while column() will take the first column in the select condition as value.
Try this
Add the below namespace and use the arrayhelper of Yii2 to map
use yii\helpers\ArrayHelper
$userdata = ArrayHelper::map($data, 'id', 'name');

How to parse JSON/REST data from server using Perl

Im trying to parse out JSON output from the server using Perl. The connection and downloading of the REST data is ok, I just need help parsing the returned data. Here is the snippet for my code:
my $response = HTTP::Tiny->new->get($SERVER_ADDR);
if ($response->{success})
{
my $html = $response->{content};
#LINES = split /\n/, $html;
chomp(#LINES);
print("Lines: '#LINES'\n"); # ZZZ
my $decoded_json = decode_json( $html );
print Dumper $decoded_json;
}
else
{
print "Failed: $response->{status} $response->{reasons}";
}
And here is the results:
Lines: '{"players":[{"currentlyOnline":false,"timePlayed":160317,"name":"MarisaG","lastPlayed":1474208741470}]}'
$VAR1 = {
'players' => [
{
'currentlyOnline' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'timePlayed' => 160317,
'lastPlayed' => '1474208741470',
'name' => 'MarisaG'
}
]
};
There will be multiple entries under the "players" for each player logged in right now. Any tips?
I'm not really sure what you're asking. You have successfully parsed the JSON by by calling decode_json(). You now have a data structure in $decoded_json. Your call to Dumper() shows the structure of that data. It's a hash reference with a single key, players. The value associated with that key is an array reference. Each element in the referenced array is another hash.
So, for example, you could print all of the players' names with code like this.
foreach (#{ $decoded_json->{players} }) {
say $_->{name};
}

Extract values from "media" array

I made and fql query and saved it into an array
$resultposts = $facebook->api(array('method' => 'fql.query',
'query' => $fqlQueryposts));
To extract the name value I use this:
echo $resultposts['first_name'];
But I have problems with "media" array, that it's into "attachment" array. This is the structure: $resultposts>attachment>media>
I should extract "type", "src" and "href" values from "media" array.
I tried in this way:
$resultposts['attachment']['media']['type'];
But it doesn't work. The error is "undefined index: type".
What can I do? Thanks
This post is a little old but as I stumbled upon it while searching for the very same question and found the answer I'll share it for others.
Using PHP SDK (3.11)
$attachment = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT attachment FROM stream WHERE post_id = "'.$poid.'"'));
foreach($attachment as $attach)
{
foreach($attach as $i => $o)
{
echo $o['name'];
foreach($o['media'] as $t => $y)
{
echo $y['type'];
}
}
}