Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using this code to pull data from a MySQL.

$.ajax({                                      
  url: 'api.php',     
  data: "",
  dataType: 'json',    
  success: function(data)
  {
    var id = data[0]; 
    var icon = data[1];
    var english = data[2];
    var british = data[3];
    $('#output').html("<b>id: </b>"+id+"<b> icon: </b>"+icon+"<b> english: </b>"+english+"<b> british: </b>"+british); //Set output element html
  }

And it outputs this correctly but my questions is how would put this data into the below.

$scope.items = [
    {
        english: '<First Row english>',
        british: '<First Row british>',
        image: '<First Row icon>'
    },
    {
        english: '<Second Row english>',
        british: '<Second Row british>',
        image: '<Second Row icon>'
    }
      //So on and so forth for all the records in the DB. 
   ]

This is from api.php not sure if this needs to be returned a certain way?

<?php 
require_once 'db.php'; // The mysql database connection script
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  $query=mysql_query("SELECT * FROM $tableName") or die(mysql_error());

while($obj = mysql_fetch_object($query)) {
 $arr[] = $obj;
}

echo $json_response = json_encode($arr);

?>
share|improve this question

1 Answer 1

 <?php 
  require_once 'db.php'; // The mysql database connection script
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  $query=mysql_query("SELECT * FROM $tableName") or die(mysql_error());
  $arr[];
  while($obj = mysql_fetch_object($query)) {
      array_push($arr, $obj);
  }

  echo $json_response = json_encode($arr);

 ?>

JS

 $http.get('api.php').success(function(data) {
     $scope.items = data;
 });
share|improve this answer
    
Thanks for the response, but it isn't quite working yet. I have added the code from api.php incase i am returning it wrong? –  Travis Apr 17 at 0:18
    
Then instead of numbers in data[] use columns name ex: english : data['english'], –  Issam Zoli Apr 17 at 1:04
    
Try new code, of course $http.get is in an angular controller –  Issam Zoli Apr 17 at 1:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.