Jump to content
Larry Ullman's Book Forums

Recommended Posts

I must be dumb.

Similar code worked previously when making another drop-down list, however it does not work in the format below.

 

Any help will be appreciated.

 

<?php
$DayCommenced= 16;
$day=range(01,31);

echo"<label>Commenced</label><br />";
echo"<select name=\"DayCommenced\">";
foreach($day as $value){
if($day==$DayCommenced){
$selected = "selected";
}else{
$selected = "";
}
echo"<option value=\"$value\" $selected>$value</option>";
}
echo "</select>";
?>

Share this post


Link to post
Share on other sites

sandari, yes, we discussed this in another topic, and you refused to listen to Larry's advice. That is why it's not working.

Do yourself a favor and run your script as is, and then look at the HTML that is generated.

Hopefully, you're very quickly see the problem with your HTML.

Share this post


Link to post
Share on other sites

OK, maybe I didn't get exactly what you and Larry were saying. However, the previous was resolved quite easily as indicated in my last post in that thread.

I have pulled the date from the DB in a single query at the start of this script and exploded the date into $DayCommenced, $MonthCommenced & $YearCommenced.

The values are all correct right up until the lines quoted above.

I think my problem is that I do not fully understand how to evaluate the individual keys in the $day array to be able to compare them to $DayCommenced, if, in fact, $day is an array.

Please explain in detail.

 

Sorry to be so thick on this one.

Share this post


Link to post
Share on other sites

Nah, I don't mind you asking for additional help.

It's just that I was hoping that after making the additional effort, you would be able to solve the problem on your own.

 

There are two issues at play here. The obvious error is that your if condition within the foreach loop is wrong. You should be using $value, not $day.

 

Another potential issue though is related to how you're handling the "selected" part.

As Larry said in your previous post, the way you're coding that part can result in issues in certain browsers.

With that said, the way you're coding the selected part here is different than the way you did in the previous post that Larry commented on, but all the same, both can be problematic.

 

Let's think about the HTML being generated by your PHP for a second (assuming the if condition is fixed). If a day in the drop-down list is not the day commenced, then you will get HTML like the following:

 

<option value="value" >

 

However, if the day is the day commenced, then you'll get HTML like the following:

 

<option value="value" selected>

 

If you are using XHTML, then both would be regarded as invalid, as thus your page will not work (and that's even aside from the problematic if condition).

Even if you're using only HTML, depending on the browser, the above might not work because the browser might not understand "selected" by itself.

 

A better solution would be something like the following:

 

<?php

 $DayCommenced = 16;

 $day = range(1, 31);

 echo '<label>Commenced</label>';

 echo '<select name="DayCommenced">';

 foreach ($day as $value) {

   echo '<option value="' . $value . '"';

   if ($value === $DayCommenced) {

     echo ' selected="selected"';

   }

   echo '>' . $value . '</option>';

 }

 echo '</select>';

 

Several things to note:

 

1) I cleaned your code up quite a bit. It's good practice to use consistent spacing for everything. Notice the inconsistent spacing in your code around the equals signs, etc.

 

2) I prefer single quotes for echoing HTML in most cases (mainly because escaping double quotes always confuses me). This is a preference on my part and not required by you though.

 

3) The <br /> tag in your code is not semantic. If you want a line break, you should code it by using CSS, not by using non-semantic HTML.

 

4) I fixed the incorrect if condition.

 

5) Notice how I echoed the HTML out to the screen. With my code ' selected=""selected"' is placed in the HTML, which is what you want as it creates 100% valid (X)HTML in all browsers.

 

Please let me know what you think.

Thank you.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks HartleySan,

it works really well, particularly after I changed:

       if ($value === $DayCommenced) {

 

to

       if ($value == $DayCommenced) {

 

or did you put that in there to see if I was reading everything? :-)

 

Anyway, all is good now. Thanks again for all your help.

Share this post


Link to post
Share on other sites

hi 

 

i have a requirement  , reports display  with php and mysql

 

i have a dropdown with options- today ,weekly,between and beside one submit button

 

when i click on submit it should display records created on this date (today,weekly)

between option -need to show 2 boxes to select date 

 

can any one help this code. waiting for reply .thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×