How do I fill a drop down list with array data

  • I have an array from a json.

    System | On start of layout - Request URL (tag "mytag")

    AJAX On "mytag" completed - Array | Load from JSON string ajax.LastData

    Is there a way i can fill a drop down list with my json array?

    Can you help me plz?

  • Yes there is a way. Loop through the array and add each item from the array to the list.

  • Can you plz show me an example how?

    im trying but i only get a blank list.

  • This is one possible implementation.

    Make sure your array is filled.

    It may also depend on your current project, there might be more efficient way (or "delayed way" using the function plugin for example), but that's the basic idea.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Can you plz show me an example how?

    im trying but i only get a blank list.

    show a code screenshot, would help.

  • Hi,

    Thanks for the reply

    im sorry for the delay but here is the screenshot and the array:

    The JSON array is:

    [{"nombre":"test"},{"nombre":"Oscar"},{"nombre":"Oscar"},{"nombre":"test"},{"nombre":"test"},{"nombre":"oscar"},{"nombre":"Oscar"}][/code:z9g7g4el]and the code:
    [img="http://s12.postimg.org/xdfzouu7x/Sin_t_tulo.png"]
    can you help me plz?
    
    Thanks in advance.
  • Hi,

    Thanks for the reply

    im sorry for the delay but here is the screenshot and the array:

    The JSON array is:

    [{"nombre":"test"},{"nombre":"Oscar"},{"nombre":"Oscar"},{"nombre":"test"},{"nombre":"test"},{"nombre":"oscar"},{"nombre":"Oscar"}][/code:3htc8ftb]> 
    and the code:
    [img="http://s12.postimg.org/xdfzouu7x/Sin_t_tulo.png"]
    can you help me plz?
    
    Thanks in advance.
    

    Why not use the way in the capx example by Kyatric ?

    So instead of for each array, use :Array for each x element

  • Moreover The JSON format you try to use is not correct and is likely the reason why your array is empty to begin with.

    In Construct 2, if you try to get the JSON out of an array, you'll notice the format is as such (in this example, it is a simple 10,1,1 array so mainly 10 cells on the X axis only) :

    {"c2array":true,"size":[10,1,1],"data":[[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]]]}[/code:3qeiicui]
    
    The JSON data you quoted have nothing to do with that format.
    You can quickly check in the [url=https://www.scirra.com/manual/169/debugger]debugger[/url] on execution and see that your array is likely empty even when it should contain the content of the JSON file.
  • Thanks for the quick reply

    Can you help me with the json format?

    This is my php

    $emparray = array();
    		// Start looping rows in mysql database.
    		while($rows=mysqli_fetch_assoc($result)){
    			$emparray[] = $rows;
    		// close while loop 
    		}
    		echo json_encode($emparray);
    [/code:wvakingv]
    what do i have to add to get that format?
    
    Thanks in advance!
  • Hi,

    I just found the solution

    $result=mysqli_query($con, $sql);
    
    		$emparray = array();
    		// Start looping rows in mysql database.
    		while($rows=mysqli_fetch_assoc($result)){
    			$emparray[] = $rows;
    		// close while loop 
    		}
    		//print_r($emparray);
    		//echo json_encode($emparray);
    		$output = array(
    	   'c2array' => true,
    	   'size' => array(
    	       0 => count($emparray),
    	       1 => 1,
    	       2 => 1
    	   ),
    	   'data' => array()
    	    );
    
    	    $x = 0;
    	    foreach ($emparray as $value) {
    	   $output['data'][$x] = array();
    	   $output['data'][$x][0] = array();
    	   $output['data'][$x][0][0] = $value["nombre"];
    	   $x++;
    	    }
    
    	    echo json_encode($output);
    [/code:2vqhamr3]
    
    Now the array data is correct in the debug layout.
    [code:2vqhamr3]{"c2array":true,"size":[7,1,1],"data":[[["test"]],[["Oscar"]],[["Oscar"]],[["test"]],[["test"]],[["oscar"]],[["Oscar"]]]}[/code:2vqhamr3]
    But the combobox is showing infinite options. lol
    
    ---UPDATE----
    
    I did it! 
    I fix the combobox like this:
    [img="http://s13.postimg.org/is1qrumdz/Sin_t_tulo.png"]
    OK now, how can i add an id in the same JSON? i mean i want to get the id of the users from the database, i guess i need them in the JSON query so i have to modify the SQL query to show it on the JSON but i dont know in what format.
    
    Now my JSON looks like this:
    
    [code:2vqhamr3]{"c2array":true,"size":[7,1,1],"data":[["2","test"],["3","Oscar"],["4","Oscar"],["5","test"],["6","test"],["7","oscar"],["8","Oscar"]]}[/code:2vqhamr3]
    
    The number are the id´s of each name in the db, but the c2 array shows only the id of each element but no the name, i want to show the name in the combobox but use the id to work with the mysql db.
  • What is the way to get 2 fields from Mysql database into an array?

    Is my array format correct?

    {"c2array":true,"size":[7,1,1],"data":[["2","test"],["3","Oscar"],["4","Oscar"],["5","test"],["6","test"],["7","oscar"],["8","Oscar"]]}
    [/code:3jkdgoc4]
    Thanks in advance!
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)