• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

php if parameter question

Hi there, is there a simpler way of writing the below?

   
<option <? if ($nights == "1") echo "selected=\"selected\""; ?> value="1">1</option>
  	<option <? if ($nights == "2") echo "selected=\"selected\""; ?> value="2">2</option>
  	<option <? if ($nights == "3") echo "selected=\"selected\""; ?> value="3">3</option>
  	<option <? if ($nights == "4") echo "selected=\"selected\""; ?> value="4">4</option>
  	<option <? if ($nights == "5") echo "selected=\"selected\""; ?> value="5">5</option>
  	<option <? if ($nights == "6") echo "selected=\"selected\""; ?> value="6">6</option>

Open in new window

0
GamesNET2
Asked:
GamesNET2
1 Solution
 
GamesNET2Author Commented:
what i'd really like to do as well is say that if $nights hasn't been defined, default it to 7
0
 
TobiasCommented:
Dear,

Something like :

if($nights=="")
{
$nights = 7;
}

for($j=0; $j <= 7 ; $j++){
if ($nights == $j)
{
echo"<option value='".$j."'>".$j."</option>";
}
else
     {
     echo"<option value='".$j."' selected=\"selected\">".$j."</option>";
     }
}

Open in new window

0
 
Atique AnsariCommented:
You can use jquery to select the option.

<select name="select_box_name" id="select_box_name">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<script type="text/javascript" language="javascript">
      $("select#select_box_name option[value='<?php echo $nights;?>']").attr("selected", "selected");
</script>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Atique AnsariCommented:
Replace select_box_name with your drop down's attribute.

Let me know if need more details.
0
 
TobiasCommented:
if($nights==""){$nights = 7;}

for($j=0; $j <= 7 ; $j++){
if ($nights == $j)
{
echo"<option value='".$j."' selected=\"selected\">".$j."</option>";
}
else
     {
     echo"<option value='".$j."'>".$j."</option>";
     }
}

echo"</select>";
?>

Open in new window


I have tested and I inverted. I post the code corrected.
0
 
eechincsCommented:
Hi GamesNET2,

Please refer to the following code.Please note that the loop should be start from 1 and end by 6.

<?php
$nights = isset($night)?$night:7;
$str_selected = '';
echo '<select name="select_night">';
for($i = 1; $i<=6;$i++){
      if($nights == $i)  $str_selected = 'selected';                  
      else $str_selected = '';
      ?>
            <option value="<?php echo $i;?>" <?php echo $str_selected;?>  ><?php echo $i?></option>
      <?php
}
echo '</select>';
?>
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now