How to let the switch statement jump out and execute it down

Source: Internet
Author: User
How to let the switch statement jump out and execute it down
$ Type = $ _ POST ['type']; // You do not need to log on to switch ($ type) {case 1: // SQL read data 1 $ acase 2: // SQL read data 2 $ a} if ($ a) {}// if any data is read, an error die is returned; if ($ user) {}// verify whether or not to log on // after logging on, switch ($ type) {case 3: xxxxxxx $ bcase 4: xxxxxxx $ B} if ($ B) {}// if no data is returned after reading the data, the error die is returned. switch ($ type) {case 5: xxxxxxx $ ccase 6: xxxxxxx $ c} if ($ c) {}// if no data is returned after reading the data, the system returns "this item is null". This is not an error.


Now there is a situation
Case 1 2 if $ a xx else 111
Case 3 4 if $ B xx else 222
Case 5 6 if $ c xx else 333
Type = 4, result 111
That is, after jumping out of 4, the intention of judging $ a is to make $ B

I want to determine the corresponding conditions after jumping out.
Of course, you can nest the three if statements, but this means that there may be 1-2 meaningless judgments every time.
Is there any good way to determine the corresponding conditions?


Reply to discussion (solution)

$ Type = $ _ POST ['type']; // $ type can only be a single-value switch ($ type) {case 1: // Generate SQL Command 1 case 2: // Generate SQL Command 2 // SQL read data $ a if ($ a) {}// return error die if no data is returned if the data is read; break; case 3: xxxxxxx $ B case 4: xxxxxxx $ B if ($ B) {}// if no data is returned after reading the data, return the error die; break; case 5: xxxxxxx $ c case 6: xxxxxxx $ c if ($ c) {}// if no data is returned when it is read, "This item is blank" is returned. this is not an error}

$ Type = $ _ POST ['type']; // $ type can only be a single-value switch ($ type) {case 1: // Generate SQL Command 1 case 2: // Generate SQL Command 2 // SQL read data $ a if ($ a) {}// return error die if no data is returned if the data is read; break; case 3: xxxxxxx $ B case 4: xxxxxxx $ B if ($ B) {}// if no data is returned after reading the data, return the error die; break; case 5: xxxxxxx $ c case 6: xxxxxxx $ c if ($ c) {}// if no data is returned when it is read, "This item is blank" is returned. this is not an error}


I tried it.
When type = 4, $ B is used for judgment, but an error is returned.
The data that should have been read
The others also tried to judge that the results always do not meet the conditions.

1 2 OK
But 1 2 does not require logon
In
If ($ ){}
Break;
After
Added if ($ user)
Determine logon
Then case 3
Case 4
If ($ B) {1} else {2}
The result is always 2.

Then describe the rules and paste the real code.

Switch ($ type) {case 1: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); case 2: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); if ($ a) {$ rep ['code'] = 1; $ rep ['valid'] = $ a; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 3: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 4: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ B) {$ rep ['code'] = 1; $ rep ['valid'] = $ B; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 5: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 6: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ c) {$ rep ['code'] = 1; $ rep ['valid'] = $ c; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep );}}


function reponse($rep){echo json_encode($rep); die;}

Switch ($ type) {case 1: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); case 2: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); if ($ a) {$ rep ['code'] = 1; $ rep ['valid'] = $ a; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 3: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 4: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ B) {$ rep ['code'] = 1; $ rep ['valid'] = $ B; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 5: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 6: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ c) {$ rep ['code'] = 1; $ rep ['valid'] = $ c; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep );}}


function reponse($rep){echo json_encode($rep); die;}



Are you sure $ SQL = SQL statement; $ B = mysql_fetch_assoc ($ SQL); can you return the query result?
Otherwise, false is returned.
$b = mysql_fetch_assoc(mysql_query($sql ));

Then describe the rules and paste the real code.


Verify if the login is removed or if the type is 3 4, the system determines that the login is not correct.
Then, what we read from the 5 6 s is that the array is read from both the 5 S and the 6 s after the while loop ..
That is, the while loop is not included in the case...

What are the meanings of type 1, 2, 3, 4?
Is the SQL commands of case 3 and case 4 the same?


Switch ($ type) {case 1: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); case 2: $ SQL = SQL statement $ a = mysql_fetch_assoc ($ SQL); if ($ a) {$ rep ['code'] = 1; $ rep ['valid'] = $ a; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 3: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 4: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ B) {$ rep ['code'] = 1; $ rep ['valid'] = $ B; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep);} break; case 5: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL ); case 6: $ SQL = SQL statement $ B = mysql_fetch_assoc ($ SQL); if ($ c) {$ rep ['code'] = 1; $ rep ['valid'] = $ c; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep );}}


function reponse($rep){echo json_encode($rep); die;}



Are you sure $ SQL = SQL statement; $ B = mysql_fetch_assoc ($ SQL); can you return the query result?
Otherwise, false is returned.
$b = mysql_fetch_assoc(mysql_query($sql ));


Oh, there is mysql_query in SQL.
There are some in the code. after all, both of them are read.
I accidentally missed it here.

What are the meanings of type 1, 2, 3, 4?
Is the SQL commands of case 3 and case 4 the same?


1 is the homepage information 2 is a details information that does not need to be logged on
3: basic personal information 4: different real-name information for reading different fields in the same table
All are single data records
5. capital log 6. point log
The array is read in the while loop.

It should be written like this.

Switch ($ type) {case 1: $ SQL = SQL statement 1 break; case 2: $ SQL = SQL statement 2 break; case 3: $ SQL = SQL statement 3 break; case 4: $ SQL = SQL statement 4 break; case 5: $ SQL = SQL statement 5 break; case 6: $ SQL = SQL statement 6 break ;} $ a = mysql_fetch_assoc ($ SQL); if ($ a) {$ rep ['code'] = 1; $ rep ['valid'] = $; reponse ($ rep);} else {$ rep ['code'] = 1008; $ rep ['error'] = 'Error parameters '; reponse ($ rep );}

$fun = switch_.$_GET['type'];$fun();function switch_1(){a(true);}function switch_2(){a(false);}function a($condition){if(!$condition){die('error');}echo __Method__;}function switch_3(){b(true);}function switch_4(){b(false);}function b($login){if(!$login){die('error');}echo __Method__;}function switch_5(){c(true);}function switch_6(){c(false);}function c($condition){if(!$condition){die('error');}echo __Method__;}

This is acceptable, but there is a small problem.
That is, data is returned for success.
However, the error message to be returned for failure is different. the error message must be null but not an error.
So to fully implement it, we have to make multiple judgments. I think this is definitely not the best.
I have made some adjustments and the overall operation is okay, but there should be some optimizations.
By the way, it is quite troublesome for me to write 16 functions in case, but I learned this method.
Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.