Jump to content

Setting default values with IF statements.


kingnutter

Recommended Posts

I am stuck with the following page that should split a dropdown selected date into a text value ($date) and Unix timestamp ($unixdate).

 

The user will be able to enter things that are either daily, monthly or yearly determined by $freq (which will eventually be POSTed from a previous page).

 

I am trying to set defaults so that a yearly entry will default the month and day to 01 so that the Unix_timestamp will be for the first day of the year.

 

All works well for daily and monthly but not yearly. I tried setting my defaults to 05 and discovered that they are not being used. The unix_timestamp has been using it's own defaults all along.

 

The trouble is it produces a NULL entry for when only $year is offered for strtotime. Also I think I should understand such basic operations before I go any further.

 

I would be very grateful if somebody could tell me where my syntax is going wrong or if I should be defining the default values in another part of the page.

 

Thanks

KN

 


<html>
<head></head>
<body>

<!-- standard page header begins -->
<p> <p>

<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<td></td>
</tr>
<td bgcolor="Brown"><font size="-1" color="White" face="courier new">
<b>Mojo Cover CDs</b></font>
</td>
</tr>
</table>
<p />
<!--standard page header ends -->

<?php


// form not yet submitted
// display initial form

if (!$_POST['submit'])
{
?>
ADD NEW CD
<table cellspacing="5" cellpadding="5">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<tr>
<td valign="top"><b><font size="-1">Title</font></b></td>
<td>
	<input size="50" maxlength="254" type="text" name="moj_title">
	</td>
</tr>
<tr>

<td valign="top"><b><font size="-1">Date</font></b></td>
	    <td>			

<?php 

// This script makes three pull-down menus

// for an HTML form: months, days, years.

// Make the months array:

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Make the days and years arrays

$days = range (1, 31);
$years = range (2001, 2009);
$freq = "yearly";

if ($freq == "daily")

{	
// Make the days pull-down menu:
echo '<select name="day">';
foreach ($days as $value) {
	echo "<option value=\"$value\">$value</option>\n";
	}
	echo '</select>';
}

// considered setting default day value for monthly / daily projects here
// else
// {$day = 01;} or maybe {$_POST[day] = '01';}


// Make the months pull-down menu:

if (($freq == "daily") || ($freq == "monthly"))

{
echo '<select name="month">';
foreach ($months as $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';
}

// make the year pull-down menu:

if (($freq = "daily") || ($freq == "monthly") || ($freq == "yearly"))

{	
echo '<select name="year">';
foreach ($years as $value) {
	echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';
}

?> <font size="-1" color="red">YEARLY STILL DOESN'T WORK</td></tr></table>



<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<td></td>
</tr>

<tr><td><font size="-1" color="red"><b>HAVE TO WORK OUT HOW TO RECALL THE DATE (WITHOUT ANY 01's) )</b></font></td>

		</tr>

		<tr>
		<td valign="top"><b><font size="-1">Issue No:</font></b>
		<input size="5" maxlength="5" type="text" name="moj_issue">
		</td>
		</tr>
		</table>
		<font size="-1" color="red">ISSUE IS TEXT FIELD. NEED TO INVESTIGATE HOW TO VALIDATE FOR NUMERICS ONLY. OR DO WE WANT TO? MIGHT NEED TO BE TEXT FOR SOME PROJECTS. DON'T FORGET "SPECIAL"<br />
		</font>

<tr>
<td valign="top"><b><font size="-1">Summary</font?</b></td>
<td>
	<textarea name="moj_summary" cols="43" rows="8"></textarea>
</td>
</tr><p>
<tr>
<td valign="top"><b><font size="-1">Genre</font></b></td>
<td>
	<input size="45" max length="254" type="text" name="moj_genre">
	</td>
	</tr>
	</table>
	<font size="-1" color="red">NB: TABS WITH COMMAS FOR NOW. CONSIDER DROPDOWN BOX (THAT CAN BE UPDATED?><br />
		moj_timestamp UPDATED AUTOMATICALLY (ALONG WITH moj_id). SHOULD WE HAVE A SEPARATE FIELD FOR UPDATED? ARRAY? WHY?
		</font>

<table cellspacing="5" cellpadding="5">
<!-- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> -->
<tr>
<td colspan=2> 
	<input type="submit" name="submit" value="Add">
	</td>
	</tr>
	</form>
	</table>

<?php
}
else
{
// includes
include('conf.php');
//include('functions.php');	

// set up error list array
$errorList = array();

$moj_title = $_POST[moj_title];
$moj_issue = $_POST[moj_issue];
$moj_summary = $_POST[moj_summary];
$moj_genre = $_POST[moj_genre];
$day = $_POST[day];
$month = $_POST[month];
$year = $_POST[year];


if (trim($_POST['moj_title']) == '')
{
	$errorList[] = 'Invalid entry: Title';
}


if (trim($_POST['moj_issue']) == '')
{
	$errorList[] = 'Invalid entry: Issue (remember - not validating numeric entry yet)';
}

if (trim($_POST['moj_summary']) == '')
{
	$errorList[] = 'Invalid entry: Summary';
}

if (trim($_POST['moj_genre']) == '')
{
	$errorList[] = 'Invalid entry: Genre';
}

// ultimately the day will be checked against the month i.e. no 30th Feb. Can maybe do this with 'minus' on unixtime
if (trim($_POST['day']) == '' && ($freq == "daily"))
{
	$errorList[] = 'Invalid entry: Day of date';
	}

if (trim($_POST['month']) == '' && (($freq == "daily") || ($freq == "monthly")))
{
	$errorList[] = 'Invalid entry: Month of date';
	}		

if (trim($_POST['year']) == '' && ((($freq == "daily") || ($freq == "monthly") || ($freq == "yearly"))))
{
	$errorList[] = 'Invalid entry: Year of date';
	}		
// check for errors
// if none found
if (sizeof($errorList) == 0)
{
	// open database connection
	$connection = mysql_connect($host, $user, $pass)
or die ('Unable to connect to database: Line 131');

// select database
mysql_select_db($db)
or die ('Unable to connect');


// This works for the text based date for now as preceding spaces appear to be stripped
$date = "$day"." "."$month"." "."$year";



// if monthly turn $day into 01 and use $month and $year for $unixdate

if ($freq == "daily")
$date = "$day"." "."$month"." "."$year";

if ($freq == "monthly")
{
$day = '05';
$date = "$month"." "."$year";
}


// if yearly, turn $day into 01 and $month into 01 and use $year for $unixdate


if ($freq == "yearly")
{
$day = '05';
$month = '05';
$date = "$year";}


$dateforunix = ("$year"." "."$month"." "."$day");
$unixdate = strtotime($day." ".$month." ".$year." ".'+ 1 hour');

// generate and execute query
$query = "INSERT INTO
mojocd(moj_title, moj_date, moj_issue, moj_summary, moj_genre, unix_timestamp, moj_timestamp)
VALUES ('$moj_title', '$date', '$moj_issue', '$moj_summary', '$moj_genre', '$unixdate', NOW())";

$result = mysql_query($query)
or die ("Error in Query: $query. " . mysql_error());

// print result
echo '<font size=-1>Update successful.
<a href=list.php>Go back to the main menu</a>.</font>';

// close database connection
mysql_close($connection);

}
else
{
	// errors found
	// print as list
	echo '<font size=-1>The following errors were encountered:';
	echo '<br>';
	echo '<ul>';
	for ($x=0; $x<sizeof($errorList); $x++)
	{
		echo "<li>$errorList[$x]";
	}
	echo '</ul></font>';
}
}
?>


<!-- standard page footer begins -->
<p>
<table width="100%" cellspacing="0" cellpadding="5">
<tr>
<td align="center"><font size="-2">
A King Nutter Production</td>
</tr>
</table>
<!-- standard page footer ends -->

</body>
</html>

 

Link to comment
Share on other sites

Ah. Good spot. Thanks.

 

However, this doesn't affect the part of the script that ultimately defines the defaults towards the end of the script. This section:

 


// if monthly turn $day into 01 and use $month and $year for $unixdate

if ($freq == "daily")
$date = "$day"." "."$month"." "."$year";

if ($freq == "monthly")
{
$day = '05';
$date = "$month"." "."$year";
}


// if yearly, turn $day into 01 and $month into 01 and use $year for $unixdate


if ($freq == "yearly")
{
$day = '05';
$month = '05';
$date = "$year";}


$dateforunix = ("$year"." "."$month"." "."$day");
$unixdate = strtotime($day." ".$month." ".$year." ".'+ 1 hour');

// generate and execute query
$query = "INSERT INTO
mojocd(moj_title, moj_date, moj_issue, moj_summary, moj_genre, unix_timestamp, moj_timestamp)
VALUES ('$moj_title', '$date', '$moj_issue', '$moj_summary', '$moj_genre', '$unixdate', NOW())";

Link to comment
Share on other sites

I've got it working. It seems that I will need to rePOST $freq in the second half of the script.

 

Would I be right in thinking that any variables set before the form is submitted, but not part of the form would get lost in the POST process?

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.