Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    loop not working

    I dont why my code is working wrongly there are records which are printed multiple times with same entrydatetime and exit datetime.

    Code:
    function cpWhitelistStats() {
        $startDate = $_POST['startDate'];
        $startDateTime = "$startDate 00:00:00";
        $endDate = $_POST['endDate'];
        $endDateTime = "$endDate 23:59:59";
        $cpId = $_POST['id'];
        $cpName = etCommonCpNameById($cpId);
    
        print "<h2 style=\"text-align: center;\">Permitted Vehicle Summary</h2>";
        print "<h2 style=\"text-align: center;\">for $cpName</h2>";
        $tmpDate = explode("/", $startDate);
        $startYear = $tmpDate[2];
        $startMonth= $tmpDate[1];
        $startDay = $tmpDate[0];
        $tmpDate = explode("/", $endDate);
        $endYear = $tmpDate[2];
        $endMonth= $tmpDate[1];
        $endDay = $tmpDate[0];
    
        $startDateTime = "$startYear-$startMonth-$startDay 00:00:00";
        $endDateTime = "$endYear-$endMonth-$endDay 23:59:59";
    
        $custId = $_SESSION['customerID'];
        $realCustomerId = $_SESSION['realCustomerId'];
    
        $maxVal = 0;
    
        if ($custId != "") {
            $conn = &newEtConn($custId);
            // Get the whitelist plates
            $staticWhitelistArray = etCommonMkWhitelist($conn, $cpId);
            array_shift($staticWhitelistArray);
    
            $startLoopDate = strtotime($startDateTime);
            $endLoopDate = strtotime($endDateTime);
            $oneDay = 60 * 60 * 24;
    
            // Get the entries
            $plateList = array_keys($staticWhitelistArray);
            $plate_lookup = implode('","', $plateList);
            $sql = "SELECT plate, entry_datetime, exit_datetime FROM stats WHERE plate IN (\"$plate_lookup\") AND entry_datetime > \"$startDateTime\" AND entry_datetime < \"$endDateTime\" AND carpark_id=\"$cpId\" ";
            $result = $conn->Execute($sql);
            if (!$result) {
                print $conn->ErrorMsg();
                exit;
            }
            $rows = $result->fields;
            if ($rows != "") {
                unset($myArray);
                foreach($result as $values) {
                    $plate = $values['plate'];
                    $new_platelist[] = $plate;
                    $inDateTime = $values['entry_datetime'];
                    $outDateTime = $values['exit_datetime'];
                    $tmp = explode(' ', $inDateTime);
                    $inDate = $tmp[0];
                    $in_ts = strtotime($inDateTime);
                    $out_ts = strtotime($outDateTime);
                    $duration = $out_ts - $in_ts;
                    $dur_array = intToDateArray($duration);
                    $dur_string = '';
                    if ($dur_array['days'] > 0) {
                        $dur_string .= $dur_array['days'] . ' days ';
                    }
                    if ($dur_array['hours'] > 0) {
                        $dur_string .= $dur_array['hours'] . ' hours ';
                    }
                    if ($dur_array['mins'] > 0) {
                        $dur_string .= $dur_array['mins'] . ' minutes ';
                    }
                    if ($dur_array['secs'] > 0) {
                        $dur_string .= $dur_array['secs'] . ' secs ';
                    }
                    $myArray[$plate][] = array($inDateTime, $outDateTime, $inDate, $dur_string);
                }
            }
    
            while ($startLoopDate < $endLoopDate) {
                $dayString = strftime("%a, %d %B %Y", $startLoopDate);
                $dayCheck = strftime("%Y-%m-%d", $startLoopDate);
    
                print "<h2>$dayString</h2>";
                print "<table width=\"100%\">";
                print "  <tr>";
                print "    <th>VRM</th>";
                print "    <th>Permit Group</th>";
                print "    <th>Entry Time</th>";
                print "    <th>Exit Time</th>";
                print "    <th>Duration</th>";
                print "  </tr>";
                foreach($new_platelist as $wlPlate) {
                    if ($myArray[$wlPlate][0][2] == $dayCheck) { 
                    print "<tr>";
                    print "<td>$wlPlate</td>";
                    if (isset($myArray[$wlPlate])) {
                        print "<td>".$staticWhitelistArray[$wlPlate]['groupname']."</td>";
                        print "<td>".$myArray[$wlPlate][0][0]."</td>";
                        print "<td>".$myArray[$wlPlate][0][1]."</td>";
                        print "<td>".$myArray[$wlPlate][0][3]."</td>";
                    }
                    else {
                        print "<td>Vehicle Not Seen</td>";
                        print "<td>Vehicle Not Seen</td>";
                        print "<td>Vehicle Not Seen</td>";
                    }
                    print "</tr>";
                    }
                }
                print "</table>";
                $startLoopDate = $startLoopDate + $oneDay;
            }
        }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Don't see this very often:
    PHP Code:
            $conn = &newEtConn($custId); 
    You don't actually need to do this, and I'd recommend against doing it. Since $conn appears to be an object of type PDO, that indicates you are on at least PHP 5.1.0. As of PHP 5.0, the object engine was overhauled and you don't need to manually assign pointers by reference since objects move by standard copy on write context of the object pointer (which points to the pointer to the data itself). When you initiate a by-reference return, that would indicate that you are probably using a static instance of the PDO and returning the reference to it. The problem here is that they are literally the same objects in ever scope, so if you destroy it, you will destroy all instance of the object. By all means, continue to do so if its something you may require doing, but don't do it unless there is an explicit reason to do it (I've seen people do it for some type of optimization, but unlike in C, PHP's default copy-on-write will automatically optimize anyway, so doing so just adds a potential disastrous scenario which may as well be using globalization in exchange for absolutely no net benefit). Still, I'm glad to see the proper use of return by reference, despite being probably undesirable in this context, and yes by-reference "pointer" is definitely different than by-object "pointer" in PHP.

    As for your actual problem, which one of these is giving you the replication? If its the first foreach loop, than that's what your query is returning to you. The second loop shouldn't have anything to do with the data since it appears to be building a calendar range.
    Methinks your problem will be here:
    PHP Code:
                foreach($new_platelist as $wlPlate) {
                    if (
    $myArray[$wlPlate][0][2] == $dayCheck) { 
    But, I cannot tell with what you have whether its a problem with the check on that $myArray, or if the problem is ultimately the construction of $myArray. My suspicion is that its the check on it, and not the $myArray creation.

    PHP Code:
    var_export($myArray); 
    Run that and post what the array structure is. Remove any data that's considered sensitive, the plate I assume is a plate number for a vehicle, so it'd be best to run on mock data for this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    kiranv5 (10-24-2013)

  • #3
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the quick response.
    My data look like this. I have this type of records occuring twice but in the array it occurs only once.

    Code:
    PHP Code:
    AD03WTE    DASHH    01/10/2013 09:12     01/10/2013 17:21    8 hours 8 minutes 8  AD03WTE    DASHH    01/10/2013 09:12     01/10/2013 17:21    8 hours 8 minutes 8 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Before the while loop, add:
    PHP Code:
    $new_platelist array_unique($new_platelist); 
    See if that removes them.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yes, almost there but when I choose date range between 01-10-2013 to 04-10-2013 it picks only once as it should pick on 02-10-2013. as my query returns the plate on 2/10/2013 in mysql.

  • #6
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    SELECT plate, entry_datetime, exit_datetime FROM stats WHERE 
     entry_datetime > '2013-10-01' AND entry_datetime < '2013-10-04'  and plate = 'AD03WTE'
    
    
    Code:
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    AD03WTE	2013-10-02 09:16:19	2013-10-02 14:03:20

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I don't understand, do you have a question hidden in here somewhere?
    Those are clearly not duplicates and fall between the range of Oct 1 and Oct 4 of 2013.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I used to get duplicate records before I have inserted array_unique where the record was appearing 3 times with the same data. but after array_unique for new platelist it appeared only once.I am bit confused now. Arrray is looking fine.
    my code has some error. I dont know what I am doing now. really sorry.

    Code:
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07

  • #9
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kiranv5 View Post
    I used to get duplicate records before I have inserted array_unique where the record was appearing 3 times with the same data. but after array_unique for new platelist it appeared only once.I am bit confused now. Arrray is looking fine.
    my code has some error. I dont know what I am doing now. really sorry.

    Code:
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    AD03WTE	2013-10-01 09:12:59	2013-10-01 17:21:07
    This record is not appearing when I use array_unique for new platelist
    Code:
    AD03WTE	2013-10-02 09:16:19	2013-10-02 14:03:20

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Go back to my first reply then where I asked for a var_export. I need that list to see what is going on with the data.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •