Beginner in AngularJS
:
I am passing parameters through http.get
to the url (php file). On my php file I have mysql query in which I have to read those parameters in the where clause.
In brief: http://example.com/page.php?dept=some_dept&office=some_office is my page where I have my angular controller and view part. From this I want to get the url string/params and passed to angular-data.php from where I am fecthing the response. However, angular-data.php have a sql query in which I am planning to pass the dynamic values in WHERE clause i.e. the params from angular controller (dept and office) before getting response on my view page (url given above with params)
Below is my code so far:
page.php?dept=some_dept&office=some_office
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$http({
method: 'GET',
url: 'angular-data.php',
data: {
param1: type,
param2: office
}
}).then(function (response) {
$scope.names = response.data.records;
});
});
page.php?dept=some_dept&office=some_office (continue...):
<tr ng-repeat="x in names">
<td>
{{$index + 1}}
</td>
<td>{{ x.Name}}.</td>
<td>{{ x.Office}}</td>
</tr>
angular-data.php where I want params dynamically for WHERE clause:
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "local_server");
$result = $conn->query("SELECT * FROM `table` WHERE `Dept` = " . $_GET['dept'] . " AND `Office` = " . $_GET['office'] . "");
$outp = "";
while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {
$outp .= ",";
}
$outp .= '{"Name":"' . $rs["Name"] . '",';
$outp .= '"Office":"' . $rs["Office"] . '"}';
}
$outp = '{"records":[' . $outp . ']}';
$conn->close();
echo($outp);