Accessing Table Field Data

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Accessing Table Field Data

Post by bidgeeman » Sat Sep 02, 2017 10:13 am

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

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Accessing Table Field Data

Post by SparkOut » Sat Sep 02, 2017 11:25 am

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"'

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Accessing Table Field Data

Post by AxWald » Sat Sep 02, 2017 11:40 am

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:
  1. 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
  2. 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
  3. 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"
  4. 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.
  5. 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
  6. 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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Accessing Table Field Data

Post by bidgeeman » Sat Sep 02, 2017 11:53 am

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

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Accessing Table Field Data

Post by bidgeeman » Sat Sep 02, 2017 12:00 pm

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

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Accessing Table Field Data

Post by AxWald » Sat Sep 02, 2017 2:58 pm

Hi,
bidgeeman wrote:I could only find this online and it totally confused me:
http://lessons.livecode.com/m/datagrid/ ... -or-column
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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Accessing Table Field Data

Post by Klaus » Sat Sep 02, 2017 3:28 pm

Hi David,

error checking, error checking, error checking! And "BINFILE"! :D

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Accessing Table Field Data

Post by jacque » Sat Sep 02, 2017 5:32 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Accessing Table Field Data

Post by bidgeeman » Sun Sep 03, 2017 12:38 am

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Accessing Table Field Data

Post by jacque » Sun Sep 03, 2017 4:35 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”