Ryan Ryan - 6 months ago 11
Javascript Question

Displaying an array in angular.js

I'm sending data of to a php file and the php file is processing and doing a sql search and its bringing back a list in a for each statement, for example: ryan, jack, billy, sarah

etc

when I echo the response, in angular, i'm getting all the names joined up like: ryanjackbillysarah

How can I show this on the page in a list so like:
Ryan
Jack
Billy
Sarah

This is my php

$result = mysqli_query( $connect, $sql);

if($result){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$list .= $row["firstname"];

}
}

echo $list;


This is my angular success, function.

// Success call back function.
request.then(function successCallback(response) {


//alert(response.data);
alert(response.data);



},

Answer

Just use json_encode in your php to return a JSON to angular.

$result = mysqli_query( $connect, $sql);

if($result){
 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    $list[] = $row["firstname"];

  }
}

echo json_encode($list);
Comments