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
Solved

php if parameter question

Posted on 2012-04-05
6
167 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
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:MadShiva
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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:MadShiva
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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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