I want to set an ion-toggle according to the value that exists on the db table.
If the value is stored in the table the toggle should be ON, if not OFF. Off (left) On(right)
So, if the artist column in artists_interests table there is a value of Cinematographer then i want the <ion-toggle [(ngModel)]="artistInterests.Cinematographers" (click)="artistInterest(artists, 'Cinematographers')"> to be ON
here is what I have so far.
html
<ion-item>
<ion-label>Cinematographers</ion-label>
<ion-toggle color="success" [(ngModel)]="artistInterests.Cinematographers" (click)="artistInterest(artists, 'Cinematographers')"></ion-toggle>
</ion-item>
<ion-item>
<ion-label>Graphic Designers</ion-label>
<ion-toggle color="gold-white" [(ngModel)]="artistInterests.graphicDesigners" (click)="artistInterest(artists, 'Graphic Designers')"></ion-toggle>
</ion-item>
.ts to call the API function to store value (e.g. Graphic Designers) on DB table - that work I mean it stores 'Graphic Designers' in the artist column in DB table.
artistInterest(artists:string, messageTitle: string)
{
this.userData.artistSettings(this.userDetails.uid, messageTitle).pipe(
map((data: any) => {
if (data.success) {
Swal.fire({
icon: 'success',
title: messageTitle,
showConfirmButton: false,
backdrop: false,
timer: 2500
})
}
})
).subscribe()
}
.ts function to retrieve the data from api function with
export class CreativeSettingsPage implements OnInit {
creativeInterests: any= {};
creative: any= {};
artistInterests: any= {};
artists: any= {};
userDetails: any = {};
constructor(
public userData: UserData
) {
this.userDetails = this.userData.getUserData();
this.creativeInsterestSet();
this.artistsInsterestSet;
}
ngOnInit() {
this.artistsInsterestSet()
}
artistsInsterestSet() {
this.userData.artistsInterests(this.userDetails.uid).pipe(
map((data: any) => {
if (data.success) {
this.artistInterests = data.artistInterests;
console.log( this.artistInterests);
}
})
).subscribe()
}
userData.ts
artistsInterests(uid:number) {
const url = this.appData.getApiUrl() + 'getArtistInterests';
const data = this.jsonToURLEncoded({
uid: uid
});
return this.http.post(url, data, { headers: this.options });
}
php function getArtistInterests
function getArtistInterests()
{
$request = \Slim\Slim::getInstance()->request();
$data = json_decode($request->getBody());
$response['success'] = true; // 1
$uid = $request->post('uid');
$db = getDB();
$sql = "SELECT A.artist, U.profile_pic,U.first_name,U.last_name,
DATE_FORMAT(A.created, '%W %D %M %Y, %l %i %p') as date
FROM artists_interests A
LEFT JOIN users U ON A.uid_fk=U.uid
WHERE A.uid_fk=? GROUP BY A.artist";
$stmt = $db->prepare($sql);
$stmt->execute(array($uid));
$db = null;
$artistInterests = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($artistInterests as $feed) {
$feed->profile_pic = SITE_URL . 'usersPic/' . $feed->profile_pic;
}
$data['success'] = true;
$data['artistInterests'] = $artistInterests;
echo json_encode($data);
}
the value is stored as e.g. Graphic Designer (two words) in artists_interests table so [(ngModel)]="artistInterests.graphicDesigners" will not work. But it should work for [(ngModel)]="artistInterests.Cinematographers" as far as I know. By working I mean the toogle to be ON (right on slide)