首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >来自php脚本的搜索栏和结果

来自php脚本的搜索栏和结果
EN

Stack Overflow用户
提问于 2018-04-14 04:08:59
回答 1查看 245关注 0票数 0

你好,我已经创建了一个SearchPage,但是我不知道如何用php脚本返回搜索结果。我已经创建了一个authservice提供程序来连接数据库,只要我调用它来返回会话用户的数据,它就会工作得很好。我希望用户在搜索栏中键入字母,并将结果显示在

代码语言:javascript
复制
<ion-content padding>
<ion-card *ngFor="let item of dataSet; let msgIndex = index">

authservice.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

let apiUrl = "http://localhost/PHP-Slim-Restful1/api/";

@Injectable()
export class AuthService {

constructor(public http: Http) {
console.log('Hello AuthService Provider');
 }

 postData(credentials, type){

  return new Promise((resolve, reject) =>{
let headers = new Headers();
 this.http.post(apiUrl+type, JSON.stringify(credentials), {headers:   headers}).
 subscribe(res =>{
  resolve(res.json());
  }, (err) =>{
  reject(err);
 });

});

 }

 }

SearchPage.ts

代码语言:javascript
复制
  @Component({ selector: "page-search", templateUrl: "search.html" })
 export class SearchPage {
  public userDetails: any;
  public resposeData: any;
 public dataSet: any;
 public noRecords: boolean;
  userPostData = {
  uid: "",
  token: "",
  username: "",
  bio: "",
  profile_pic: "",
  message: "",
  msg_id: "",
   title: "",
   description: "",
  media_pic: "",
   created: "",
  uid_fk: ""
};

constructor(
 public common: Common,
 public navCtrl: NavController,
 public app: App,
 public menu: MenuController,
 public authService: AuthService,
 platform: Platform,
 statusBar: StatusBar,
 splashScreen: SplashScreen
  ) {
     const data = JSON.parse(localStorage.getItem("userData"));
  this.userDetails = data.userData;
  this.userPostData.uid = this.userDetails.uid;
  this.userPostData.token = this.userDetails.token;
  this.userPostData.username = this.userDetails.username;
  this.userPostData.msg_id = this.userDetails.msg_id;
  this.userPostData.message = this.userDetails.message;
  this.userPostData.title = this.userDetails.title;
  this.userPostData.description = this.userDetails.description;
  this.userPostData.media_pic = this.userDetails.media_pic;
  this.userPostData.created = this.userDetails.created;
  this.userPostData.profile_pic = this.userDetails.profile_pic;
  this.userPostData.bio = this.userDetails.bio;
  this.noRecords = false
  this.SearchResults();
 }


  SearchResults() {
  this.authService.postData(this.userPostData, "userGroupSearch").then(

   result => {
       this.resposeData = result;
       if (this.resposeData.userGroupSearch) {
           this.dataSet = this.resposeData.userGroupSearch;
           console.log(this.dataSet);

       } else {
           console.log("No access");
       }
   },
   err => {
       //Connection failed message
   }
  );
 }

我在这里调用PHP函数,userGroupSearch是函数的名称。

代码语言:javascript
复制
  this.authService.postData(this.userPostData, "userGroupSearch").then(

而JSON在这里返回

代码语言:javascript
复制
  this.dataSet = this.resposeData.userGroupSearch;

SearchPage.html

代码语言:javascript
复制
   <ion-header>
   <ion-navbar>
  <button ion-button menuToggle>
  <ion-icon name="menu"></ion-icon>
   </button>
   <ion-searchbar [(ngModel)]="myInput"
           [showCancelButton]="shouldShowCancel"
           (ionInput)="onInput($event)"
           (ionCancel)="onCancel($event)"
           placeholder="Search for Media, Artists, Flows...">
    </ion-searchbar>
    </ion-navbar>

    <ion-content padding>
    <ion-card *ngFor="let item of dataSet; let msgIndex = index"></ion-card>
    </ion-content>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49824428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档