php - if statement does not work inside while loop which is inside for loop -


i've spent hours on problem , , searched answer online, found nothing helped me.

so, making filter hotel website, , 1 of criteria hotel search district hotel located. use method:

<?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); ?> 

where $district1, $district2, $district3, $district4 4 different districts of city.

what trying build district variables using for loop (the digit @ end of variable name ascending 1 : $district1..2..3..4),

then place these district variables in if clause inside while loop follows:

<?php  $districts_filter_query="select * 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++){  {          $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));     } ?> 

the problem here, guess: eval(' return $district'.$count.';'), try construct district variable names. not work.

everything works if use code:

            <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> 

but not suitable me because there more 30 districts, , take lot of time , effort make minor change code.

i greatful if me solve problem , save time. thanks, hope question clear enough.

use http://php.net/manual/en/language.variables.variable.php instead of ugly eval(). (for example ${"district$count"})

$count = 0; {     $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)); 

and delete loop around it.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -