Page 1 of 1
Accessing Table Field Data
Posted: Sat Sep 02, 2017 10:13 am
by bidgeeman
Hi.
A friend of mine is helping me build a php page to communicate with my LC stack for registering passwords, email etc. I have parsed a JSON link to one of his php pages and placed the data into a Table Field with 4 lines down and 2 columns across . All I am trying to do is access the second columns that contain the important data. I have tried looking at the rev online lesson about accessing column data in fields but it seems extremely overly complex seeing that you can just put the entire text of the table field into a single field and code access to the lines and characters. So my question is....is there a straight forward way to access the data directly in a table field in any column or line?
Thanks
Bidge
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 11:25 am
by SparkOut
Hi bidge
You mean a table field right, not a datagrid?
A table field is just a field with a few properties like tabstops set. So if you set the itemDelimiter to tab, you can easily access say 'item 2 of line 4 of field "myTableField"'
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 11:40 am
by AxWald
Hi,
bidgeeman wrote:[...] the data into a Table Field with 4 lines down and 2 columns across . All I am trying to do is access the second columns that contain the important data. [...]
Code: Select all
set itemdel to tab
get item 2 of line 1 of fld "table_fld"
This way?
bidgeeman wrote:[...] So my question is....is there a straight forward way to access the data directly in a table field in any column or line? [...]
What I do usually is:
- After retrieving the data (from a file, a database, whatever) I save it for further use. Custom properties is what I prefer for this (I make sure to have proper tab-return delimited data!):
Code: Select all
set the cRawData of this stack to myData
If I need to change the data, I do it in a variable (myData). Often it is useful to have both the actual data & the original data available (for instance to check if the data have changed ...), so I use a second custom property:
Code: Select all
set the cWorkData of this stack to myData
- Sometimes it makes sense to have the data in array form (often I use both forms ...):
Code: Select all
split myData by row
set the cRawArray of this stack to myData
- Now that we have the data I can display 'em in the field:
Code: Select all
put the cWorkData of this stack into fld "table_fld"
- This is the easy way - often the data have to be formatted before displaying, so I don't put them into the field directly, but use another custom property, of the field:
Code: Select all
set the cTableData of fld "table_fld" to myData
and in the field script:
Code: Select all
setprop cTableData tData
repeat for each line myLine in tData
put item 1 of myLine & tab & item 2 of myLine & tab & \
round(item 3 of myLine) & CR after myVar
end repeat
delete last char of myVar
put myVar into me
pass cTableData
end cTableData
So each time I put changed data into my table (via setting the cTableData) the data get nicely formatted (here: column 3 has rounded number), without changing the real data.
- If the user selects a line in the field, and I want to do something based on the content of the 2nd column:
Code: Select all
on selectionChanged
-- runs whenever a line is hilited
if (line (the hilitedline of me) of me is empty) then exit selectionChanged
set itemdel to tab
put item 2 of line (the hilitedline of me) of me into myVar -- or wherever
end selectionChanged
- Trick: You can save "the tabstops" of your field & change them according to the data you feed to the field. For tabstop management, try "revTabRuler" in Development/Plugins - this is a well hidden gem!
Well, that's some basic techniques I use when working with table flds (and I do this a lot). Hope there's something you can use. Don't hesitate to ask more questions!
Have fun!
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 11:53 am
by bidgeeman
AxWald.
That worked perfectly thank you so much!
I could only find this online and it totally confused me:
http://lessons.livecode.com/m/datagrid/ ... -or-column
Many thanks once again
Bidge
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 12:00 pm
by bidgeeman
EDIT: [SOLVED] I closed down LC, re-opened it and the error went away????
Sorry to bother you all again but this snippet of code was working well for me earlier but now it has started throwing up and error:
Code: Select all
on mouseUp
put URL "http://www.something.com" into depthvariable
put jsonimport(depthvariable) into tArray
combine tArray by return and tab
put tArray into field "arraydata"
end mouseUp
Error Message:
Code: Select all
button "Parse": execution error at line 4 (LCB Error in file json.lcb at line 52: syntax error: 1:0 unexpected end of input)
Bidge
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 2:58 pm
by AxWald
Hi,
That's about "DataGrids". These are a different beast from table fields.
You can think of the datagrid as a tablefield with attached arrays, scripts, bell, whistles, spoilers & built-in confusion.
Both are doing actually the same, they're just suited for different needs. And have different requirements ...
If you (like me) prefer the lean, fast table field, those links may be helpful for you:
-
Scott Rossis "TableLab"
-
Bernd Niggemanns "modTableField"
Have fun!
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 3:28 pm
by Klaus
Hi David,
error checking, error checking, error checking! And "BINFILE"!
Code: Select all
on mouseUp
put URL "http://www.something.com" into depthvariable
if the result <> EMPTY then
answer "Error:" && the result
exit to top
end if
put jsonimport(depthvariable) into tArray
combine tArray by return and tab
put tArray into field "arraydata"
end mouseUp
You are probably passing invalid data to the "jsonimport()" function.
Best
Klaus
Re: Accessing Table Field Data
Posted: Sat Sep 02, 2017 5:32 pm
by jacque
bidgeeman wrote:EDIT: [SOLVED] I closed down LC, re-opened it and the error went away????
Sorry to bother you all again but this snippet of code was working well for me earlier but now it has started throwing up and error:
This means the JSON is incorrectly formed. I've seen it many times, usually because there's a missing end bracket or curly brace. It's usually because the PHP script isn't sending the right thing. You will probably see it again if you request the same data as the first time it errored.
It will be difficult to fix without an example of the malformed JSON , so as Klaus said, add some error checking and if one occurs, capture the incoming JSON in a field or text file or somewhere else where you can copy it and show him.
Re: Accessing Table Field Data
Posted: Sun Sep 03, 2017 12:38 am
by bidgeeman
Hi Klaus and jacque.
Thank you so much for the error checking pointer. I applied it and no error presented itself?
This is not the first time I have had something strange like that happen though.
Once again thank you for your help.
Bidge
Re: Accessing Table Field Data
Posted: Sun Sep 03, 2017 4:35 pm
by jacque
Well, it probably means the error only occurs with some particular types of data retrieval. Leave the error check in place and if it happen again you'll be able to examine the JSON.