Ionic i cant retriving data from mysql and insert in main page

home.ts
import { Component } from '@angular/core';
import { NavController,LoadingController } from 'ionic-angular';

import {HttpProvider} from '../../providers/http/http';
import 'rxjs/add/operator/map';
import { Http } from '@angular/http';



@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers:[HttpProvider]
})
export class HomePage {
 newsData: any;
  loading: any;
   getJsonData(){
  return this.http.get('http://localhost/posts.php').map(res => res.json());
}
  constructor(public httpProvider: HttpProvider, private http: Http, public navCtrl: NavController, public loadingCtrl: LoadingController) {
     this.loading = this.loadingCtrl.create({
      content: `
      <ion-spinner ></ion-spinner>`
    });

    this.getdata();

  }
 
 getdata(){
    this.loading.present();
    this.httpProvider.getJsonData().subscribe(
      result => {
        this.newsData=result.data.children;
        console.log("Success : "+this.newsData);
      },
      err =>{
        console.error("Error : "+err);
      } ,
      () => {
        this.loading.dismiss();
        console.log('getData completed');
      }
    );
  }
}

http.ts(provider)
import { Injectable } from ‘@angular/core’;
import { Http } from ‘@angular/http’;
import ‘rxjs/add/operator/map’;

/*
Generated class for the HttpProvider provider.

See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular DI.
*/
@Injectable()
export class HttpProvider {

constructor(public http: Http) {
console.log(‘Hello HttpProvider Provider’);
}
getJsonData(){
return this.http.get(‘http://localhost/posts.php’).map(res => res.json());
}

}home.html <ion-content> <ion-list > <ion-item text-wrap *ngFor="let item of newsData" > <p >{{item.data.title}}</p> <p>{{item.data}}</p> </ion-item> </ion-list> </ion-content>
posts.php

<?php $mysqli = new mysqli("localhost", "root", "", "technologies"); $query = "SELECT * FROM emp"; $dbresult = $mysqli->query($query); while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){ $data[] = array( 'id' => $row['id'], 'name' => $row['name'] ); } if($dbresult){ $result = "{'success':true, 'data':" . json_encode($data) . "}"; } else { $result = "{'success':false}"; } echo($result); ?>

This code needs some serious help.

  • You don’t want to inject both HttpProvider and Http in the same class
  • You don’t want per-page HttpProvider
  • getJsonData() needs a more descriptive name and should only be in HttpProvider, not HomePage
  • Don’t store loading indicators in object properties, this encourages reuse and double-dispose bugs
  • give newsData a proper type and initialize it

i solved thisi solved this

I have been doing this for well over 30 years.

In my experience, there are two types of programmers:

  • those who have the same sort of passion for the craft that writers or anybody else in a creative endeavour have
  • those who are just doing it because they are or think they will be get paid

The first category would never post what you just did. The second I have no interest in assisting.

2 Likes

i solved this . not slept for 2 days after watching ur comment. and did my self.