• 3
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have been trying to make an app that would let users take an assessment and then reveal their score. The registration of users is already working however i am trying to add score node in that parent node. I cant access the child node and i cant update the users score. please help me.The User is the parent node and its child is Rona.

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Users").child("Rona");
Map map = new HashMap < > ();
map.put("score", 0);
ref.updateChildren(map);

The result will be the addition of the score property in your database with the value of 0.

Edit: According to your comment:

but i wanted to access other users to. not just Rona

To solve this, you need to use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Users").orderByChild("username");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds: dataSnapshot.getChildren()) {
            Map map = (Map) ds.getValue();
            map.put("score", 0);
            ds.getRef().updateChildren(map);
        }
    }
    @Override public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

Using the above code, you'll be able to update the score property not only to Rona but to all the other users.

  • 1
Reply Report
      • 2
    • @RonaCeciliaAbayata In this case, change Log.d(TAG, databaseError.getMessage()); to Log.d("TAG", databaseError.getMessage());. Does it work now?
      • 1
    • thanks for this. but i wanted to access other users to. not just Rona i dont want to specify on who to put the score with.
    • like if other users will have to register, i will have to get the username but i dont know how to do that.