![]() |
![]() |
||||||
|
|||||||
| Tags: array, help, question, tricky |
![]() |
|
|||
|
Hi All,
I have created a page to update an event record and I'm trying to populate the drop-down option list for $months with the value stored in the record. I have created a $mysqldate array to separate the different elements of the date as stored in the table, for insertion into the various parts of the form. For brevity sake, I'm not showing all of the code. > $months = > array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec'); > $mysqldate = explode('-', $row_getEvents['event_date']); > <select name="month" id="month"> > <?php for ($i=1;$i<=12;$i++) { ?> > <option value="<?php echo $i <10 ? '0'.$i : $i; ?>" > <?php echo ' selected="selected"'; ?>><?php echo > $months[$i-1]; ?> </option> > <?php } ?> > </select> > <label for="day">Date:</label> > <input type="text" name="day" id="day" size="2" maxlength="2" > value="<?php echo $mysqldate[2]; ?>" /> > <label for="year">Year:</label> > <input type="text" name="year" id="year" size="4" > maxlength="4" value="<?php echo $mysqldate[0]; ?>"/> The problem is I can't figure out how to take the value $mysqldate[1] (which is the value for month) and have it display as the default month for this record, while still having all months available in the drop-down. The code as I have it includes all months, but the default is Dec. I have changed <?php echo $months[$i-1]; ?> to this <?php echo $months[$mysqldate[1]-1]; ?> and get the correct month as default, but that turns all twelve options into the same month, e.g. twelve Aug's. I probably need a conditional statement, but I don't know what the condition is or should be. So how do I set the record's month as the default choice, while preserving the other eleven choices in the drop-down list? TIA Brett |
| Sponsored Links |
|
|||
|
.oO(Brett)
>I have created a page to update an event record and I'm trying to >populate the drop-down option list for $months with the value stored in >the record. I have created a $mysqldate array to separate the different >elements of the date as stored in the table, for insertion into the >various parts of the form. For brevity sake, I'm not showing all of the >code. > >> $months = >> array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec'); >> $mysqldate = explode('-', $row_getEvents['event_date']); >> <select name="month" id="month"> >> <?php for ($i=1;$i<=12;$i++) { ?> >> <option value="<?php echo $i <10 ? '0'.$i : $i; ?>" >> <?php echo ' selected="selected"'; ?>><?php echo >> $months[$i-1]; ?> </option> >> <?php } ?> >> </select> >> <label for="day">Date:</label> >> <input type="text" name="day" id="day" size="2" maxlength="2" >> value="<?php echo $mysqldate[2]; ?>" /> >> <label for="year">Year:</label> >> <input type="text" name="year" id="year" size="4" >> maxlength="4" value="<?php echo $mysqldate[0]; ?>"/> > >The problem is I can't figure out how to take the value $mysqldate[1] >(which is the value for month) and have it display as the default month >for this record, while still having all months available in the >drop-down. The code as I have it includes all months, but the default >is Dec. I have changed <?php echo $months[$i-1]; ?> to this <?php echo >$months[$mysqldate[1]-1]; ?> and get the correct month as default, but >that turns all twelve options into the same month, e.g. twelve Aug's. I >probably need a conditional statement, but I don't know what the >condition is or should be. So how do I set the record's month as the >default choice, while preserving the other eleven choices in the >drop-down list? Try this for printing the drop-down options (I don't like the many switchings between PHP and HTML mode, so this is just a single PHP block), slightly restructured for legibility: <?php $months = array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ); $mysqldate = explode('-', $row_getEvents['event_date']); print "<select name='month' id='month'>\n"; for ($i = 1; $i <= 12; $i++) { printf("<option value='%u'%s>%s</option>\n", $i, $i == $mysqldate[1] ? " selected='selected' : '', $months[$i-1] ); } print "</select>\n"; ?> One note: I've removed the leading zeros in the option values. Working with leading zeros if the values are in fact purely numeric can be troublesome. If your form processing script needs those zeros, use this format string for the printf() function instead: "<option value='%02u'%s>%s</option>\n" Another note: You could also use MySQL's DAY(), MONTH() and YEAR() functions to let the database return the parts of the date. Then you wouldn't have to explode() it in your script. Usually I find it easier and more convenient to let the DB already do as much work as possible and deliver the data in the format that I need. HTH Micha |
|
|||
|
Micha,
As always, thanks very much for your help. I had a little problem with the code as you posted it - I was getting errors - so after fiddling with it some, I got it to work. I hope I didn't do something I shouldn't have. The change I made was in the line with 'selected'. I put a closing double quote just before the colon and two single quotes after it. It seems to be working just fine. Did I do something wrong? > <?php print "<select name='month' id='month'>\n"; > for ($i = 1; $i <= 12; $i++) { > printf("<option value='%02u'%s>%s</option>\n", > $i, > $i == $mysqldate[1] ? " selected='selected' ": '', > $months[$i-1] > ); > } > print "</select>\n"; ?> Also Micha, I'm not completely clear on how this code works, particularly <option value='%02u'%s>%s</option>. If it's not too much trouble would you explain how that works? Thanks again. Best, Brett |
|
|||
|
.oO(Brett)
>I had a little problem with the code as you posted it - I was getting >errors I admit - it was untested. ![]() > - so after fiddling with it some, I got it to work. I hope I >didn't do something I shouldn't have. The change I made was in the line >with 'selected'. I put a closing double quote just before the colon >and two single quotes after it. It seems to be working just fine. Did >I do something wrong? Nope, looks correct. The single quotes were already there, it was just the double quote before the colon that was missing. So the string wasn't closed and the parser complained somewhere later. >Also Micha, I'm not completely clear on how this code works, >particularly <option value='%02u'%s>%s</option>. If it's not too much >trouble would you explain how that works? The printf()/sprintf() functions take a format string as the first parameter and an arbitrary number of additional arguments. The format string may contain placeholders, which are then replaced by the actual values. In the example above there are three placeholders in the string (the '%' thingies) and three additional arguments passed to the function after the format string, which will replace the placeholders in the given order. There are various types of placeholders. The %s denotes strings, they are simply replaced without modifications. In this case the first %s placeholder becomes " selected='selected'" if the condition matches, the second %s becomes the name of the month, taken from the $months array. The very first placeholder %u is different, it stands for an unsigned integer. Whatever you pass as an argument to the function to replace this %u will be treated as an unsigned integer. In this case it's just the loop variable $i. In addition to these various types of placeholders there are some options you can use. The %u placeholder for example can be extended to output the value with any arbitrary number of leading spaces or zeros. The %02u in my second example will output the value $i as an unsigned integer with at least two digits, left-padded with zeros if necessary. Probably it sounds more complicated than it actually is. ![]() In short: printf()/sprintf() are really powerful functions when it comes to the creation of strings with a lot of embedded variables or, as in the examples above, even more complex expressions. The alternative would be to use a lot of string concatenation, but usually these functions lead to better readable code and are easier to maintain or extend. Have a look at the PHP manual for more options and examples. http://www.php.net/sprintf >Thanks again. You're welcome. Micha |
|
|||
|
Micha,
Thanks for the explanation, you should consider doing the documentation for PHP. I went to the PHP.net site and read both print_f() and sprint_f() but it was not nearly as clear as your explanation - which is why I asked you about it. In fact, the documentation at the PHP.net site is considered abysmal by many newcomers, myself included. Yes, based on your explanation, it seems that print_f() and sprint_f() are very powerful functions that I would be well advised to learn more fully. Thanks for taking the time to answer so completely. The great thing is, every time I ask a question that is answered by someone much more knowledgeable than I, I learn not just the answer to that particular question, but the solution to future questions too. A case in point. David Powers answered a previous post and included some code for strrpos () and substr(), both of which I used to solve a new problem. It is invaluable to rookies to have such a great resource in the very generous veterans. Thanks to you all. Best, Brett |
|
|||
|
.oO(Brett)
>Thanks for the explanation, you should consider doing the documentation >for PHP. I went to the PHP.net site and read both print_f() and >sprint_f() but it was not nearly as clear as your explanation - which is >why I asked you about it. In fact, the documentation at the PHP.net >site is considered abysmal by many newcomers, myself included. Actually, many (most?) developers including myself consider the PHP manual quite good, if not one of the best if compared to the other "competitors". Remember: it's mostly a reference manual, not a tutorial, even though it contains some introduction chapters. The only thing I don't like (yet) is the fundamental change in the chapter structure some weeks ago. On my local server I still have the older version, while in the online manual now I might have to search a while to find the chapter I'm looking for ... >Yes, based on your explanation, it seems that print_f() and sprint_f() >are very powerful functions that I would be well advised to learn more >fully. Indeed. >Thanks for taking the time to answer so completely. You're welcome. I'm glad it was helpful. Micha |
|
|||
|
On Thu, 24 Jul 2008 08:58:49 -0400, Brett <nospam@address.withheld>
wrote: >I went to the PHP.net site and read both print_f() and >sprint_f() One small point of order is that neither function contains an underscore character. The function names are printf() and sprintf(). I know it's probably a simple typo, but for anyone else reading this they might be pulling their hair out looking for print_f() or sprint_f(). ;-) Gary |
|
|||
|
Hi Micha,
Sorry for waking you. You helped me once before on this and I'm hoping you can help me again. I have decided to change all of the date fields on my update page to drop down lists, and using your technique I have successfully done the month and day fields. The problem I'm having is with the year field. I have an array $years = range(2006, 2016) to populate the list, but what is being saved on update is the key not the value. Here's the code: > <label for="select">Year:</label> > <?php print "<select name='year' id='year'>\n"; > for ($i = 1; $i <=9; $i++) { > printf("<option value='%4u',%s>%s</option>\n", > $i, > $i == $mysqldate[0] ? " selected='selected' ": '', > $years[$i-1] > ); > } > print "</select>\n"; ?> After updating when I view the record list I see Aug. 21, 0003. I have been scratching my head over this one trying to figure out how to get the value not the key saved in the table. What am I doing wrong? Thanks for your help, again. Brett |
|
|||
|
I got it sorted out.
> <select name="year" id="year"> > <?php > foreach ($years AS $year) { > print "<option value=\"$year\" "; > if ($year == $now['year']) { print ' > selected="selected" '; } > print ">$year</option>\n"; > } > ?> > </select> I went to a foreach which was easier. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise