0

我已经花了几个小时解决这个问题,并在网上搜索答案,但没有找到任何对我有帮助的东西。

所以,我正在为一个酒店网站做一个过滤器,酒店搜索的标准之一是酒店所在的地区。我使用 GET 方法:

<?php if (isset($_GET['District1'])){
    $district1= ($_GET["District1"]);
    }else ($district1=0);
if (isset($_GET['District2'])){
    $district2= ($_GET["District2"]);
    }else ($district2=0);
if (isset($_GET['District3'])){
    $district3= ($_GET["District3"]);
    }else ($district3=0);
if (isset($_GET['District4'])){
    $district4= ($_GET["District4"]);
    }else ($district4=0);
?>

其中 $district1, $district2, $district3, $district4 是城市的 4 个不同的区。

我要做的是使用for 循环构建区域变量(变量名称末尾的数字将按 1 递增: $district1.. 2 .. 3 .. 4),

然后将这些区域变量放在while 循环内的if 子句中,如下所示:

<?php 
$districts_filter_query="SELECT * FROM districts_table ";
        $districts_filter_result = mysql_query($districts_filter_query, $connect);
        $districts_filter_row = mysql_fetch_array($districts_filter_result);
        for($count = 1; $count <= 4 ; $count++){

do {

        $district_name = $districts_filter_row['district_name'];
        $district_value = $districts_filter_row['district_value'];



    echo '
           <label for="group13"> <p ';  ?> <?php 
            if ( eval(' return $district'.$count.';') !=0){echo 'class="greenBG"';}
                    else {echo ' class="checkbox" ';}?> <?php echo ' >
                    <input type="checkbox" name="District'.$count.'" id="group13" value="'.$district_value.'"
                         onClick="this.form.submit();" ';?> 
                    <?php if ( eval (' return $district'.$count.';') !=0){echo ' checked ';};?><?php echo '/> '.$district_name.' 
                </p>
           </label> ';

    }
    while($districts_filter_row = mysql_fetch_array ($districts_filter_result));
    }
?>

问题出在这里,我猜:eval('return $district'.$count.';'),通过这个我尝试构造区变量名。但它不起作用。

如果我使用此代码,一切都会完美运行:

            <label for="group1"> <p <?php 
            if ($district1!=0){echo 'class="greenBG"';}
                    else echo 'class="checkbox"';?>>
                    <input type="checkbox" name="District1" id="group1" value='1013' onClick="this.form.submit();" 
                    <?php if ($district1!=0){echo 'checked ';};?>/> district1
                </p>
           </label>
            <label for="group2"> <p <?php 
             if ($district2!=0){echo   'class="greenBG"';}
                    else echo 'class="checkbox"';?>>
                    <input type="checkbox" name="District2" id="group2" value='1014' onClick="this.form.submit();" 
                    <?php if ($district2!=0){echo 'checked ';};?>/> district2
                </p>
           </label>
            <label for="group3"> <p <?php 
            if ($district3!=0){echo 'class="greenBG"';}
                    else echo 'class="checkbox"';?>>
                    <input type="checkbox" name="District3" id="group3" value='1015' onClick="this.form.submit();" 
                    <?php if ($district3!=0){echo 'checked ';};?>/> district3
                </p>
           </label>
             <label for="group4"> <p <?php 
            if ($district4!=0){echo 'class="greenBG"';}
                    else echo 'class="checkbox"';?>>
                    <input type="checkbox" name="District4" id="group4" value='1016' onClick="this.form.submit();" 
                    <?php if ($district4!=0){echo 'checked ';};?>/>  district4
                </p>
           </label>

但这不适合我,因为有30多个区,对代码进行任何细微的更改都需要花费大量的时间和精力。

如果您能帮我解决这个问题并节省我的时间,我将不胜感激。谢谢,希望我的问题足够清楚。

4

2 回答 2

0

that's a little convoluted as an approach, but a quick solution to get rid of eval is to use variable variables:

<?php
$district_name = "district$count";
if ($$district_name != 0) { ... }
?>

... though a better solution would be to just use an array and loop through it.

于 2013-04-17T21:38:38.263 回答
0

使用http://php.net/manual/en/language.variables.variable.php而不是一些难看的eval(). (例如${"district$count"}

$count = 0;
do {
    $count++;
    $district_name = $districts_filter_row['district_name'];
    $district_value = $districts_filter_row['district_value'];
?>
<label for="group<?php echo $count; ?>">
    <p <?php 
        if (${"district$count"} != 0) echo 'class="greenBG"';
        else echo 'class="checkbox"'; ?>>
        <input type="checkbox" name="District<?php echo $count; ?>" id="group<?php echo $count; ?>" value='<?php echo $district_value; ?>' onClick="this.form.submit();" 
        <?php if (${"district$count"} != 0) echo 'checked '; ?>/> <?php echo $district_name; ?>
    </p>
</label>
<?php
}
while($districts_filter_row = mysql_fetch_array ($districts_filter_result));

然后删除这个for循环。

于 2013-04-17T21:40:18.817 回答