Query get data from database change to array - php

I want to make a chart, I have database province and I want to make this be xAxis on Highcharts.
My Database
<?= \dosamigos\highcharts\HighCharts::widget([
'clientOptions' => [
'chart' => [
'type' => 'spline'
],
'title' => [
'text' => 'Fruit Consumption'
],
'xAxis' => [
'categories' => [
'Jabodetabek',
'Banten',
'Jawa Tengah',
'Jawa Timur',
'Medan',
'Riau',
'Bangka Belitung',
'Lampung',
'Kalimantan Selatan',
'Kalimantan Barat',
'Kalimantan Timur',
'Kalimantan Tengah',
'NTB',
'NTT',
'Papua']
],
'yAxis' => [
'title' => [
'text' => 'Fruit eaten'
]
],
'plotOptions' => [
'line' => [
'dataLabels' => [
'enabled'=> true
]
]
],
'series' => [
['data' => [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
],
'responsive' => [
'rules' => [[
'condition' => [
'maxWidth' => 500
],
'charOptions' => [
'legend' => [
'align' => 'center',
'verticalAlign' => 'bottom',
'layout' => 'horizontal'
],
'yAxis' => [
'labels' => [
'align' => 'left',
'x' => 0,
'y' => 0
],
'title' => [
'text' => null
]
]
]
]]
]
]
]);
How can I make the contents of the province table be an array? I want data count all group by provinces.
Please help me

The yii2-friendly method is to use gii to generate models for each of your tables. Then you can get an array like
$provinces = \app\models\Province::find()->select('name')->asArray();
You can use a similar technique to get the count; I'd post an example except I'm not sure what the correct sql would be for your case.

Related

Why can't I use my analyzer and get answer 'failed to find analyze'?

I made my index with analyzer like in documentation (there).
This is my index create:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'index_analyzer' => [
'my_index_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'mynGram2'
],
],
],
'search_analyzer' => [
'my_search_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'standard',
'lowercase',
'mynGram2'
],
],
],
'filter' => [
'mynGram2' => [
'type' => 'nGram',
'min_gram' => 2,
'max_gram' => 20,
],
],
],
'max_ngram_diff' => 50,
],
],
];
$x = $this->obj->indices()->create($params);
Then i try use my analyzer:
$params = [
'index' => 'mytestindex',
'body' => [
'analyzer' => 'my_search_analyzer',
'text' => 'текст проверить чтобы'
],
];
$x = $this->obj->indices()->analyze($params);
But I get this message:
'{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[PEREGOVOR2][127.0.0.1:9300][indices:admin/analyze[s]]"}],"type":"illegal_argument_exception","reason":"failed
to find analyzer [my_search_analyzer]"},"status":400}'
So... what am I doing wrong? Why can't I use my analyzer and get answer 'failed to find analyze'?
You're not building your analyzer correctly. You only need one analyzer section in your settings:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [ <--- change this
'my_index_analyzer' => [
'type' => 'custom',
"tokenizer" => "standard",
'filter' => [
"lowercase",
"mynGram2"
],
],
'my_search_analyzer' => [
"type" => "custom",
"tokenizer" => "standard",
'filter' => [
"standard",
"lowercase",
"mynGram2"
],
],
],
'filter' => [
'mynGram2' => [
"type" => "nGram",
"min_gram" => 2,
"max_gram" => 20,
],
],
],
"max_ngram_diff" => "50",
],
],
];
$x = $this->obj->indices()->create($params);

Yii2 : Flot charts extension labelFormatter usage

I'm trying to utilise the Yii2 Flot extension with more or less success. However, I'm having trouble altering the labels of a pie-chart using a labelFormatter function.
This is the extension I'm using: bburim/flot
Here is the code I have so far, it generates a nice chart, but I cannot alter the labels.
Any help is appreciated.
echo Chart::widget(
[
'data' => [
['label' => 'Series1', 'data' => [1, 12]],
['label' => 'Series2', 'data' => [1, 16]],
['label' => 'Series3', 'data' => [1, 89]],
['label' => 'Series4', 'data' => [1, 44]],
['label' => 'Series5', 'data' => [1, 38]],
],
'options' => [
'series' => [
'pie' => [
'show' => true,
'label' => [
'show' => true,
'treshold' => 0.1,
'radius' => 0.6,
'value' => 'value',
],
],
],
'grid' => [
'hoverable' => true,
],
'legend' => [
'position' => 'nw',
'show' => true,
'font' => [
'size' => 16,
],
'margin' => 10,
'backgroundOpacity' => 0.5,
],
],
'plugins' => [
Plugin::PIE,
],
'htmlOptions' => [
'class' => 'chartdiv',
],
]
);
That is a pretty broad term to say customize, I will assume it means providing custom label text, its pretty simple and straight forward you just need to use the new JsExpression() for the callback function to work correctly
echo Chart::widget(
[
'data' => [
['label' => 'Series1', 'data' => [1, 12]],
['label' => 'Series2', 'data' => [1, 16]],
['label' => 'Series3', 'data' => [1, 89]],
['label' => 'Series4', 'data' => [1, 44]],
['label' => 'Series5', 'data' => [1, 38]],
],
'options' => [
'series' => [
'pie' => [
'show' => true,
'label' => [
'show' => true,
'treshold' => 0.1,
'radius' => 0.6,
'value' => 'value',
],
],
],
'grid' => [
'hoverable' => true,
],
'legend' => [
'labelFormatter' => new JsExpression("function(label, series) {
// series is the series object for the label
return 'Custom Label for '+series.label+'';
}"),
'position' => 'nw',
'show' => true,
'font' => [
'size' => 16,
],
'margin' => 10,
'backgroundOpacity' => 0.5,
],
],
'plugins' => [
Plugin::PIE,
],
'htmlOptions' => [
'class' => 'chartdiv',
],
]
);
On a side note I wont use that repo as the owner seems to be unresponsive since 2015 not a single reply on the issues, you might have to maintain or fix the bugs your self so better fork the repo and use your own customized version

Get value from array have a multirows

I Have this array and i want to get all parent have a balance :
this array is multi rows note that sub-array is not static could be more
Thanks
$array_content = [
'id'=> 4,
'Parent' => [
[
'id' => 54,
'Parent' => [
[
'id' => 324,
'KUI' => 'ABC',
'figure' => 'Tira',
'Parent'=> []
],
[
'id' => 52355,
'lft' => 'LEFT',
'Parent' => [
[
'id' => 4,
'Parent' => [
[
'id' => 234,
'ui' => 'UITed',
'Parent'=> ['Balance'=>450.3]
]
]
],
[
'id' => 76,
'ui' => 'some value',
'Parent'=> []
]
],
]
],
],
[
'id' => 23,
'title' => 'ABC',
'Parent' => [
],
]
]
];
The output that i need to see is this balance by the id =234 from parent:
'Balance'=>450.3
You can also, instead of iterating the array, make it Json and use substr and strpos (or regex) to find the value.
$json = json_encode($array_content);
$pos = strpos($json, "Balance\":")+9; // +9 for Balance": is 9 characters
Echo substr($json, $pos, strpos($json, "}", $pos)-$pos); // 450.3
https://3v4l.org/cojXe
Here i found the answer Thanks everyone :
$array_content = [
'id'=> 4,
'Parent' => [
[
'id' => 54,
'Parent' => [
[
'id' => 324,
'KUI' => 'ABC',
'figure' => 'Tira',
'Parent'=> []
],
[
'id' => 52355,
'lft' => 'LEFT',
'Parent' => [
[
'id' => 4,
'Parent' => [
[
'id' => 234,
'ui' => 'UITed',
'Parent'=> ['Balance'=>450.3]
]
]
],
[
'id' => 76,
'ui' => 'some value',
'Parent'=> []
]
],
]
],
],
[
'id' => 23,
'title' => 'ABC',
'Parent' => [
],
]
]
];
function search($arr,$id)
{
if(gettype($arr) == 'array')
foreach($arr as $key =>$list)
{
if(gettype($list) == 'array'){
if(isset($list['id']))
{
if($list['id'] ==$id)
print_r($list['Parent']);
}
search($list,$id);
}
}
}
foreach($array_content as $key)
{
search($key,234);
}

How do i highlight my result in elasticsearch?

I am not able to highlight my result, which part of my query is wrong?
PHPClient for elasticsearch throws exception on execution.
$query = [
"query" => [
"filtered" => [
"query" => [
"bool" => [
"should" => [
[
'query_string' => [
'fields' => [
'Title.title^4',
'Title.ngrams_front^2',
'Title.ngrams_back'
],
'defaultOperator' => 'or',
'query' => $paramsObj->q
]
],
[
'query_string' => [
'auto_generate_phrase_queries' => 0,
'enable_position_increments' => false,
'fields' => [
'Title.title',
'Address',
'keys'
],
'query' => $paramsObj->q,
'use_dis_max' => false,
'boost' => 2
]
],
[
'fuzzy' => [
'Title.title' => [
'value' => $paramsObj->q,
'boost' => 1,
'min_similarity' => 0.5,
'max_expansions' => 20,
'prefix_length' => 0
]
]
]
]
]
],
"filter" => $filters
]
],
"highlight" => [
"fields" => [
'Title.title' => [ "pre_tags" => "<em>", "post_tags" => "</em>" ]
]
]
];
First i tried highlighting at filtered level, then i googled and found out i need to do at query level at top of filtered level, so i did but still it throws exception.
Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException'
If at all anyone can help, kindly help.
Try something like this:
$query = array(
'query' => array(
'bool' => array(
'should' => array(
'fuzzy' => array(
'name' => array(
'value' => $serachstring,
'boost' => 1,
'min_similarity' => 0.5,
'max_expansions' => 20,
'prefix_length' => 0
),
),
// ...
)
),
),
'highlight' => array(
"pre_tags" => "<em>",
"post_tags" => "</em>",
'fields' => array(
'name' => (object) array()
)
),
);

How to give my own id to the container of High Chart in Yii

I'm using high-chart extension, but I don't know how to give ID to the container, now getting random id's like yw11,yw12 etc., How to give my own ids to the container like graph1, graph2 etc.,
You have two options:
Change widget id container as states in this line.
Set directly id of container:
$this->Widget('ext.highcharts.HighchartsWidget',array(
'options' =>array(
'chart' => array('renderTo' => $id),
'title' => array('text' => 'title'),
'yAxis' => array(
'title' => array('text' => 'y-values'),
)
)
));
set id to highcharts in yii2 :
echo Highcharts::widget([
'id' => 'YOUR-CUSTOM-ID',
'options' => [
'chart' => [
'type' => 'column',
'margin' => [60, 10, 40, 40],
],
'title' => [
'text' => 'Symmetrical Distribution',
'x' => 25
],
'subtitle' => [
'text' => 'Fisher\'s Iris Data: Sepal Width',
'x' => 25,
],
'legend' => [
'enabled' => false,
],
'credits' => [
'enabled' => false,
],
'tooltip' => [
],
'scripts' => [
'highcharts-more', // enables supplementary chart types (gauge, arearange, columnrange, etc.)
'modules/exporting', // adds Exporting button/menu to chart
'themes/grid' // applies global 'grid' theme to all charts
],
'plotOptions' => [
'series' => [
'pointPadding' => 0,
'groupPadding' => 0.5,
'borderColor' => 'rgba(255,255,255,0.5)',
'color' => '#434348',
]
],
'xAxis' => [
'title' => [
'text' => 'Sepal Width (cm)'
]
],
'yAxis' => [
'title' => [
'text' => ''
]
],
'series' => [
[
'name' => 'Distribution',
'data' => [[2.1, 4], [2.3000000000000003, 7], [2.5, 13], [2.7, 23], [2.9000000000000004, 36], [3.1, 24], [3.3000000000000003, 18], [3.5000000000000004, 10], [3.7000000000000006, 9], [3.9000000000000004, 3], [4.1, 2], [4.300000000000001, 1]]
]
]
]
]);

Categories