Jump to content

[SOLVED] event time drop down, pre select time that is already in db table - for upd func


bradkenyon

Recommended Posts

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>';
?>

 

Link to comment
Share on other sites

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>

 

 

 

 

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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>';
?>

 

 

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.