Form dates search between dates in JSON file


#1

Hi there,

I have an Ionic app with a form. This form consists of 2 fields, type=“date”. I also have a JSON file with several requests in it, these requests do all have a start- and enddate. I would like to send these two values (dates) to my JSON file which sends back the requests between those dates (and the requests with these dates too).

Form

<ion-view title="Search">
  <ion-header-bar>
    <h1 class="title">Request form</h1>
  </ion-header-bar>
  <ion-content>
    <form ng-submit="searchReportsDates()" ng-controller="searchReportsCtrl">
      <div class="list">
	      	<div class="item item-divider">
			    Search between dates
			</div>
			<label class="item item-input">
			  <span class="input-label">Starts</span>
			  <input type="date" ng-model="searchData.startdate">
			</label>
			<label class="item item-input">
			  <span class="input-label">Ends</span>
			  <input type="date" ng-model="searchData.enddate">
			</label>
			<li class="item">
	          <button class="button button-width-red ion-checkmark"></button>
			</li>
      </div>
    </form>
  </ion-content>
</ion-view>

Controller

.controller('searchReportsCtrl', function($scope, $timeout, $http, $ionicLoading, RequestService) {
 	  
 	  $scope.searchData = {};
   
      $scope.searchReportsDates = function(){
        
        var data = {
        "startdate": $scope.searchData.startdate,
        "enddate": $scope.searchData.enddate
        };
        
    $http.post("http://url/to/searchreports.php", data,{"startdate": $scope.searchData.startdate, "enddate": $scope.searchData.enddate})
        .success(function(data, status, headers, config){
            $scope.requestsBetweenDates = data;
            console.log($scope.requestsBetweenDates); //this should contain the array with the requests
            
            // or maybe like this?
            //$scope.requestsBetweenDates = JSON.stringify(data);
        });
        
        $scope.loadingIndicator = $ionicLoading.show({
		    content: 'Loading Data',
		    animation: 'fade-in',
		    showBackdrop: false,
		    maxWidth: 200,
		    showDelay: 100
	    });
        
        $timeout(function() {
	        $scope.loadingIndicator.hide();
	        $scope.searchData.startdate = "";
	        $scope.searchData.enddate = ""; 
	    }, 1000); 
    };   
})

PHP script

<?php

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = file_get_contents('php://input');
    $vars = json_decode($data);
    $startdate = $vars->startdate;
    $enddate = $vars->enddate;
    
    $sql = "SELECT * FROM Requests, Users WHERE Requests.startdate=? AND Requests.enddate=?";
    $stmt = $conn->prepare($sql);
	$stmt->bindParam(1, $startdate);
	$stmt->bindParam(2, $enddate);
	$stmt->execute();
	$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //array(0){}
	if($result) {

foreach ($result as $row){
    $return[]=array('employeeid'=>$row['employeeid'],
    				'picture'=>$row['picture'],
    				'firstname'=>$row['firstname'],
    				'lastname'=>$row['lastname'],
    				'role'=>$row['role'],
    				'id'=>$row['id'],
    				'startdate'=>$row['startdate'],
    				'enddate'=>$row['enddate'],
    				'type'=>$row['type'],
    				'reason'=>$row['reason'],
    				'notes'=>$row['notes'],
    				'level'=>$row['level']);
}

header('Content-type: application/json');
echo '' . json_encode($return) .'';
return true;

} else {
	return false;
}

$conn = null;

}
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
?>

I’m guessing that i have to convert the strings, which I send to the PHP script, to actual dates… But the dates in the database should also be converted, right?

I think everything should work, except for the PHP script, right?

Thanks in advance,

Jan


#2

Can anyone help me with this please?
Thanks.


#3

Now I found this:

var dateFrom = "02/05/2013"; //this can be my inserted startdate
var dateTo = "02/09/2013"; //this can be my inserted enddate
var dateCheck = "02/07/2013"; //this should loop through all the requests I have and only display the ones that are between the first two dates

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1[2], d1[1]-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], d2[1]-1, d2[0]);
var check = new Date(c[2], c[1]-1, c[0]);

console.log(check > from && check < to)

Can anyone help me please?

Thanks in advance,

Jan


#4

Hi,
are u resolved your problm please
because i have the same structure,i whoud to have a search by date :wink: