How do I populate an array with records from a sql table

0 favourites
  • 6 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hi there! I want to retrieve the records from a table (ex:index1, name1, index2, name2, etc) and load it in an array and then put it in a dropdown list like: name1, name2, etc).

    And I don't know how much records will be in the table so I retrieve it through a query and set a global variable in construct to know how much records will I have to show.

    I tried using loops and ajax requests but I'm totally lost and I need it desperately to work.

    Anyone can help me, please!?

  • Retrieve it all in one query and use a seperator in between every bit of data. You set that in the php script. As an example i will use "|" as a seperator.

    The line in php would look something like this:

    echo $row["index1"] . "|" . $row["name1"] . "|" . $row["index2"] . "|" . $row["name2"] . "|" ;

    Then in Construct2 you use: system > set variable > (tokencount(AJAX.LastData, "|")

    This will count the pieces of data seperated of by the "|"

    Then just set the size of the array with that variable.

  • Retrieve it all in one query and use a seperator in between every bit of data. You set that in the php script. As an example i will use "|" as a seperator.

    The line in php would look something like this:

    echo $row["index1"] . "|" . $row["name1"] . "|" . $row["index2"] . "|" . $row["name2"] . "|" ;

    Then in Construct2 you use: system > set variable > (tokencount(AJAX.LastData, "|")

    This will count the pieces of data seperated of by the "|"

    Then just set the size of the array with that variable.

    Thanks a lot man, that's exactly what I did, after hours of searching around. Really thanks. Now if you could help with one more thing: I created a dropdown list with the array generated with the query, but how do I assign the value of the selected item in the list to a variable? Please, if you could help me with this too, it would be much appreciated!

  • Nevermind man! Just figured it out. Thanks!

  • stymphalian

    Sorry i didn't see your message earlier. Just got home. Got it working now though?

    Setting a variable to the first item in the array would be system > set variable > arrayname.At(0, 0)

    This is assuming you have a 2D array..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I have a 2d array with one field on each axis, so my X axis is "name" and Y axis is "ID". I fill the list with items in the X axis. So my list is a list of names. I'd like to assign to a variable, the value of the Y axis related to the X element selected in the list, but I'm having some trouble with it.

    EX: The value in index 0 of the list is the value in index 0, Axis X of the Array. and so on. When selecting an item in the list, I'd like to assign to var the value of the Y axis related to that X element in the array.

    I dont know if I made myself clear, I'm a little confused right now.. lol

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)