Loading of content to the list with iteration from php echo

I’m trying to load with constructor names for dynamically generated buttons from list public names: any = []; by number this.nmb, generated this way:

name.ts NamePage:


    name() { 
      this.names = [];   
       for (var i = 0; i <= this.nmb; i++) {      
       var headers = new Headers();
       headers.append('Content-Type', 'application/x-www-form-urlencoded');
       let options = new RequestOptions({ headers: headers });
         let postParams = '&user=' + this.user + '&num=' + i;
         this.http.post(this.phpPath + "select.php", (postParams), options)
           .subscribe(data => {
             this.names.push(data.text().trim());
           }, error => {
             console.log(error);
           });
       }
     }

with backend select.php:

then by num from btnsArray which is equal to this.nmb in name.html, but each time list created differently, with one load public names: any = []; order proper:

    Sally
    John
    Said
    Maria

without certain sequence, but randomly, with another constructor load, loading order wrong:


    John
    Sally
    Said
    Maria

So It makes the correct loading of the NamePage content (click)="fnc(nmb) by the order from MySql, but sometimes not through the correct name assigned to the button {{bandNames[nmb]}}.

Seems like something wrong with loading this.names.push(data.text().trim()); but I’m not sure what exactly.

Advice would be helpful

First off, this API is pretty inefficient, so if you fixed it to take a list and return a list, then you would have only one HTTP request and this entire problem would go away on its own.

If you don’t want to do that, though, then you have to realize that the order that you fire off HTTP requests has zero relation to the order you will see responses. You are going to have to use something like the forkJoin operator to trigger an action after all requests have resolved, and then sort names at that point.

1 Like

@rapropos Hello, thanks for answer

yes exactly, I’ve solved this with return of list and splitting by exists sequence:

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["name"];
    }
} else {
    echo "0 results";
}