Link to home
Start Free TrialLog in
Avatar of akohan
akohan

asked on

foreach question


Hello Group,

I'm working on my first PHP code and will appreciate if someobody could give me some help.

I have:

$strDays = array (       '','1st', '2nd', '3rd' ,'4th', '5th' , '6th', '7th', '8th', '9th', '10th',
                                      '11th', '12th', '13th' ,'14th', '15th' , '16th', '17th', '18th', '19th', '20th',
                                      '21st', '22nd', '23rd' ,'24th', '25th' , '26th', '27th', '28th', '29th', '30th',
                                      '31st'
                                    );
<? foreach ($strDays as $valDay){ ?>
<option selected><? echo $valDay; ?> </option>
<? } ?>

to populate a drop-down menu by days of month. Then I use it for the 2nd time for a 2nd drop-down menu and it points to 31th day. How  can I reset it back to 1th or first element of the array?

Thanks,
A

ASKER CERTIFIED SOLUTION
Avatar of tolgaong
tolgaong
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akohan
akohan

ASKER


Hello,

Thanks but this is not what I had asked for. How can I reset the variable?

That actually is what you asked for; the only reason the dropdown comes up with 31 selected is because of the "selected" attribute in each of those <option> tags.

The way tolgaong wrote it, the 1st day will be selected.

No part of your code requires any "reset"; foreach doesn't work the same as next() and current() and all those where it's up to you to maintain the internal array pointer.
Avatar of akohan

ASKER


Sorry my bad but I'm using same code for different item but don't have such a problem! can you give me insight into this?

Is it possible the other <select> is overriden by something client-side (perhaps Javascript, or perhaps even your browser filling in a known form value like "date of birth" or you or something)?

Apart from that, if it's identical code it should be behaving identically.
Avatar of akohan

ASKER


Oh I see. Now I get it. I hadn't used "selected" in my previous code. Once again thanks both of you.