1

I am trying to generator the report based on login datetime and logout datetime. Here is my code:

while (strtotime($begin_date) <= strtotime($end_date)){
        foreach($us AS $title) { 
        $stmt="SELECT event,event_epoch,event_date,campaign_id,user_group,session_id,server_ip,extension,computer_ip,phone_login,phone_ip from ".$user_table." where user = '$title' and event_date >= '$begin_date $begin_time'  and event_date <= '$begin_date $end_time' order by user_id asc;";
        $rslt=mysql_to_mysqli($stmt, $link);
        $events_to_print = mysqli_num_rows($rslt);
        while ($events_to_print > $o){
                $sel1 = "select full_name from users where user = '$title'";
                $res1 = mysqli_query($link,$sel1);
                $row1 = mysqli_fetch_array($res1);
           if (preg_match("/LOGIN/i", $row[0])){    
                    $date = date('Y-m-d');               
                    $MAIN.="<tr $bgcolor>";
                    $MAIN.="<td><font size=2>$begin_date</td>"; 
                    $MAIN.="<td align=right><font size=2> $row1[0] </td>\n";
                    $MAIN.="<td align=right><font size=2> $title </td>\n";
                    $MAIN.="<td align=right><font size=2> $row[3] </td>\n";
                    $MAIN.="<td align=right><font size=2> $row[2] </td>\n";
                    $val = $row[2];
            } else {
                    $MAIN.="<td><font size=2>$row[2]</td>"; 
                    $MAIN.="<td align=right><font size=2> $row1[2] </td>\n";
                    $MAIN.="<td align=right><font size=2> $row[1]</td>\n";
                    $MAIN.="<td align=right><font size=2> $row1[3] </td>\n";
                    $MAIN.="<td align=right><font size=2> $row12[2] </td>\n";
            }
}
}

In the above code if codition and else codition two are executing both times. result is like this:

Date       AgentName   AgentId  CAMPAIGN LOGINTIME    LOGOUTTIME    COUNT   LASTCALL
2020-04-22  ABC           1        test   2020-04-22  2020-04-22      12    No
                                            14:20:00   14:25:10
2020-04-22  XYZ           3        test12 2020-04-22  2020-04-22      6       No
                                            14:50:00   15:25:10
2020-04-22  ABC           1        test   2020-04-22  2020-04-22      120      NO
                                            19:20:00    19:10:10  
2020-04-22    190      YES
14:20:00

2020-04-22  ABC           1        test   2020-04-22  2020-04-22      Yes       No
                                            14:20:00   14:25:10

In the above code 2020-04-22 14:20:00 190 YES this is belongs to logout but that is display in login place bez there is no login detailsin, in this case what I need to do. Expected output like this:

Date       AgentName   AgentId  CAMPAIGN LOGINTIME    LOGOUTTIME    COUNT   LASTCALL
2020-04-22  ABC           1        test   2020-04-22  2020-04-22      12    No
                                            14:20:00   14:25:10
2020-04-22  XYZ           3        test12 2020-04-22  2020-04-22      6       No
                                            14:50:00   15:25:10
2020-04-22  ABC           1        test   2020-04-22  2020-04-22      120      NO
                                            19:20:00    19:10:10  
                                                       2020-04-22    190      YES
                                                        14:20:00

2020-04-22  ABC           1        test   2020-04-22  2020-04-22      Yes       No
                                           14:20:00   14:25:10

Note :DB data like this:


event eventdate 

LOGIN  2020-04-22
        12:15:00
LOGOUT  2020-04-22
         12:30:10
LOGIN   2020-04-22
        12:15:00
LOGOUT  2020-04-22
        13:15:10
LOGOUT  2020-04-22
        14:15:00
LOGIN  2020-04-22
        15:25:00

halfer
  • 19,824
  • 17
  • 99
  • 186
  • you are vurnurable to sql injection use **Prepoare statements** with oarameters for your where clause. see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Apr 22 '20 at 15:21
  • Thanks @nbk ,sorry i did not get your point and I don't the concept ,is there any different way to do this kindly let me know,thanks. – Murala Sandeep Apr 22 '20 at 15:58
  • If a value is missing use ` ` to keep the columns aligned. – Dave Apr 22 '20 at 16:03
  • @Dave Thanks,here values are not missing ,i am displaying logtime with respective logouttime so max.it is coming perfect but the problem is when ```DB``` data having two times logout then issue is coming ,like i am displaying logtime with respective logouttime some times there is no login details only logout details that should diplay at right but it not . bez in my if condtion i have give ```.....``` it is ending in else condition ,so only else part is executing ,when two time having logout . – Murala Sandeep Apr 22 '20 at 16:15
  • 2
    Between the `` add ` ` as I suggested. – Dave Apr 22 '20 at 16:18
  • @Dave Thanks ,if i add ``` ``` that is affected to login details also at left side – Murala Sandeep Apr 22 '20 at 16:51
  • What heans you check if row[0] is emoty and then add   else take the value – nbk Apr 22 '20 at 19:24
  • if (empty($row1[0])) {  } else {$row1[0];}; – nbk Apr 22 '20 at 19:32
  • and this for all values. – nbk Apr 22 '20 at 19:33
  • @nbk thank you so much for your valuble response ,here problem is i have give if and else condition ,they both are executing like ```f (preg_match("/LOGIN/i", $row[0])) // LOGIN,LOGIN = 1{}``` if condition is executing then ```else{}```condition is executing,if i give the condition like ```if(empty($row1[0]){}``` it is effected to all the logout details – Murala Sandeep Apr 23 '20 at 04:43

0 Answers0