Adobe Dreamweaver Forums



Last 10 THreads :         Problem fetching policy-file-request (Last Post : greenx - Replies : 0 - Views : 1 )           »          Coldfusion Login (Last Post : ProjectedSurplus - Replies : 0 - Views : 1 )           »          FTP Error Occurred - Cannot make connection (Last Post : ostephy - Replies : 2 - Views : 3 )           »          Opening a flash movie in dreamweaver (Last Post : Nancy O - Replies : 1 - Views : 3 )           »          Re: Unable to authenticate installer (Last Post : D Sparks - Replies : 1 - Views : 2 )           »          Microphone in the latest beta (Last Post : 0x656b694d - Replies : 0 - Views : 1 )           »          Please help me (Last Post : HalfNelson - Replies : 0 - Views : 1 )           »          Re: Fireworks screws up colors from photoshop (Last Post : chirp88 - Replies : 5 - Views : 6 )           »          CFdirectory recurse error (Last Post : Adam Cameron - Replies : 1 - Views : 2 )           »          Coldfusion Login (Last Post : ProjectedSurplus - Replies : 0 - Views : 1 )           »         


Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
User Info Statistics
Go Back   Adobe Dreamweaver Forums > Dreamweaver: Main > Dreamweaver Application Development
 
Tags: , , ,

Reply
  #1 (permalink)  
Old 07-23-2008, 02:08 PM
Brett
 
Posts: n/a
Diggs:
Default Please help with tricky array question

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




Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-23-2008, 02:42 PM
Michael Fesser
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

.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
Reply With Quote
  #3 (permalink)  
Old 07-23-2008, 04:17 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

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

Reply With Quote
  #4 (permalink)  
Old 07-23-2008, 08:22 PM
Michael Fesser
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

.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
Reply With Quote
  #5 (permalink)  
Old 07-24-2008, 01:23 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

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

Reply With Quote
  #6 (permalink)  
Old 07-24-2008, 08:18 PM
Michael Fesser
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

.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
Reply With Quote
  #7 (permalink)  
Old 07-24-2008, 09:24 PM
Gary White
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

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
Reply With Quote
  #8 (permalink)  
Old 07-25-2008, 01:05 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

Gary,

> One small point of order is that neither function contains an
> underscore character.



You're right, but then, these small inaccuracies don't really matter... :-)
Reply With Quote
  #9 (permalink)  
Old 08-21-2008, 10:17 PM
Brett
 
Posts: n/a
Diggs:
Default Michael Fesser if you are there please help

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

Reply With Quote
  #10 (permalink)  
Old 08-22-2008, 06:53 PM
Brett
 
Posts: n/a
Diggs:
Default Re: Michael Fesser if you are there please help

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.


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
All times are GMT. The time now is 10:38 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Inactive Reminders By Mished.co.uk