• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

multiple switch statements

I am trying to set up a form that displays the value of radio buttons from a form from a database.

heres my thought,

The first question's value is "one"
I want a switch statement to say

This works when I am checking one question but doesn't work if I want to do this for question "two"

I have about 80 something questions.

What do I need to do to devide these switch statements?
switch (one){
case "ok";
print "
value='ok' checked
value='needs_work'
value='not_applicable'";
}
 
switch (one){
case "needs_work";
print "
value='ok'
value='needs_work checked'
value='not_applicable'";
}
 
switch (one){
case "not_applicable";
print "
value='ok'
value='needs_work'
value='not_applicablechecked'";
}
 
switch (two){
case "ok";
print "
value='ok' checked
value='needs_work'
value='not_applicable'";
}
 
switch (two){
case "needs_work";
print "
value='ok'
value='needs_work checked'
value='not_applicable'";
}
 
switch (two){
case "not_applicable";
print "
value='ok'
value='needs_work'
value='not_applicablechecked'";
}

Open in new window

0
noel4nopun
Asked:
noel4nopun
  • 10
  • 10
  • 2
1 Solution
 
nizsmoDeveloperCommented:
how about something like this, is this what you are looking for?

<?php
      // your code..
      
      switch($question)
      {
            case "one":
                  {
                        // code for your question one
                        break;
                  }
                  
            case "two":
                  {
                        // code for your question two
                        break;
                  }
            
            case "three":
                  {
                        // code for your question three
                        break;
                  }
            
            // etc...
      }
?>

0
 
gamebitsCommented:
switch (one){

     case "first case":
           blablabla
      break;

     case "second case":
         blebleble
       break;
   
       case "third case":
           bliblibli
      break;
}

same thing for all the case statement
   
0
 
noel4nopunAuthor Commented:
how is that different than what I showed as an example.

My above example works indivisually, but not when I add the second one.

It only uses the last switch statement...
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
nizsmoDeveloperCommented:
you have so many switches, which is probably not necessary. Most of the time one switch consists of many cases, instead of what you have above which only has 1 case to 1 switch.

Not totally sure what you are trying to do, maybe you can clarify?
0
 
noel4nopunAuthor Commented:
I actually thought I had a switch statement that had 3 cases to them

if question "one" (which is the switch)
equals "ok" (the 1st case)
then print the form radio button with ok checked as an initial value...

if question "one" equals "needs_work"
then print the radio button to have needs work checked.

if question "one" equals "not_applicable"
then print the radio button to have not_applicable as the initial value...


The above example I showed works.
But only for one question

If I list say 10 questions only the last one in the list actually displays the radio button with an initial value, the rest are left blank?
0
 
noel4nopunAuthor Commented:
My apolgies for the above example the code I used had only one switch then the 3 cases inside the handler, I posted what I thougt was a condinsed version of the code I am trouble shooting but just realized that it was in fact wrong...

Should have been
switch (one){
case "ok";
print "
value='ok' checked
value='needs_work'
value='not_applicable'";
 
case "needs_work";
print "
value='ok'
value='needs_work checked'
value='not_applicable'";
 
 
case "not_applicable";
print "
value='ok'
value='needs_work'
value='not_applicablechecked'";
}
 
switch (two){
case "ok";
print "
value='ok' checked
value='needs_work'
value='not_applicable'";
 
case "needs_work";
print "
value='ok'
value='needs_work checked'
value='not_applicable'";
 
case "not_applicable";
print "
value='ok'
value='needs_work'
value='not_applicablechecked'";
}

Open in new window

0
 
nizsmoDeveloperCommented:
what is your "one" and "two"?

are they variables? should they not have a dollar sign before them? eg $one, $two...we are talking about php right? :)
0
 
gamebitsCommented:
I'd like to see the form that is calling this script.
0
 
noel4nopunAuthor Commented:
yes where talking php
and the actual code is a variable that has a value brought in from the database...

in this example "one" is a question on a form that the answer has three radio button,
The code I am trouble shpooting is when my administrator needs to update a form,...

it loads up the form with the radio buttons selected whith the current value from the database

0
 
noel4nopunAuthor Commented:

<?php
	  switch ($row['one']){
	  	case "ok":
	 	print " OK <input name='one' type='radio' value='ok'checked>
      		+ work <input name='one' type='radio' value='needs_work'>
      		NA <input name='one' type='radio' value='not_applicable'>";
			
			break;
			
		case "needs_work":
	 	print " OK <input name='one' type='radio' value='ok'>
      		+ work <input name='one' type='radio' value='needs_work'checked>
      		NA <input name='one' type='radio' value='not_applicable'>";
		
			break;
			
		case "not_applicable":
		print " OK <input name='one' type='radio' value='ok'>
      		+ work <input name='one' type='radio' value='needs_work'>
      		NA <input name='one' type='radio' value='not_applicable'checked>";
			break;
		}
			?>
       <?php
	   
	   print"</td>
    </tr>
    <tr>
      <td colspan='9' align='left'>82.	Install updated PM sticker (if vehicle meets DOT regulations)</td>
      <td align='center'>"?>
      <?php
	  switch ($row['eightytwo']){
	  	case "ok":
	 	print " OK <input name='one' type='radio' value='ok'checked>
      		+ work <input name='one' type='radio' value='needs_work'>
      		NA <input name='one' type='radio' value='not_applicable'>";
			
			break;
			
		case "needs_work":
	 	print " OK <input name='one' type='radio' value='ok'>
      		+ work <input name='one' type='radio' value='needs_work'checked>
      		NA <input name='one' type='radio' value='not_applicable'>";
		
			break;
			
		case "not_applicable":
		print " OK <input name='one' type='radio' value='ok'>
      		+ work <input name='one' type='radio' value='needs_work'>
      		NA <input name='one' type='radio' value='not_applicable'checked>";
			break;
		}
			?>
       <?php

