sam20e Posted September 8, 2012 Share Posted September 8, 2012 Hi I have one date field drop down : $day = $_POST['day']; $mon = $_POST['month']; $year = $_POST['year']; $dob = $year . "-" . $mon . "-" . $day; $ins = array( 'd_dob' => $dob ); <tr> <td align="left" valign="middle">Date Of Birth<span class="astrikrequired">*</span>: </td> <td align="left" valign="middle"> <select name="day" id="day" class="tyextfild validate-selection required"> <?php if($day == '') { $day=date(d, strtotime($d_dob)); } $gnrl->getDropdownList(array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'),$day); ?> </select> <select name="month" id="month" class="tyextfild validate-selection required"> <?php if($month == '') { $month=date(m, strtotime($d_dob)); } ?> <option value="01" <?php if(in_array('01',explode(',',$month))) echo 'selected="selected"';?> >Jan</option> <option value="02" <?php if(in_array('02',explode(',',$month))) echo 'selected="selected"';?>>Feb</option> <option value="03" <?php if(in_array('03',explode(',',$month))) echo 'selected="selected"';?>>Mar</option> <option value="04" <?php if(in_array('04',explode(',',$month))) echo 'selected="selected"';?>>Apr</option> <option value="05" <?php if(in_array('05',explode(',',$month))) echo 'selected="selected"';?>>May</option> <option value="06" <?php if(in_array('06',explode(',',$month))) echo 'selected="selected"';?>>Jun</option> <option value="07" <?php if(in_array('07',explode(',',$month))) echo 'selected="selected"';?>>Jul</option> <option value="08" <?php if(in_array('08',explode(',',$month))) echo 'selected="selected"';?>>Aug</option> <option value="09" <?php if(in_array('09',explode(',',$month))) echo 'selected="selected"';?>>Sep</option> <option value="10" <?php if(in_array('10',explode(',',$month))) echo 'selected="selected"';?>>Oct</option> <option value="11" <?php if(in_array('11',explode(',',$month))) echo 'selected="selected"';?>>Nov</option> <option value="12" <?php if(in_array('12',explode(',',$month))) echo 'selected="selected"';?>>Dec</option> </select> <select name="year" id="year" class="tyextfild validate-selection required"> <?php if($year == '') { $year=date(Y, strtotime($d_dob)); } $gnrl->getDropdownList(array('1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975','1976','1977','1978','1979','1980','1981','1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012'),$year); ?> </select> </td> </tr> This works perfectly. After i select date/month/year,and submit, its showing the results stored in the db without any issues... now i need another date set same thing like this. for ex date/month/year drop down. i tried changing the variable names etc but still it shows incorrect date/month/year (previous data) can help me how can i create another set of this 3 dorp down without issues? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/ Share on other sites More sharing options...
Pikachu2000 Posted September 8, 2012 Share Posted September 8, 2012 Your question is rather unclear. Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/#findComment-1376241 Share on other sites More sharing options...
sam20e Posted September 8, 2012 Author Share Posted September 8, 2012 Hi ok, this is what i want. I have a birthday form. the above code is for date dropdown, month drop down, year drop down. now i need another set of drop down for example anniversary date : date dropdown, month drop down, year drop down. when i copy my birthday dropdown code and create an anniversary set, it shows birthday set value even though database contains correct value in short i need another set of birthday dropdown... Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/#findComment-1376245 Share on other sites More sharing options...
Christian F. Posted September 8, 2012 Share Posted September 8, 2012 You're doing quite a few things in that code which isn't just unnecessary, but also quite wasteful. For instance, why you're using explode () on the $month variable for every single month I don't know. Not only would it have been better to just use it once, store the result, and then check against it, but from what I could see there is no need for explode () at all: There is only going to be one month selected. I've taken the liberty to clean up your code a bit, and rework things to something more like how I'd do it. Hopefully this will give you an idea on what needs to be done: <?php if ($dobDay == '') { $dobDay = date ('d', strtotime ($d_dob)); } if ($dobMonth == '') { $dobMonth = date ('m', strtotime ($d_dob)); } if ($dobYear == '') { $dobYear = date ('Y', strtotime ($d_dob)); } // Create dropdowns options for the day and year. $dobDayDropdown = $gnrl->getDropdownList (range ('1', '31'), $dobDay); $dobYearDropdown = $gnrl->getDropdownList (range ('1960', '2012'), $dobYear); // Create custom dropdown options for the month. $outTemp = "\t\t\t\t" . '<option value="%d"%s>%s</option>' . "\n"; $dobMonthDropdown = ''; for ($run = 1; $run <= 12; $run++) { if ($dobMonth == $run) { $selected = ' selected="selected"'; } else { $selected = ''; } // Adds the current month to the options, generating the short month name based upon locale. $dobMonthDropdown .= sprintf ($outTemp, $run, $selected, date ('M', strtotime ("2000-{$Run}-1"))); } ?> <tr> <td align="left" valign="middle">Date Of Birth<span class="astrikrequired">*</span>:</td> <td align="left" valign="middle"> <select name="dob_day" id="dob_day" class="tyextfild validate-selection required"> <?php echo $dobDayDropdown; ?> </select> <select name="dob_month" id="dob_month" class="tyextfild validate-selection required"> <?php echo $dobMonthDropdown; ?> </select> <select name="dob_year" id="dob_year" class="tyextfild validate-selection required"> <?php echo $dobYearDropdown; ?> </select></td> </tr> I would recommend to move all of the PHP processing to above any of the HTML outputting. That way the code will not only be a lot cleaner and easier to read, but it will be a lot more flexible and give you power to do a lot more than what you currently can. Read the "HEADER ERRORS" thread for more information on that. Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/#findComment-1376251 Share on other sites More sharing options...
sam20e Posted September 11, 2012 Author Share Posted September 11, 2012 i did following : <select name="day_new" id="day_new" class="tyextfild validate-selection required"> <?php if($day_new == '') { $day_new=date(d, strtotime($d_soc_exp_date)); } $gnrl->getDropdownList(range( 1, 31 ),$day_new); ?> </select> <select name="month_new" id="month_new" class="tyextfild validate-selection required"> <?php if($month_new == '') { $month_new=date('m', strtotime($d_soc_exp_date)); } for($i=00; $i <= 12; $i++ ) { $selected = ( in_array('$i',explode(',',$month_new) ) ? "selected=\"selected\"" : "" ); echo "<option value=\"$i\"$selected>". date(M, $i) ."</option>"; } ?> </select> <select name="year_new" id="year_new" class="tyextfild validate-selection required"> <?php if($year_new == '') { $year_new=date(Y, strtotime($d_soc_exp_date)); } $gnrl->getDropdownList(range( '1970', date( 'Y', time() ) ),$year_new); ?> </select> in this database record has been already stored.. date and year showing correct figure stored in the db.. but month not showing.. according to db March should be selected but this one Dec getting selected any help? Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/#findComment-1376914 Share on other sites More sharing options...
Christian F. Posted September 11, 2012 Share Posted September 11, 2012 Solved, I think, via IRC: 05:36 ChristianF SAM: Yes, 1-12 is not valid timestamps. Well.. They are, but only for the first 12 seconds in January 1st 1970. 05:37 SAM how can i change it? ChristianF Also, move the "explode ()" out of the loop. The way it's now you're translating the string into an array for every month, instead of doing it just once. 05:38 ChristianF Yes, you'll need to construct a proper timestamp using "strtotime ()". Which I'm quite positive I did in the previous example I wrote for you. ChristianF Also, related to the "explode ()" issue: Are you allowing multiple months to be selected, or only one? 05:40 SAM only 1 month.. ChristianF Then why use explode in the first place? 05:45 SAM how can i construct without explode? 05:47 ChristianF What does $month contain? ChristianF You always start with the value of the variable, and its type, when figuring out how write the conditional statement. SAM u mean drop down? ChristianF No, I mean the value of the variable $month. 05:48 SAM 12 months SAM $month=date('m', strtotime($d_dob)); SAM from this column : d_dob ChristianF This has nothing to do with the HTML code, or the dropdown. The fact that that's where the value might come from is quite irrelevant for this question. 05:49 ChristianF Run a var_dump () on $month, just before the test. Then you'll see what it contains. SAM ok.. 05:57 Frost http://pastebin.com/i2BLD8Wk Quote Link to comment https://forums.phpfreaks.com/topic/268145-php-drop-down/#findComment-1376943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.