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

Get help using Construct 2

Post » Fri May 22, 2015 6:41 pm

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!?
B
3
Posts: 6
Reputation: 187

Post » Sat May 23, 2015 7:56 am

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.
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

Post » Sat May 23, 2015 6:17 pm

UberDark wrote: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!
B
3
Posts: 6
Reputation: 187

Post » Sat May 23, 2015 6:59 pm

Nevermind man! Just figured it out. Thanks!
B
3
Posts: 6
Reputation: 187

Post » Sat May 23, 2015 8:08 pm

@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..
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

Post » Sun May 24, 2015 11:01 pm

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
B
3
Posts: 6
Reputation: 187


Return to How do I....?

Who is online

Users browsing this forum: Lordshiva1948, plinkie, sublimate and 14 guests