bradkenyon Posted September 17, 2008 Share Posted September 17, 2008 I have a function that adds an event to a db table, which stores it as: YYYY-MM-DD HH:MM:SS For the HH:MM:SS, I have 3 drop downs, Hours, Minutes, AM/PM, right now is 9:15am, so it'd go in as 09:15:00 if it was 4:15pm, it'd go in as 16:15:00 For Hours: 1-12, Minutes: 00, 15, 30, 45, and well AM/PM: AM or PM If it is set to PM, I just have it add 12 to the hour selected and I parse everything nicely into the datetime field in the table. Now the question is, for the update function, I want to display those same three drop downs for the time, Hours, Minutes, AM/PM, already selected with the time that was set when the event was initially added. I am assuming it is a while loop that goes thru and tries to match the value within the table up to the value within the drop down box. I am a little foggy on who to go about that, any help would be greatly appreciated. Here is the code that generates the drop downs for the add function: <?php //hour $h = 1; print '<select name="event_hour">'; while($h <= 12){ print '<option value="'.$h.'">'.$h.'</option>'; $h++; } print '</select> : '; //minute print '<select name="event_minute"> <option value="00">00</option> <option value="15">15</option> <option value="30">30</option> <option value="45">45</option> </select> '; //AM or PM print '<select name="event_am_pm"> <option value="AM">AM</option> <option value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/ Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 found this code, but it checks for the index. I want to check it against the value w/i the table. so if the event i am updating had it set to 4:35pm, I want it to check for the minute(s): 30, as you'll see in this example, but it uses the index 2 to find it within the array. So ya, I want it to match the one within the array, not the index of the array, so the code below I need help tweaking to do just that. <?php function dropdown($name, $options, $selected=null) { /*** begin the select ***/ $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n"; $selected = $selected; /*** loop over the options ***/ foreach($options as $key=>$option) { /*** assign a selected value ***/ $select = $selected==$key ? ' selected' : null; /*** add each option to the dropdown ***/ $dropdown.= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n"; } /*** close the select ***/ $dropdown.= '</select>'."\n"; /*** and return the completed dropdown ***/ return $dropdown; } ?> <form> <?php $name = 'minutes'; $options = array(00, 15, 30, 45); $selected = '2'; echo dropdown($name, $options, $selected); ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643740 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Not sure exactly what you're trying to do. Do you just want to set the selected values to the value found in the database? If so, try this (I also suggest using for loops): <?php $timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++{ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute"> for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } </select> '; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643751 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 This part seems to be confusing me: print '<select name="event_minute"> for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } </select> '; the part where you print the select name... and then the for loop, why print the loop? Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643759 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 I changed it from four options (00, 15, 30, and 45) to all 60 minutes. Either would work, but I just thought I'd throw the option in there. In either case, the $selected variable needs to be set to either blank or 'selected.' In either case, it will be printed inside the option tag. It will only be 'selected' once, and that will be the option that will be selected on the page. Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643768 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 Thank you very much. Copied the code and tried it out, no dice. The color coding in textwrangler makes me wonder if there is a missing ; on the print of the minute for loop, included a screen shot and the code that I used. <?php $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++{ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute"> for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } </select> '; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643782 Share on other sites More sharing options...
lonewolf217 Posted September 17, 2008 Share Posted September 17, 2008 yes there are missing semicolons after Minute and AM/PM print statements Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643787 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 lonewolf is right. It was missing the semicolon and the single quote at the end. Try this: <?php $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++{ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute">'; for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } </select> '; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643794 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Arg, I see another problem.: </select> '; should be print '</select> '; Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643796 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 thanks f1fan found another, at the very end, missing a ' at the end of </select> ; This is what i have now, and it isn't displaying anything. <?php $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++{ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute">'; for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select>'; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> I feel close, but not sure if the timestamp I set it to manually is messing it up. Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643798 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 What are you getting on your page? Any errors or anything weird? Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643805 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 Sadly, nothing. This is exactly what I have. <?php error_reporting(E_ALL); $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++{ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute">'; for ($i=1;$i<=60;$i++{ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select>'; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643813 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Ok, here ya go, I found the issues. Two missing ')' on the for loops. Plus, I should have made the minute loop start at 0 and end with 59, rather than 1 and 60. <?php error_reporting(E_ALL); $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++){ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute">'; for ($i=0;$i<=59;$i++){ $i = $i<10?'0'.$i:$i; $selected = $i==date('i',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select>'; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643818 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 works great! thank you. now not to be picky, could i just set it to display minutes by increments of 15, since when they add it, it only displays it as 00, 15, 30, 45. Sorry for being a pain. I will definitely hold onto the script as is for possibly having to use minutes 1 - 59 down the road. Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643823 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 <?php error_reporting(E_ALL); $timestamp = strtotime('2008-09-18 10:00:00'); //$timestamp = strtotime($timestampfromdb); //hour print '<select name="event_hour">'; for ($i=1;$i<=12;$i++){ $selected = $i==date('g',$timestamp)?"selected":""; print "<option $selected value='$i'>$i</option>"; } print '</select> : '; //minute print '<select name="event_minute">'; for ($i=0;$i<=3;$i++){ $m = $i*15; $m = $m<10?'0'.$m:$m; $selected = $m==date('i',$timestamp)?"selected":""; print "<option $selected value='$m'>$m</option>"; } print '</select>'; //AM or PM print '<select name="event_am_pm"> <option '.(date('A',$timestamp)=="AM"?"selected":"").' value="AM">AM</option> <option '.(date('A',$timestamp)=="PM"?"selected":"").' value="PM">PM</option>'; print '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643824 Share on other sites More sharing options...
bradkenyon Posted September 17, 2008 Author Share Posted September 17, 2008 thank you f1fan and lonewolf217 definitely saved me a lot of time!!! Quote Link to comment https://forums.phpfreaks.com/topic/124637-solved-event-time-drop-down-pre-select-time-that-is-already-in-db-table-for-upd-func/#findComment-643826 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.