我已经花了好几个小时来解决这个问题,并在网上搜索答案,但没有找到任何帮助我的方法。
所以,我正在为酒店网站制作过滤器,酒店搜索的标准之一是酒店所在的区域。我使用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 loop 来构建区变量(变量名末尾的数字将按1递增:$ district1 .. 2 < / strong> .. 3 .. 4 ),
然后将这些分区变量放在 if子句中,该子句位于 while循环内,如下所示:
<?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个区,并且需要花费大量的时间和精力才能对代码进行微小的更改。
如果你帮我解决这个问题并节省我的时间,我会很高兴。谢谢,希望我的问题很清楚。
答案 0 :(得分:0)
作为一种方法有点令人费解,但摆脱eval
的快速解决方案是使用变量变量:
<?php
$district_name = "district$count";
if ($$district_name != 0) { ... }
?>
......虽然更好的解决方案是使用数组并循环遍历它。
答案 1 :(得分: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));
然后删除它以循环它。