Cluster large dataset into groups of 500/700 - php

Hi everyone! I'm working with a large tab delimited file which contains counts and lengths. I want to display this large data set in a HighCharts, but the count can go up to 100k. Trying to display this will be a lost cause.. so I want to cluster the data in sets of 500/700! An example of my data:
35 1265
36 1310
37 1180
38 1064
39 938
40 906
41 821
42 903
43 845
44 816
45 815
46 853
47 858
.......
72721 1
72732 1
72878 1
72984 1
73138 1
73279 1
73283 1
73379 1
74322 1
74373 1
75038 1
76222 1
76606 1
77153 1
78573 1
80839 1
I have tried to find an example of a bar/column chart in HighCharts that can work with this, but no luck yet. It is a part of a Laravel website, so my best option (I thought) is altering the data in PHP before using it in HighCharts.
I want to have a loop that fills an array with the sum of all the second values between count 0 and 500, and then another array filled with the sum of all the data from 501-1000.. I have tried searching on the internet, but I must be google'ing wrong. Does someone have any suggestions on how I could handle this?
I want the output to look something like this:
0-500 23520
501-1000 235216
1001-1500 235138

Related

Communicate with child process bidirectionally over STDIO

I am trying to communicate with the Stockfish chess engine from a PHP script. For that I am using the UCI protocol, so I will need to send commands line by line. For example, this is what I would normally enter on the Windows Command Prompt:
Stockfish.exe
position startpos
go depth 16
Stockfish.exe is the 64-bit version of the Stockfish chess engine.
I failed to do it using exec(). Here is how I attempted it:
exec("stockfish.exe\nposition startpos\ngo depth 16");
The engine runs, but the commands are not executed, so I get:
Stockfish 10 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott
where I should get something like:
info depth 1 seldepth 1 multipv 1 score cp 116 nodes 20 nps 10000 tbhits 0 time 2 pv e2e4
info depth 2 seldepth 2 multipv 1 score cp 112 nodes 54 nps 27000 tbhits 0 time 2 pv e2e4 b7b6
info depth 3 seldepth 3 multipv 1 score cp 148 nodes 136 nps 45333 tbhits 0 time 3 pv d2d4 d7d6 e2e4
info depth 4 seldepth 4 multipv 1 score cp 137 nodes 247 nps 61750 tbhits 0 time 4 pv d2d4 e7e6 e2e4 c7c6
bestmove d2d4 ponder e7e6
I've read many threads on related issues, but couldn't find a workaround. Is there any way to accomplish this?

Highlighting MySQL Data Results

I'm going to struggle to explain this.. so please feel free to ask for more information.
I'm reading data from a MySQL DB Using :
SELECT * FROM resource where date > DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY `date` DESC
This produces results like this :
ID DATE STACK REF NAME LOCATION CREW
9 2016-01-01 06:34:50 A6YH75F 12 Local List SPAIN 1A
8 2016-01-01 06:34:48 QWE343 23 POINT ONE GERMANY 3A
7 2016-01-01 06:34:46 WER342 43 Insite UK 4A
6 2016-01-01 06:34:44 WFF5G1 34 LANWise FRANCE 5A
5 2016-01-01 06:34:42 2D3D35 21 Polent USA 7A
4 2016-01-01 06:34:40 8541FW 33 Rosta UK 4B
3 2015-12-30 16:48:23 A6YH75F 12 Local List SPAIN 2A
2 2015-11-21 14:32:01 FFCWF4 34 LANWISE FRANCE 6A
1 2015-10-14 11:02:22 POI8H6 75 BALAND IRELAND 6B
This is where it's hard to explain.
I'm looping through the results and echoing out the results into a <table>
When this is live there may appear hundreds of entries for each NAME, what I want to do it highlight output based on some specific criteria.
For example:
If a NAME appears multiple times, compare the latest and previous values of STACK & REF for that name. (so comparing the last two entries per name)
If they are different highlight the output for that entry.
ie:
I can see Local List appears twice in the list. The first date stamp is 2016-01-01 06:34:50 & then at 2015-12-30 16:48:23
Thats fine. On both occasions the STACK and REF are the same.
However LANWISE also appears twice but it's STACK is different on the newer occasion. How do I flag this and highlight the LATEST Entry for LANWISE.
I'm also looking to see if an entry hasn't appeared in the last 65 days.
So looking at the list BALAND last appeared 2015-10-14 11:02:22 so this is greater than 65 days and they haven't appeared since. How do I highlight this ?
Thanks
You can use the following query that returns all of the information required in order to determine records that need to be highlighted:
SELECT ID, `DATE`, STACK, REF, NAME, LOCATION, CREW,
--Calculate difference between `DATE` field and current date
DATEDIFF(NOW(), `DATE`) AS DaysDiff,
-- Get the count of newer records having the same NAME
COALESCE((SELECT COUNT(*)
FROM resource AS r2
WHERE r2.NAME = r1.NAME AND r2.`Date` > r1.`Date`), 0) CountNewer,
-- Find wether an older record exists having the same NAME and
-- different REF or STACK or both
COALESCE((SELECT 1
FROM resource AS r3
WHERE r3.NAME = r1.NAME AND
r3.`Date` < r1.`Date` AND
(r3.REF <> r1.REF OR r3.STACK <> r1.STACK)), 0) IsHighlighted
FROM resource AS r1
WHERE date > DATE_SUB(now(), INTERVAL 12 MONTH)
ORDER BY `date` DESC
I don't know if it's the best way, but I do something like that ($variable is the result of your query)
$already = array();
#The loop where you make your table. I use foreach but use your current loop (probably while if you fetch a query response) :
foreach ($variable as $key => $value) {
$class='';
if(isset($already[$value['name']]) && $already[$value['name']] == $value['date']) {
$class='highlight';
}
#The next time, the variable exist :
$already[$value['name']] = $value['date'];
#do your stuf and add $class to your class in your HTML (and do something inyour CSS)
}
With the isset($already[$value['name']]) you know if the line existing before. And with the == comparator, you know if it's the same value.
Use CSS and class HTML attribute for Highlighting the concerned lines.
Adapt this code for your usage, it's just an example based of your post.
For compare date, you can use DateTime. Example of use :
$datetime1 = new DateTime($xDateDebut[2].'-'.$xDateDebut[1].'-'.$xDateDebut[0]);
$datetime3 = new DateTime(date('Y').'-'.date('m').'-'.date('d'));
$delaisAvantJourJ = $datetime1->diff($datetime3);
($xDateDebut is an explosed SQL date field)
$delaisAvantJourJ is now an object with a lot of usefull information (like the number of day diff)

