I can't get mysql group values in ScrollView?

Hi,
I’m trying to create a ScrollViewList to show the users that have the same classroom code in mysql. So if three users have same classroom code, then they see eachother in the Classroom List.

For this purpose, I tried to get the data from mysql:
PHP:
siniflistesi.php

<? php
    $con = mysqli_connect('IP','dbname','dbpass','dbuser');

    if (mysqli_connect_errno())
    {
        echo "1: Bağlantı kurulamadı."; //error code 1=connection failed
        exit();
    }
        $classroom = mysqli_real_escape_string($con, $_POST["classroom"]);
        $query = "SELECT * FROM users WHERE classroom IN(SELECT classroom FROM users GROUP BY classroom having count(classroom ) > 1)"
        $result = mysqli_query($con, $query);
        if(mysqli_num_rows($result) > 0)
            {
                while($row = mysqli_fetch_array($result))
                    {
                        echo "0\n";
                        echo $row["username"]."\t". $row["pp"]."\n".;
                        //echo ";".$row["username"]. ";".$row["pp"].;   // pp is a point name.
                    }
            }
        else
        {
            echo "User info is wrong.";
        }
?>

And then created a c# for listing.

SinifListesi.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class SinifListesi : MonoBehaviour {

    public Button button;
    public Text ListNameLabel;
    public Text ListPpLabel;
    public Image ListAvatarImage;
    public GameObject ListKingImage;
}

And using this to create the list:

SinifListesi_CreateScrollList.cs

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;

[System.Serializable]
public class item
{
    public string ListName;
    public string ListPp;
    public Sprite ListAvatar;
    public bool ListKing;
    public Button.ButtonClickedEvent thingToDo;
}

public class SinifListesi_CreateScrollList : MonoBehaviour
{
    public GameObject sampleButton;
    public Transform contentPanel;
    public List<item> itemList;

    void Start()
    {
        StartCoroutine(Listingclassroom());
    }

    IEnumerator Listingclassroom()
    {
        WWW snflst = new WWW("http://localhost/sqlconnect/siniflistesi.php");
        yield return snflst;
        if (snflst.text != null)
        {
            DBManager.SnfLst = snflst.text;
            foreach (var item in itemList)
            {
                GameObject newButton = Instantiate(sampleButton) as GameObject;
                SinifListesi buttonScript = newButton.GetComponent<SinifListesi>();

                buttonScript.ListNameLabel.text = "Ö:" + snflst.text.ToString();
                buttonScript.ListPpLabel.text = item.ListPp;
                buttonScript.ListAvatarImage.sprite = item.ListAvatar;
                buttonScript.ListKingImage.SetActive(item.ListKing);

                buttonScript.button.onClick = item.thingToDo;
                newButton.transform.SetParent(contentPanel, false);
            }
        }
    }

    public void DoSomething()
    {
        Debug.Log("Something");
    }
}

Do you have any suggestions?
Thank you.

I can see that you put all your generated button under the content Panel.
Does it not look withe or what is wrong in that case?
Maybe using a vertical layout group might help.

1 Like

Thank you fffMalzbier, I’m trying a different way to do that.

public class ClassroomList : MonoBehaviour
{
    public Text StudentName;
    public Text StudentPp;
    public int TotalStudents;

    IEnumerator Start()
    {
        WWW www = new WWW("http://***/classroom.php");
        yield return www;

        JArray jsonArray = JArray.Parse(www.text);
        dynamic u = JObject.Parse(jsonArray[0].ToString());
        dynamic p = JObject.Parse(jsonArray[0].ToString());
        dynamic c = JObject.Parse(jsonArray[0].ToString());
        dynamic a = JObject.Parse(jsonArray[0].ToString());
        dynamic username = u["username"].ToString();
        dynamic pp = p["pp"].ToString();
        dynamic classroom = c["classroom"].ToString();
        dynamic accounttype = a["accounttype"].ToString();

        print(username);
        print(pp);
        print(classroom);
        print(accounttype);

        StudentName.text = username;
        StudentPp.text = pp;
    }
}

That code works perfectly for now. The code takes the student info from mysql to my UI.

But now my problem is that I can not duplicate the button for other students to show on the list.

Do you have any idea for listing all students in the list?