在php / mysql的switch case中来自mysql的可变条件? [关闭]

my problem is that i have to implement variable conditions which means that the user is able to define a VALUE for period of time.This data will be stored in mysql. PHP is used on server-side.

i.e.

FROM 2018-01-01 TO 2018-01-03 VALUE 10; 
FROM 2018-01-04 TO 2018-01-06 VALUE 20; 
DEFAULT (used if not in time gap) VALUE 100;

Therefore I thought a switch case can be an option. But is it possible to have a foreach loop on cases?

like:

$date = "2018-01-01";

switch ($date) {
    foreach(... as $data){
    case $data:
        //load variable
        break;
    }
    default:
        //load default values
}

Maybe i'm on the wrong way - pls hlp.

No, your syntax is wrong. In all programming languages that I can think of, you are not allowed to overlap blocks. A switch block can be contained in a loop, or one or more of it's cases' blocks may contain their own loops. (switch is a special case, in that may not have code outside it's case statements.)

You're (in a sense) trying to terminate a for-loop block, inside the block of an if statement within it.

The problem with your code, is that it will be unmaintainable, difficult to understand, and slightly inefficient.

The easiest thing to do, would be to run a cron job or something else which would be periodical and check if today's date falls in the date region defined by your user in the mysql data.

You could simply do with a better SQL statement to grab the data and store it in a slightly different way - for example:

start_date end_date

and then your SQL result would do all the work of your current switch statement (the switch statement won't work as expected anyway).