sandboxj
sandboxj

Reputation: 1254

Ng-repeat filter search using MySQL data

I am trying to populate a div tag with ng-repeat and data from a mysql database. I want to use ng-repeat because I want to apply a filter later on. My problem is combining angular and sql.

I want the html to do something like this

 <div>
 <input type="text" ng-model="filterText" class="search" placeholder="Filter ...">
 <p ng-repeat="data in datalist | filter : filterText">{{data}}</p>
 </div> 

I have the following function in my controller:

 getData();
 function getData() {
 $http.post("/server/get_data.php").success(function(data){
  $scope.datalist = data; // the above mentioned datalist
});
 };

I approached the sql retrieval in get_data.php as follows, but it is giving me output I do not understand:

 <?php 
 require_once 'db_connection.php';

    $query = "SELECT * FROM datatable";
    $result = mysqli_query($connection, $query);
    if (!$result) {
      die("Database query failed.");
    }
    echo $json_response = json_encode($result);
 ?>

The echo gives me the following: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}. And the ng-repeat therefore does not run.

Lets say my database has the columns "id" and "name". How do I get the right query / values for what to get the ng-repeat filter to work?

Alternatively, this is something very similar, but I do not understand how to get the if(isset($_GET[ ... ])) to work. What do I need to input in my case for this approach? The steps before the php are basically the same or?

http://angularcode.com/simple-task-manager-application-using-angularjs-php-mysql/

  $query="INSERT INTO tasks(task,status,created_at)  VALUES ('$task', '$status', '$created')";
  $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

  $result = $mysqli->affected_rows;

  echo $json_response = json_encode($result);
  }
  ?>

Upvotes: 1

Views: 1075

Answers (1)

Vitalii Strimbanu
Vitalii Strimbanu

Reputation: 477

Your {{data}} is actually an object(one row from your sql table), you can use

<p ng-repeat="data in datalist | filter : filterText">{{data.myfield}}</p>

and replace myfield with the field you want to display.

And one more thing, you do not actually fetch your $result from database, see mysqli_fetch_array

Upvotes: 1

Related Questions