How to execute specific code based on parameters in php-php Tutorial

Source: Internet
Author: User
How to execute specific code with parameters in php according to parameters, for example, I have a webpage
Test. php
Is there a parameter test. php? 1, test. php? 2, test. php? 3, test. php? 4, test. php? 5,

Can I get the url? Parameters.
To execute the specific code. Find the code .. It seems to be different from asp ..

Obtain parameters.
Then
If = 1
Execute 1 related code

If the value is equal to 2, execute the related code of 2.
..................
If the value is 5, run the related code of 5.

If none of them are equal to 12345, a Parameter error is prompted.
Please code, myself, Cainiao ..


Reply to discussion (solution)

Don't understand what you mean
What do you do in asp?

I guess the author's asp website has been rewritten, "?" The following values may be category = 1, 2, 3..., so you can judge $ _ GET ['Category '] =? You can run the specified code. I don't know if my guess is correct.
Hope the answer is helpful to you.

I guess the author's asp website has been rewritten, "?" The following values may be category = 1, 2, 3..., so you can judge $ _ GET ['Category '] =? You can run the specified code. I don't know if my guess is correct.
Hope the answer is helpful to you.

For example, I want to call the data of each column on the website.
I only want to use a php page, and then use the id parameter to identify the content of different columns, and then use the id parameter for output ..?


Don't understand what you mean
What do you do in asp?
For example, I want to call the data of each column on the website.
I only want to use a php page, and then use the id parameter to identify the content of different columns, and then use the id parameter for output ..?

If url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. assume that the obtained parameter is $ actionswitch ($ action) {case '1 ':...; break; case '2 ':...; break; case '3 ':...; break; case '4 ':...; break; case '5 ':...; break; default :...; break ;}

For example, I want to call the data of each column on the website.
I only want to use a php page, then use the id parameter to identify the content of different topics, and then use the id ......
You can use different IDs and parameters to combine different query conditions, so you can get different data and columns. Of course you have used the database.

I understand what the landlord means.
Http: // localhost/test. php? 2
This url is used
$ Id = key ($ _ GET );
Get ID number

Http: // localhost/test. php? Id = 2
This url is used
$ Id = $ _ GET ['id'];
Get ID number


PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. assume that the obtained parameter is $ actionswitch ($ action) {case '1 ':...; break; case '2 ':...; break; case '3 '......

$ Action = $ _ SERVER ['query _ string'];
Switch ($ action)
{
Case '1':...; break;
Case '2':...; break;
Case '3':...; break;
Case '4':...; break;
Case '5':...; break;
Default:...; break;
}
?>

In this way, an error is reported ..
Online seeking complete demo code, previously engaged in asp, thanks...

Reference the reply from zhangbin1988 on the fifth floor: PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. assume that the obtained parameter is $ actionswitch ($ action) {case '1 ':...; break; case ......

If the id is 1, the output echo is column 1.

If the id is 2, the output echo is column 2.

Complete Code... I have never written php before
, The format and syntax are still learning...

I learned a function key from the moderator.

I understand what the landlord means.
Http: // localhost/test. php? 2
This url is used
$ Id = key ($ _ GET );
Get ID number

Http: // localhost/test. php? Id = 2
This url is used
$ Id = $ _ GET ['id'];
Get ID number

Reference the reply from zhangbin1988 on the fifth floor: PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. assume that the obtained parameter is $ actionswitch ($ action) {case '1 ':...; break; case ......

If you copy the file directly, you will surely report an error... it was a mess I wrote. replace it with your logic code !!!

Reference the reply from phpcainiao on the eighth floor: reference the reply from zhangbin1988 on the fifth floor: PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. if the obtained parameter is $ actionswitch ($ action) {ca ......

For the complete code format, thank you...

If the id is 1, the output echo is column 1.

If the id is 2, the output echo is column 2.

Complete Code... I have never written php before
, The format and syntax are still learning...

Reference the reply from phpcainiao on the eighth floor: reference the reply from zhangbin1988 on the fifth floor: PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. if the obtained parameter is $ actionswitch ($ action) {ca ......

My asp, your logic, is also written in this way. now I guess there are still some syntaxes and formats I am not right about. ,.

For example, I have a webpage
Test. php
Is there a parameter test. php? 1, test. php? 2, test. php? 3, test. php? 4, test. php? 5,

Can I get the url? Parameters.
To execute the specific code. Find the code .. It seems to be different from asp ..

Obtain parameters.
Then
If = 1
Execute 1 related code

If the value is equal to 2, execute the related code of 2.
...............


$ Class = $ _ SERVER ['query _ string'];
Switch ($ class)
{
Case 1:
Echo "Number 1 ";
Break;
Case 2:
Echo "Number 2 ";
Break;
Case 3:
Echo "Number 3 ";
Break;
Default:
Echo "No number between 1 and 3 ";
}
A little eye-catching ..

Reference 11 floor zhangbin1988 reply: reference 8 floor phpcainiao reply: reference 5 floor zhangbin1988 reply: PHP code? 123456789101112 if url rewriting is not used, use $ _ SERVER ['query _ string'] to obtain parameters such as 1, 2, and 3. if url rewriting is used, use $ _ GET ['parameter name'] to obtain parameters such as 1, 2, and 3. if the obtained parameter is $ ......

Case '1': echo 'column 1'; break; you can also replace echo 'column 1' with the corresponding logic. you can only write it yourself.

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.