Solved

multiple switch statements

Posted on 2007-11-16
22
301 Views
Last Modified: 2008-03-06
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
Comment
Question by:noel4nopun
  • 10
  • 10
  • 2
22 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302408
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
 
LVL 28

Expert Comment

by:gamebits
ID: 20302422
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
 

Author Comment

by:noel4nopun
ID: 20302711
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302729
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
 

Author Comment

by:noel4nopun
ID: 20302902
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
 

Author Comment

by:noel4nopun
ID: 20302908
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302921
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
 
LVL 28

Expert Comment

by:gamebits
ID: 20302941
I'd like to see the form that is calling this script.
0
 

Author Comment

by:noel4nopun
ID: 20302944
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
 

Author Comment

by:noel4nopun
ID: 20302951

<?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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302968
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:noel4nopun
ID: 20302980
yeah no matter which one i try, it only works for the last entry...
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302982
hmm it looks like this $row variable is coming out of the database would I be correct to assume so?
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302985
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
 

Author Comment

by:noel4nopun
ID: 20302994
82 fields
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20303003
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
 

Author Comment

by:noel4nopun
ID: 20303024
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20303039
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
 

Author Comment

by:noel4nopun
ID: 20303049
they all print but only the last question in the switch statement comes in with an initial value checked
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20303057
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
 

Author Comment

by:noel4nopun
ID: 20303066
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20303069
thank you and glad to help noel4nopun :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now