Solved

php if parameter question

Posted on 2012-04-05
6
169 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
Independent Software Vendors: 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: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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How Close unsubmited attempts 10 42
php subtract from date 2 26
Increase counter and attr inside a while loop 15 34
Why is my $_POST not going to results page 10 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

679 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