?
Solved

php if parameter question

Posted on 2012-04-05
6
Medium Priority
?
178 Views
Last Modified: 2012-05-24
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
Comment
Question by:GamesNET2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Author Comment

by:GamesNET2
ID: 37810652
what i'd really like to do as well is say that if $nights hasn't been defined, default it to 7
0
 
LVL 10

Expert Comment

by:Tobias
ID: 37810812
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
 
LVL 7

Expert Comment

by:Atique Ansari
ID: 37810868
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Atique Ansari
ID: 37810913
Replace select_box_name with your drop down's attribute.

Let me know if need more details.
0
 
LVL 10

Expert Comment

by:Tobias
ID: 37811138
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
 
LVL 2

Accepted Solution

by:
eechincs earned 2000 total points
ID: 37815666
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question