Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/29/2020 in all areas

  1. Meanwhile, here's an alternative solution to my previous one, this one without the SQL variables. SELECT SUM(CASE WHEN DATE(datein) > DATE(dateout) THEN DATEDIFF(datein, dateout) - 1 ELSE 0 END ) as tot_absent FROM ( SELECT a.dateout , MIN(b.datein) as datein FROM ajoo_login a LEFT JOIN ajoo_login b ON a.dateout < b.datein GROUP BY a.dateout ) logins; +------------+ | tot_absent | +------------+ | 327 | +------------+
    2 points
  2. OK, I loaded your data into a test table INSERT INTO ajoo_login (datein, dateout) VALUES ('2019-03-30 17:05:24', '2019-03-30 17:09:47'), ('2019-04-01 15:13:32', '2019-04-01 15:19:46'), ('2019-04-04 23:37:21', '2019-04-04 23:50:51'), ('2019-04-18 15:28:35', '2019-04-18 15:33:10'), ('2019-04-23 16:35:20', '2019-04-23 16:42:35'), ('2019-04-24 12:03:07', '2019-04-24 12:10:28'), ('2019-05-01 08:05:48', '2019-05-01 08:20:28'), ('2019-05-08 18:04:04', '2019-05-08 18:14:57'), ('2019-05-09 08:18:15', '2019-05-09 08:29:38'), ('2019-06-18 12:49:01', '2019-06-18 13:10:15'), ('2019-09-05 17:17:33', '2019-09-13 15:24:28'), ('2019-09-28 07:05:03', '2019-09-28 08:12:26'), ('2019-09-28 12:55:56', '2019-09-28 13:21:15'), ('2019-09-28 16:47:52', '2019-10-01 16:28:18'), ('2019-10-03 13:11:44', '2019-12-10 17:56:25'), ('2020-05-22 12:08:32', '2020-08-27 17:21:02'); Running the query gives SELECT SUM(diff) AS tot_absent FROM ( SELECT CASE WHEN DATE(datein) > DATE(@prevout) THEN DATEDIFF(datein, @prevout) - 1 ELSE 0 END AS diff , datein , @prevout := dateout AS dateout -- store dateout in @prevout FROM ajoo_login JOIN (SELECT @prevout := NULL) init -- initialize @prevout ) logins; +------------+ | tot_absent | +------------+ | 327 | +------------+ Running just the subquery portion gives mysql> SELECT -> CASE WHEN DATE(datein) > DATE(@prevout) -> THEN DATEDIFF(datein, @prevout) - 1 -> ELSE 0 -> END AS diff -> , datein -> , @prevout := dateout AS dateout -> FROM ajoo_login -> JOIN (SELECT @prevout := NULL) init; +------+---------------------+---------------------+ | diff | datein | dateout | +------+---------------------+---------------------+ | 0 | 2019-03-30 17:05:24 | 2019-03-30 17:09:47 | | 1 | 2019-04-01 15:13:32 | 2019-04-01 15:19:46 | | 2 | 2019-04-04 23:37:21 | 2019-04-04 23:50:51 | | 13 | 2019-04-18 15:28:35 | 2019-04-18 15:33:10 | | 4 | 2019-04-23 16:35:20 | 2019-04-23 16:42:35 | | 0 | 2019-04-24 12:03:07 | 2019-04-24 12:10:28 | | 6 | 2019-05-01 08:05:48 | 2019-05-01 08:20:28 | | 6 | 2019-05-08 18:04:04 | 2019-05-08 18:14:57 | | 0 | 2019-05-09 08:18:15 | 2019-05-09 08:29:38 | | 39 | 2019-06-18 12:49:01 | 2019-06-18 13:10:15 | | 78 | 2019-09-05 17:17:33 | 2019-09-13 15:24:28 | | 14 | 2019-09-28 07:05:03 | 2019-09-28 08:12:26 | | 0 | 2019-09-28 12:55:56 | 2019-09-28 13:21:15 | | 0 | 2019-09-28 16:47:52 | 2019-10-01 16:28:18 | | 1 | 2019-10-03 13:11:44 | 2019-12-10 17:56:25 | | 163 | 2020-05-22 12:08:32 | 2020-08-27 17:21:02 | +------+---------------------+---------------------+
    2 points
  3. PHP is single-threaded*, so yea each tick callback is run in series and must finish before the next tick callback can be run. Using event-based processing lets you make things that seem like they can multi-task but they don't really. A promise can be used to do work as well, it doesn't have to just wait for a result. Conceptually they are something that is used to just wait for something to finish, but that doesn't mean it has to be some external process. You could create a promise that just does work within PHP, such as that emailer queue in the previous thread. That could be implemented as a promise if one desired. From what I've seen, one of the biggest things people generally don't understand about promises is that they are not something that can just magically wait until another thing is done. They need to periodically check if that other thing is done. Since PHP is single-threaded, they can't just start up a background-thread to do that. Even if they could, the main thread would still need to know not to exit while the promise is pending. What is needed then is a way to hook into the main loop so the promise can do it's work from time to time and this is what functions like futureTick and addPeriodicTimer are for. These functions provide a way for the promise to register a callback with the main loop so that they will occasionally gain control of the main thread and be able to do whatever needs done. For promises that are just waiting on something, that likely is just a simple check to see if the thing is ready yet and nothing more. For something like the email queue, that would be sending out a small batch of emails. If you don't need the extra functionallity of the promise interface, then futureTick / addPeriodicTimer can still be used to just hook into the event loop for whatever reason. That's what the original email queue does, it hooks the loop to allow processing of emails but doesn't go so far as to provide a full promise implementation. As for your task, the react project has a package for running commands that you can use: reactphp/child-process. I think using that would be your best bet to handle your image generation, the shell thing you linked seems unnecessary and I'm not sure if it'd handle running commands in parallel or not. With the child-process package you'd just have to create a small function to execute your process and return a promise. That promise would be resolved when the process emits a exit event. For example: function executeCommand($loop, $command){ $deferred = new React\Promise\Deferred(); $process = new React\ChildProcess\Process($command); $process->on('exit', function ($code) use ($deferred){ if ($code === 0){ $deferred->resolve(); } else { $deferred->reject(); } }); $process->start($loop); return $deferred->promise(); } With that, you can generate all your images in parallel using something like: $promiseList = []; foreach ($imageCommand as $cmd){ $promiseList[] = executeCommand($loop, $cmd); } React\Promise\all($promiseList)->done(function(){ //All images generated, send your emails. }); * pthreads and pht provide some threading capability, but I've never tried using them.
    1 point
  4. Because of the JOIN, it is initialized before it is used. As an alternative to the join you could have two queries ... SELECT @prevout := NULL; -- initialize @prevout SELECT SUM(diff) AS tot_absent FROM ( SELECT CASE WHEN DATE(datein) > DATE(@prevout) THEN DATEDIFF(datein, @prevout) - 1 ELSE 0 END AS diff , datein , @prevout := dateout AS dateout -- store dateout in @prevout FROM ajoo_login ) logins; @vars are just like variables in any other language - you assign values to them (and use those values) as the query processor loops through each of the records
    1 point
  5. Where did that suddenly spring from? There's no mention in your original post. Don't keep us in the dark and still expect help.
    1 point
  6. My version would be to JOIN the table to itself once or twice in order to pair up consecutive records, then DATEDIFF()-1 the end of the first with the start of the second, then SUM the results.
    1 point
  7. Well, this would be one of those times. You can't do simple math like $now - $last with the value returned by microtime(). You need to use microtime(true) for that. For example: <?php $last = microtime(); sleep(5); $now = microtime(); $diff = $now - $last; printf("%0.4f seconds have passed", $diff); One might expect since the script sleeps for 5 seconds to get a result like 5.xxxx seconds have passed but what you actually get is: Notice: A non well formed numeric value encountered in W:\expired.php on line 7 Call Stack: 0.0002 391984 1. {main}() W:\expired.php:0 Notice: A non well formed numeric value encountered in W:\expired.php on line 7 Call Stack: 0.0002 391984 1. {main}() W:\expired.php:0 0.0044 seconds have passed Due to how microtime returns it's result $diff would never be greater than 1, and could potentially be negative.
    1 point
  8. You were close with your original method but it needed a couple of tweaks while fetch { if $type != $t { if $t != '' output </optgroup> // only close previous group if there was one output <optgroup> $t = $type end if output <option> end while output </optgroup> // close final group
    1 point
  9. I have an inflateable baseball bat which I often use on such occasions.
    1 point
  10. What if you echo the query string to examine the syntax? echo "SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'";
    1 point
  11. Telling us what the problem is goes a long way towards getting it resolved.
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.