mySQL reformat query results into pivot table

May I ask for a little help with the following please ?
My query results are returned like ...
cDate posId val
14/01/2011 0 200
14/01/2011 1 199
…
14/01/2011 8 198
14/01/2011 9 197
06/03/2015 0 264
06/03/2015 1 264
…
06/03/2015 8 261
06/03/2015 9 264
Is it possible to 'reformat' the output like this ...
Position 14/01/2011 06/03/2015
0 200 264
1 199 264
2 195 264
3 192 264
4 190 264
5 195 263
6 197 261
7 199 260
8 198 261
9 197 264
I'd like to read through these rows in php and put together a csv file that I can download.
I think this Crosstabs section at artfulsoftware.com may contain the answer for me, but I'm just not 'getting it' right now.
May I ask you for a pointer or two please ? Am I heading in the right direction with Pivot Tables ?
Thank you.

PHP arrays and order sorting

I have to create delivery routes for shipping orders for different clients all over the country. The routes are fixed, meaning that the delivery points inside each route are arranged in a specific order, and the mileage between points in route are defined in a matrix of the route. So, every point in route has the number of km between every point from the route
So, I have the route 'Route_A' defined with the points:
Route_A = ['Point_1','Point_2','Point_3','Point_4', 'Point_5']
The matrix form is something like this:
Warehouse Point_1 Point_2 Point_3 Point_4 Point_5
Warehouse 0 10 20 30 40 50
Point_1 10 0 15 25 35 45
Point_2 20 15 0 5 20 70
Point_3 30 25 5 0 50 25
Point_4 40 35 20 50 0 10
Point_5 50 45 70 25 10 0
The planner selects points from route (not always a full route) and I want the system to be able to calculate the total distance for given route
So, I could have a delivery route (with points from route above) like this (the order of the route must also be rearranged to look like 'Route_A'):
delivery_route = ['Point_3', 'Point_2', 'Point_5', ,'Point_3', 'Point_2','Point_5','Point_1']
I would like to know if this could be done easily without having to write a messy php code
You could do something simple like this:
$delivery_length = 0;
for ($i = 1, $route_len = count($delivery_route); $i < $route_len; $i++) {
$delivery_length += $distance_matrix[$delivery_route[$i-1]][$delivery_route[$i]];
}
It iterates the list of locations in the route and adds up the distance from the previous stop to the current one.

Count array counts 1 too many

Does anyone know how to get arround the annoying problem that when counting how many values there is inside an array if the value is 0 it says 1 becuase it counts the name or something. So like this:
0 : 1
1 : 1
2 : 2
3 : 3
4 : 4
5 : 5
6 : 6
7 : 7
8 : 8
To fully answer the question, I'd need the code for this.
My sneaking suspicion is that whatever you are count()ing isn't an empty array the first time, but something else. An initialized, non-null, non-array and non-Countable-object variable has a count() of 1.
See count's documentation for more info.

Categories