How do I fill a drop down list with array data

Get help using Construct 2

Post » Tue Aug 26, 2014 4:37 am

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?
B
5
S
1
Posts: 14
Reputation: 408

Post » Tue Aug 26, 2014 4:41 am

Yes there is a way. Loop through the array and add each item from the array to the list.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 26, 2014 1:45 pm

Can you plz show me an example how?
im trying but i only get a blank list.
B
5
S
1
Posts: 14
Reputation: 408

Post » Tue Aug 26, 2014 1:53 pm

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.
You do not have the required permissions to view the files attached to this post.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,795

Post » Tue Aug 26, 2014 9:27 pm

ozkr wrote:Can you plz show me an example how?
im trying but i only get a blank list.


show a code screenshot, would help.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Dec 28, 2015 1:49 am

Hi,
Thanks for the reply :)

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

The JSON array is:
Code: Select all
[{"nombre":"test"},{"nombre":"Oscar"},{"nombre":"Oscar"},{"nombre":"test"},{"nombre":"test"},{"nombre":"oscar"},{"nombre":"Oscar"}]


and the code:
Image
can you help me plz?

Thanks in advance.
B
5
S
1
Posts: 14
Reputation: 408

Post » Mon Dec 28, 2015 1:53 am

ozkr wrote:Hi,
Thanks for the reply :)

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

The JSON array is:
Code: Select all
[{"nombre":"test"},{"nombre":"Oscar"},{"nombre":"Oscar"},{"nombre":"test"},{"nombre":"test"},{"nombre":"oscar"},{"nombre":"Oscar"}]


and the code:
Image
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
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Dec 28, 2015 10:24 am

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) :

Code: Select all
{"c2array":true,"size":[10,1,1],"data":[[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]],[[value]]]}


The JSON data you quoted have nothing to do with that format.
You can quickly check in the debugger on execution and see that your array is likely empty even when it should contain the content of the JSON file.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,795

Post » Mon Dec 28, 2015 11:56 pm

Thanks for the quick reply :)

Can you help me with the json format?

This is my php
Code: Select all
$emparray = array();
      // Start looping rows in mysql database.
      while($rows=mysqli_fetch_assoc($result)){
         $emparray[] = $rows;
      // close while loop
      }
      echo json_encode($emparray);

what do i have to add to get that format?

Thanks in advance!
B
5
S
1
Posts: 14
Reputation: 408

Post » Tue Dec 29, 2015 1:33 am

Hi,
I just found the solution :)

Code: Select all
$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);


Now the array data is correct in the debug layout.
Code: Select all
{"c2array":true,"size":[7,1,1],"data":[[["test"]],[["Oscar"]],[["Oscar"]],[["test"]],[["test"]],[["oscar"]],[["Oscar"]]]}

But the combobox is showing infinite options. lol

---UPDATE----

I did it! :)
I fix the combobox like this:
Image
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: Select all
{"c2array":true,"size":[7,1,1],"data":[["2","test"],["3","Oscar"],["4","Oscar"],["5","test"],["6","test"],["7","oscar"],["8","Oscar"]]}


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.
B
5
S
1
Posts: 14
Reputation: 408

Next

Return to How do I....?

Who is online

Users browsing this forum: dand, mickclarke and 29 guests