- Home /
Question by
laharl219 · Mar 15, 2016 at 02:50 PM ·
dropdowndynamicallyfillsqldatabase
Can i fill a dropdown with data from a database or a Json file?
I need to dynamically fill a dropdown at unity, i can get the information from a database or a Json file(I just know how to fill a dropdown manually at the dropdown's component). Is there a way to do it?
Comment
Answer by matthewseaward · Mar 15, 2016 at 08:49 PM
I'm assuming you are using the new UI elements and canvas?
This is simple:
public Dropdown myDropdown; // Make sure to assign this
private List<string> data;
void Start()
{
data = LoadDataFromDBOrJSON(); // Or whatever way you will read your data in
myDropdown.options.Clear();
foreach(string str in data)
{
myDropdown.options.Add(new Dropdown.OptionData(str));
}
}
Answer by unity_eVEKXzm04KI-og · Jul 03, 2018 at 01:02 AM
The same question cómo leer los datos del mysql db ??,Lo mismo que pregunto tsondhi2006 cómo leer los datos del mysql db ??
Answer by tsondhi2006 · Jul 12, 2018 at 09:03 PM
Did it:-
1) Create a c# file
void Func1()
{
WWW www = new WWW ("127.0.0.1/somefolder/somefile.php");// give location of Php file
while (!www.isDone) {
}
string itemsDataString = www.text;
string[] wwwItems = itemsDataString.Split (';'); //the values in mysql db is picked in a single string and should have some marker so as to split
DD.options.Clear ();
foreach (string str in wwwItems) {
DD.options.Add (new Dropdown.OptionData (str));
}
}
2) Create a php file which will pass the values from mysql to unity [somefile.php]:-
<?php
$conn = new mysqli("127.0.0.1", "root", "", "somedb");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM sometable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo ";".$row["somefield"];
}
}
$conn->close();
?>
Your answer
![](https://koobas.hobune.stream/wayback/20220612071930im_/https://answers.unity.com/themes/thub/images/avi.jpg)