-1

I don't know how this code is running well.

$on_count=0;
$on_users=array();

foreach ($res as $t_res) {
  $dteStart = new DateTime("now");
  $dteEnd   = new DateTime($t_res["last_seen"]);
  $dteDiff  = $dteStart->diff($dteEnd);
  $y=$dteDiff->format("%Y");
  $m=$dteDiff->format("%m");
  $d=$dteDiff->format("%d");
  $H=$dteDiff->format("%H");
  $i=$dteDiff->format("%i");
  $s=$dteDiff->format("%s");
  $in_sec_res=$y*12*30*24*60*60+$m*30*24*60*60+$d*24*60*60+$H*60*60+$i*60+$s."\n";
  $in_sec_cond=3*60;
  if ($in_sec_res<$in_sec_cond) {
    $on_count=$on_count+1;
    $i=0;
    foreach($t_res as $t_ress){
      if ($i==2) {
        $on_users[$on_count]=$t_ress;
        //echo $on_names[$on_count]."**\n";
      }
      $i++;
    }
  }
}

I want an explanation (inner foreach()). Here,

last_seen

is a random DateTime.

Or is there any other way to collect all

$t_res['name']

in an array?

Bla Bla
  • 49
  • 1
  • 10
  • last_seen is _actually_ random? that doesnt make much sense. if this is code that exists inside the company you work for you should consider smacking the developer with a rolled-up newspaper. – castis May 25 '18 at 20:30
  • 1
    This code is very *odd*. – tadman May 25 '18 at 20:31
  • `last_seen` is from Database. – Bla Bla May 25 '18 at 20:31
  • @castis ha ha ha – Bla Bla May 25 '18 at 20:33
  • @BlaBla `last_seen` is the key to `$t_res` object. You are getting data from the database and creating date out of it. The data is returned as key-value pairs from the SQL query and `last_seen` is a key. – Maihan Nijat May 25 '18 at 20:33
  • @tadman, I know. But as a beginner, I need an explanation – Bla Bla May 25 '18 at 20:34
  • @MaihanNijat. Ok, but I want to collect all `$t_res['name']` from DataBase to an array. Here its working well. But if there is any other way to collect names from database to an array and echo/print it. – Bla Bla May 25 '18 at 20:39
  • I think you should figure out what this code is supposed to do, and a more minimal, understandable solution can emerge. For us to help you'll need to supply examples of what data this is operating on. A complete, self-contained example is best, something we can run `php test.php` on and get good output from. This code is unnecessarily complex, but it's not clear what can be removed because it's not clear what input it takes. – tadman May 25 '18 at 20:40
  • In my DB, 3 columns `id` `name` `last_seen`. I want to count online members and show online members name. I think, I can't explain more than that, as I said I'm a beginner – Bla Bla May 25 '18 at 20:47

1 Answers1

1

The inner foreach loop is very odd. It's basically just doing the same thing as:

$on_users[$on_count] = $t_res['name'];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • But replacing `$i=0; foreach($t_res as $t_ress){ if ($i==2) { $on_users[$on_count]=$t_ress; //echo $on_names[$on_count]."**\n"; } $i++; }` by `$on_users[$on_count] = $t_res[2];` showing `Notice: Undefined offset: 2 in C:\xampp\htdocs\....\header.php on line 35` error. – Bla Bla May 25 '18 at 20:43
  • I've added an `if` statement that checks if there are enough elements. – Barmar May 25 '18 at 20:49
  • Can you post the output of `var_dump($t_res);`? – Barmar May 25 '18 at 20:52
  • But there `(count($t_res) >= 3)` is `true`. – Bla Bla May 25 '18 at 20:52
  • Is `$t_res` an associative array? – Barmar May 25 '18 at 20:53
  • something like : `array(12) { ["id"]=> string(1) "8" ["last_seen"]=> string(19) "2018-05-26 00:18:27" ["name"]=> string(12) "Afzal Sabbir" ["regno"]=> string(10) "2012133105" ["user_name"]=> string(11) "afzalsabbir" ["user_password"]=> string(32) "#########" ["user_email"]=> string(18) "afzalbd2@gmail.com" ["user_photo"]=> string(0) "" ["user_versity"]=> string(1) "0" ["dept_name"]=> string(0) "" ["verify_code"]=> string(7) "26af9ab" ["verified"]=> string(1) "1" } array(12) { ["id"]=> string(1) "9" ["last_seen"]=> string(19) "2018-05-25 23:51:20" ["name"]=> ` – Bla Bla May 25 '18 at 20:54
  • Updated the answer – Barmar May 25 '18 at 20:56
  • No. `$t_res` is not an associative array – Bla Bla May 25 '18 at 20:56
  • The array you just posted is an associative array. – Barmar May 25 '18 at 20:57
  • Almost there! `$on_count`=`5`. And `$t_res['name']` must be 5. But 4 `name`s showing. – Bla Bla May 25 '18 at 20:59
  • Then maybe I'm not clear about the definition of the associative array, sorry. – Bla Bla May 25 '18 at 21:02
  • An associative array is an array where the keys are strings rather than numerical indexes. Like `id`, `last_seen`, `name`, etc. – Barmar May 25 '18 at 21:02
  • Yes! it is. Thanks – Bla Bla May 25 '18 at 21:03
  • The script uses `$on_count` incorrectly. It's incrementing it before using it as the index, so it skips element `0` in the array. There's no need to use a variable for that, just use `$on_users[]` to append to the array. – Barmar May 25 '18 at 21:04
  • No, 5 persons are online now. But 1st persons name is missing according to DB order. – Bla Bla May 25 '18 at 21:07
  • Maybe their `last_seen` time doesn't fit the condition in your `if` statement. – Barmar May 25 '18 at 21:09
  • See https://stackoverflow.com/questions/5988450/difference-between-2-dates-in-seconds?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa for a much simpler way to get the difference between two times in PHP. – Barmar May 25 '18 at 21:10