Solved

php if parameter question

Posted on 2012-04-05
6
164 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:Atique Ansari
Comment Utility
Replace select_box_name with your drop down's attribute.

Let me know if need more details.
0
 
LVL 10

Expert Comment

by:MadShiva
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now