Jump to content

Three $_post Values In One


son

Recommended Posts

I have a db table which stores a date in column with date format. On webpage there are select drop-downs for day, month and year. I am struggling in how to bring those all together in web page code. The code for dropdown is:

<p><label for="date">Date</label><?php// make calendar pull-downfunction make_calendar_pulldowns ($m = NULL, $d = NULL, $y = NULL)	{// month array$months = array (1 => 'January','February','March','April','May','June','July','August','September','October','November','December');// month pull-downecho '<select name="month">';foreach ($months as $key => $value)	{echo "<option value=\"$key\"";if ($key == $m)	{echo ' selected="selected"';	}echo ">$value</option>\n";}echo '</select>';//days pull-downecho '<select name="day">';for ($day = 1; $day <= 31; $day++)	{echo "<option value=\"$day\"";if ($day == $d)	{echo ' selected="selected"';	}echo ">$day</option>\n";}echo '</select>';// years pull-downecho '<select name="year">';for ($year = 2009; $year <= 2009; $year++)	{echo "<option value=\"$year\"";if ($year == $y)	{echo ' selected="selected"';	}echo ">$year</option>\n";}echo '</select>';} // end of function definition// get today's info and call function$dates = getdate();make_calendar_pulldowns ($dates['mon'], $dates['mday'], $dates['year']);?></p>

The other $_POST values are stored in variables for insert query (example below):

if (!isset($_POST['location']) OR empty($_POST['location'])) {$location = FALSE;$errors['location'] = '\'Location\' is a required field';} else	{	$location = escape_data($_POST['location']);}

How can I create a variable which holds all three value in correct fomat for db (year-month-day)? DB displays for example '2008-03-27'...Son

Link to comment
Share on other sites

I managed to get the date inserted correcly into database, but would like to shorten the first line of my code:

if (!isset($_POST['year']) OR !isset($_POST['month']) OR !isset($_POST['day']) OR empty($_POST['year']) OR empty($_POST['month']) OR empty($_POST['day'])) {$date = FALSE;$errors['date'] = '\'Date\' is a required field';}	else	{$date = $_POST['year'];$date .= '-';$date .= $_POST['month'];$date .= '-';$date .= $_POST['day'];echo $date;}

Tried several ways to combine all three '!issets' and all three 'empties', but could only manged to look at empty page. How do you combine those $_Post values?Son

Link to comment
Share on other sites

I guess you could implode() it. It's opposite of explode(). Like here:
<?php$array = array('lastname', 'email', 'phone');$comma_separated = implode(",", $array);echo $comma_separated; // lastname,email,phone?>

See more:http://www.php.net/implodehttp://www.w3schools.com/php/func_string_implode.aspHope it helps :)

Cheers. Do you also know how to shorten the line:if (!isset($_POST['year']) OR !isset($_POST['month']) OR !isset($_POST['day']) OR empty($_POST['year']) OR empty($_POST['month']) OR empty($_POST['day'])) {That would be great!Son
Link to comment
Share on other sites

Cheers. Do you also know how to shorten the line:if (!isset($_POST['year']) OR !isset($_POST['month']) OR !isset($_POST['day']) OR empty($_POST['year']) OR empty($_POST['month']) OR empty($_POST['day'])) {That would be great!Son
Seems to me that you probably don't need empty() here... I've found that when using if(isset($_POST)), $_POST is an autoglobal and is always set. But when using specific keys like here $_POST['day'], if e.g. $_POST['day'] is not set (!isset), can it be anything else but empty?(?!) So, I think:if (!isset($_POST['year']) OR !isset($_POST['month']) OR !isset($_POST['day'])is just enoughTest it! Experiment!?(Am I wrong, anyone?)
Link to comment
Share on other sites

Leave empty there, remove isset. You don't need to use isset if you use empty.
Does empty also include the possibility that a field is not set? Always used both as I learned it some time ago from a book this way...Son
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...