Need to add variables depending on the answer to a question

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
Peter K
Posts: 63
Joined: Sat Apr 09, 2011 9:33 pm

Need to add variables depending on the answer to a question

Post by Peter K » Wed Jun 15, 2011 2:16 am

I've got a variable called property_type. Depending on the answer to the property_type question I will want to ask more questions. I need follow up information.

My code checks for the answer to property code as follows:

Code: Select all

on closeField
   global prop
   
   switch upper(fld m_prop_type)
      case "CONDO"
         put 1 into prop
         break
      case "COOP"
         put 2 into prop
         break
      case "SFR"
         put 3 into prop
         break
      default
         answer warning "Your answer is invalid"
   end switch
   
end closeField
If the answer is CONDO I want to know the common charges and real estate taxes for the property. If the answer is COOP I want to know the maintenance and tax deductibility of the maintenance. If the answer is SFR (single family residence) I need to know know the real estate taxes on the property. The question is how do I put the questions on the stack (and get the answer)?

Thanks in advance,

Peter

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Need to add variables depending on the answer to a question

Post by Mark » Wed Jun 15, 2011 11:26 am

Hi Peter,

Whether you want to display real estate prices or ice cream flavours is totally irrelevant. What exactly do you want to do? What do you want it to look like?

One way to ask a user a question is:

Code: Select all

ask question "Hi, how are you doing?" with "I'm fine, thank you"
if it contains "fine" then
  put "You are doing fine" into fld x
else
  put "Do you want to tell more?" into fld x
end if
If you are more specific, I migh be able to help you better.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Peter K
Posts: 63
Joined: Sat Apr 09, 2011 9:33 pm

Re: Need to add variables depending on the answer to a question

Post by Peter K » Wed Jun 15, 2011 7:29 pm

If the user says that the property is a condo, I need information on common charges and real estate taxes.

I'm looking for the code to be something like:
Case condo
@ row, column say "What are the common charges " get common charges
@ row, column say "What are the real estate taxes " get real estate taxes
break

Thanks,

Peter

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Need to add variables depending on the answer to a question

Post by Mark » Wed Jun 15, 2011 7:38 pm

Peter,

Stop writing "condo" and "real estate". Start thinking technically. What do you want to happen on the screen? What is the input that you need from the user?

I really have no clue what you want, buy maybe this helps you:

Code: Select all

switch myVar
case "house1"
  put "House 1" into fld "Type of House"
  break
case "house2"
  put "House 2" into fld "Type of House"
  break
end switch
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Need to add variables depending on the answer to a question

Post by mwieder » Wed Jun 15, 2011 8:07 pm

Peter- try something like

Code: Select all

switch prop
  case 1 -- "CONDO"
    ask "what are the common charges?"
    if it is not empty then
      -- do something with it
    end if
    ask "what are the real estate taxes?"
    if it is not empty then
      -- do something with it
    end if
    break
  case 2
    -- etc
    break
  case 3
    break
  default
end switch

Peter K
Posts: 63
Joined: Sat Apr 09, 2011 9:33 pm

Re: Need to add variables depending on the answer to a question

Post by Peter K » Sun Jun 19, 2011 3:49 am

I've used the ask statement to get the information that I need, but I don't like the way that it looks. Maybe it's a pet peeve. I have a lot of white space on the card that asks the original question, and I'd like to figure out a way to put the questions on the same card as the rest of the information that the user is entering. I'm just stuck on the syntax. If I use put "House1" into fld "Type of house" where do I initialize (for lack of a better phrase) "Type of House"?

Thanks,

Peter

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Need to add variables depending on the answer to a question

Post by mwieder » Sun Jun 19, 2011 4:36 am

Peter- I have no idea what you're trying to do. You haven't given a lot of information, and some of it contradicts other parts.

The example I posted (using ask statements) isn't at all how I would deal with things, but it does give you something to work with based on what you originally posted.

From what I can gather from what you've posted here, I think you'd be better off with a pulldown menu rather than a text field. That way you can control the answers instead of having free-form text. And in the menuPick handler of that menu button you'd deal with the next step. If you're looking for an ajaxy way to put information on the screen that varies with the answers to already-answered questions then there are several ways of doing that. What is it you're trying to accomplish?

Peter K
Posts: 63
Joined: Sat Apr 09, 2011 9:33 pm

Re: Need to add variables depending on the answer to a question

Post by Peter K » Sun Jun 19, 2011 6:45 pm

I agree with you that a pulldown menu would be much better than entering text into a field. I'll make that change at some point. I'm still getting used to the syntax of this language and how things are done.

Basically I have three options in a text field. Lets call my options 1, 2 or 3. If the user chooses option 1 then I need answers to 2 questions. Call the answers A and B. If the user chooses option 2 then I need answers to two different questions. Lets call the answers C and D. If the user chooses option 3 then I need an answer to 1 question, call the answer E.

The questions are obviously specific to the option chosen in the text field. So the text that describes the information that I need must be specific. As an example for option 1 answer A may be the answer to "What time is it?". For option 2 answer C may be the answer to "What is today's date". For option 3 the answer to E may be the answer to "What day of the week is it?"

So my thought is that depending on the text entered in the field, I would like these questions put on the card with all of the other data entry information so the user can see what they've entered.

Does this make more sense?

Thanks,

Peter

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Need to add variables depending on the answer to a question

Post by mwieder » Sun Jun 19, 2011 7:32 pm

That helps some. If those are your parameters then I'd do something like this:

Place two input fields and two corresponding label fields on the card.
Depending on the state of the pulldown menu, change the text of the label fields and/or hide label field 2 and text field 2. So this way you really only have two input fields to worry about, you're just changing the associated label field so that it looks like you're showing different input fields.

(Untested but...)

Code: Select all

on menuPick pChosen
  show lblField2
  show txtField2
  switch pChosen
    case "option 1"
      put "what time is it?" into lblField1
      break
    case "option 2"
      put "what is today's date?" into lblField1
      break
    case "option 3"
      hide lblField2
      hide txtField2
      put "what day of the week is it?" into lblField1
      break
  end switch
end menuPick
Now you're ready to get the text input from the fields and do what you want with it.

An alternative method might be to have multiple cards with similar-looking controls and switch to a different card depending on the state of the option.

Peter K
Posts: 63
Joined: Sat Apr 09, 2011 9:33 pm

Re: Need to add variables depending on the answer to a question

Post by Peter K » Mon Jun 20, 2011 8:39 pm

This almost works. The syntax for the show command is "show fld lbField1". I've also hidden the text field. I can't figure out the syntax to show the text field and then accept the answer that is put in. If the answer to What time is it? is 4:00, I'm stuck on how to show the text entry field and get the answer 4:00. I'll keep trying but if you have the answer, I'd appreciate it.

Thanks for everything so far,

Peter

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Need to add variables depending on the answer to a question

Post by mwieder » Mon Jun 20, 2011 10:09 pm

The syntax for the show command is "show fld lbField1".
:oops:

...but now I'm confused. Why are you hiding the text field with the answer? You only need to hide the fields that will be unused for your particular questions. Am I missing something else obvious?

show field "nameOfYourTextField" -- will show the text field
put field "nameOfYourTextField" into tAnswer -- will put "4:00" into variable tAnswer (even if it's hidden)

You will probably want a returnInField and/or enterInField handler in the text field to trigger getting the answer.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”