Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

foreach question

Posted on 2006-11-10
6
Medium Priority
?
301 Views
Last Modified: 2006-11-18

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

0
Comment
Question by:akohan
[X]
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
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
tolgaong earned 200 total points
ID: 17916978
<html>
<body>
<?php
$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'
                              );
?>
<select name="yourdates">                                           
<?php foreach ($strDays as $key=>$valDay){ ?>
<option <?php if ($key==1) echo "selected"?> ><?php echo $valDay; ?> </option>

<?php
 }
?>
</select></body></html>
0
 

Author Comment

by:akohan
ID: 17917148

Hello,

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

0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17917294
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:akohan
ID: 17917522

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?

0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17917549
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.
0
 

Author Comment

by:akohan
ID: 17917565

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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

721 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