I am currently working with angular cli version 8.1.0 and I have a requirement to pass parameters in the URL and retrieve data from a PHP MySQL database. On the PHP side, everything is functioning correctly and the URL looks like this: http://localhost/repos/Sportaz-repo/AdminStore/angular_admin/php/index.php?id=4
However, I am facing challenges in retrieving the data along with the parameter.
approval.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ApiService } from 'src/app/api.service';
@Component({
selector: 'app-approval',
templateUrl: './approval.component.html',
styleUrls: ['./approval.component.css']
})
export class ApprovalComponent implements OnInit {
constructor(private activatedRoute:ActivatedRoute,private apiService:ApiService) { }
id:any;
result:any;
ngOnInit()
{
this.id=this.activatedRoute.snapshot.paramMap.get('id');
console.log(this.id);
this.activatedRoute.queryParamMap.subscribe((queryParams:Params)=>{
let vendorId=queryParams['id'];
this.apiService.getVendorById(vendorId)
.subscribe(data=>{
this.result=data;
});
});
}
}
api.service.ts
import { Injectable, Output,EventEmitter } from '@angular/core';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Users } from './users';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private httpClient : HttpClient) { }\
getVendorById(data)
{
let datas=JSON.stringify({'id':data});
return this.httpClient.get('http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php?id='+datas)
.map(response => response.json() );
}
}
index.php
<?php
$conn=mysqli_connect("localhost","root","root","angdb");
$request=$_SERVER['REQUEST_METHOD'];
$data=array();
switch($request)
{
case 'GET':
response(getData());
break;
default:
#code...
break;
}
function getData()
{
global $conn;
if(@$_GET['id'])
{
@$id=$_GET['id'];
$where="AND id=".$id;
}
else
{
$id=0;
$where="";
}
$query=mysqli_query($conn,"select * from vendor where status='pending' ".$where);
while($row=mysqli_fetch_assoc($query))
{
$data[]=array("id"=>$row['id'],"changeColumn"=>$row['changeColumn'],"type"=>$row['type'],"timestamp"=>$row['timestamp'],"status"=>$row['status'],"name"=>$row['name']);
}
return $data;
}
function response($data)
{
echo json_encode($data);
}
?>
approval.component.html
{{result.name}}