DanielSmurts
DanielSmurts

Reputation: 641

how to retrieve particular data from firebase database for web

I created a database for footballers, I would like to retrieve the data and display it in a table, but I would like to be able to retrieve say defenders separately, strikers separately....not all the player data at once

this is how I am currently retrieving and displaying the data

var rootRef = firebase.database().ref("imageurl");
rootRef.on("child_added", snap => {
  var pic = snap.child("url").val();
  var name = snap.child("name").val();
  var nationality = snap.child("nationality").val();
  var birthday = snap.child("dob").val();
  var height = snap.child("height").val();
  var position = snap.child("category").val();
  var foot= snap.child("foot").val();
  var weight = snap.child("weight").val();
  var transfer = snap.child("transferLink").val();
  var youtube = snap.child("youtubeLink").val();

  $("#tbl_body").append("<tr><td>" + "<img class=footbl src=" + pic + ">" + "</td><td>" + name + "</td><td>" + nationality + "</td><td>" + birthday +  "</td><td>" + height +
   "</td><td>" + position +  "</td><td>" + foot +  "</td><td>" + weight +  "</td><td>" + transfer +
    "</td><td>" + youtube + "</td></tr>");
});

but this option lists all the database entries... I would like to be able to retrieve, say only the defenders for example.

Upvotes: 1

Views: 81

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

You'll want to use a Firebase Database query for that. It's a bit unclear where the position of a player is kept from the code you shared. But say you want to only show Dutch soccer players, you can do so by:

var rootRef = firebase.database().ref("imageurl");
var query = rootRef.orderByChild("nationality").equalTo("Netherlands");
query.on("child_added", snap => {
  ...

For more on this, read the Firebase documentation on sorting and filtering data.

Upvotes: 1

Related Questions