Open in new window

0
 
nizsmoDeveloperCommented:
so you are saying that switch ($row['one']) works but switch ($row['eightytwo']) (or any other number) doesn't? have you checked to see if the variables are assigned correctly?
0
 
noel4nopunAuthor Commented:
yeah no matter which one i try, it only works for the last entry...
0
 
nizsmoDeveloperCommented:
hmm it looks like this $row variable is coming out of the database would I be correct to assume so?
0
 
nizsmoDeveloperCommented:
i read your original question and you did say it was coming out of a database, in that case if you are referring to $row["one"], row["two"] etc....$row["eightytwo"] does that mean that you have 82 columns (fields) in your table? or do you mean 82 rows(records)?
0
 
noel4nopunAuthor Commented:
82 fields
0
 
nizsmoDeveloperCommented:
for one of the fields that doesn't work, eg $row["thirtytwo"] as an example, have you made sure that the value is in fact equal to either "ok", "needs_work", or "not_applicable"?

Alternatively you can do something like this (just temporarily) to catch the variable when it is not equal to any of those 3 options:
<?php
  switch ($row['thirtytwo']){
        case "ok":
        print " OK <input name='one' type='radio' value='ok'checked>
        + work <input name='one' type='radio' value='needs_work'>
        NA <input name='one' type='radio' value='not_applicable'>";
        break;
                
        case "needs_work":
        print " OK <input name='one' type='radio' value='ok'>
        + work <input name='one' type='radio' value='needs_work'checked>
        NA <input name='one' type='radio' value='not_applicable'>";
        break;
                
        case "not_applicable":
        print " OK <input name='one' type='radio' value='ok'>
        + work <input name='one' type='radio' value='needs_work'>
        NA <input name='one' type='radio' value='not_applicable'checked>";
        break;
        
        default:
        echo "Does not match ok, needs_work, or not_applicable";
        break;
        }
?>

Open in new window

0
 
noel4nopunAuthor Commented:
no luck with that solutions either

I tried to put a defualt to the end of the statement earlier as well to no avail.

Also there wouldn't ever really be an "empty" value, it will always have one of those three options...

I was able to get the form to work with an if and else if statement, but that approach didn't work with seperating the needs_work and or not_applicable

I would prefer to use the switch statement approach seems simplier.

But for some reason it doesn't do the first switch then go to the next it just keeps running through until the last one and only prints the last one?...

Was hoping that there was some type of code or procedure I was missing that would "stop" the first switch then run the next one...
0
 
nizsmoDeveloperCommented:
so, to clarify, you are doing ok in printing out the radio buttons? but not selecting them? or do you have trouble printing the radio buttons in the first place?
0
 
noel4nopunAuthor Commented:
they all print but only the last question in the switch statement comes in with an initial value checked
0
 
nizsmoDeveloperCommented:
gooood now i see your problem!

This is because ALL your radio buttons are the same names! if you change the names for every switch statement then all should be alright!

Example code for 2 switch statements, but you have 80 something which is essentially the same...notice that the name="xxxx" is changed for every switch statement, ie in the first switch name="one" and in the second switch the name="two".
switch ($row['thirtytwo']){
        case "ok":
        print " OK <input name='one' type='radio' value='ok'checked>
        + work <input name='one' type='radio' value='needs_work'>
        NA <input name='one' type='radio' value='not_applicable'>";
        break;
                
        case "needs_work":
        print " OK <input name='one' type='radio' value='ok'>
        + work <input name='one' type='radio' value='needs_work'checked>
        NA <input name='one' type='radio' value='not_applicable'>";
        break;
                
        case "not_applicable":
        print " OK <input name='one' type='radio' value='ok'>
        + work <input name='one' type='radio' value='needs_work'>
        NA <input name='one' type='radio' value='not_applicable'checked>";
        break;
        
        default:
        echo "Does not match ok, needs_work, or not_applicable";
        break;
        }
 
        
  switch ($row['fiftyfour']){
        case "ok":
        print " OK <input name='two' type='radio' value='ok'checked>
        + work <input name='two' type='radio' value='needs_work'>
        NA <input name='two' type='radio' value='not_applicable'>";
        break;
                
        case "needs_work":
        print " OK <input name='two' type='radio' value='ok'>
        + work <input name='two' type='radio' value='needs_work'checked>
        NA <input name='two' type='radio' value='not_applicable'>";
        break;
                
        case "not_applicable":
        print " OK <input name='two' type='radio' value='ok'>
        + work <input name='two' type='radio' value='needs_work'>
        NA <input name='two' type='radio' value='not_applicable'checked>";
        break;
        
        default:
        echo "Does not match ok, needs_work, or not_applicable";
        break;
        }
?>

Open in new window

0
 
noel4nopunAuthor Commented:
what a dope I am you are so right.

Thank you very much for your time and I hope these point help move you up in ranks on that leader boards looks like your closing in on RQuadling

good luck and thanks again for your help.

0
 
nizsmoDeveloperCommented:
thank you and glad to help noel4nopun :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 10
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now