matt.sisto Posted May 2, 2009 Share Posted May 2, 2009 I am trying to create a variable based upon user input. This is my if statement, however the variables $end_time and $event_end have no value when echoed out. Any help appreciated. if ($unit=='Hour'){ $end_time = ($start_time + ($quantity * (60*60))); $event_end = $event_start; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } else{ $event_end = ($event_start + ($quantity * ((60*60)*24))); $end_time = $start_time; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/ Share on other sites More sharing options...
DjMikeS Posted May 2, 2009 Share Posted May 2, 2009 Can you show us your HTML form ? Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824330 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 bookingform.php <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $sql="SELECT service_id, service FROM service"; $result=mysql_query($sql); $service_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["service_id"]; $service=$row["service"]; $service_option.="<OPTION VALUE=\"$id\">".$service; } $sql="SELECT con_id, first_name, last_name FROM consultant"; $result=mysql_query($sql); $rconsultant_option=""; while ($row=mysql_fetch_array($result)) { $con_id1=$row["con_id"]; $con_id2=$row["con_id"]; $con_id3=$row["con_id"]; $first_name=$row["first_name"]; $last_name=$row["last_name"]; $consultant_option1.="<OPTION VALUE=\"$con_id1\">".$first_name." ".$last_name; $consultant_option2.="<OPTION VALUE=\"$con_id2\">".$first_name." ".$last_name; $consultant_option3.="<OPTION VALUE=\"$con_id3\">".$first_name." ".$last_name; } $yeartoday = date(Y); $yearplus1 = date(Y)+1; for($y=$yeartoday;$y<=$yearplus1;$y++){ $selected = ""; $longDate = date("Y", mktime(0,0,0,1,1,$y)); if ($Year==$y){ $selected = "selected \n"; } $year_option.="<option value=\"$y\"$selected>$longDate</option>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>SRC: Booking Form</title> <link href="master.css" rel="stylesheet" type="text/css"> <link rel="shortcut icon" type="image/ico" href="favicon.ico"> <script type="text/javascript"> <!-- function OnSubmitForm() { if(document.pressed == 'Check') { window.open("check.php","Window1","menubar=no,width=560,height=360,toolbar=no, scrollbars=yes"); } else if(document.pressed == 'Book') { document.bookingForm.action ="book.php"; } return true; } //--> </script> </head> <body> <div id="centerPanel"><div id="header">Booking Form</div><p> <div id="content"> <form name="bookingForm" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onSubmit="return OnSubmitForm();"> <label id="eventStart">Event Date:</label><br /> <select id="year3" name="year"><option value="<?php $_POST['year']; ?>">Year <?=$year_option?></option></select> <?php $arrMonth = array('01','02','03','04','05','06','07','08','09','10','11','12'); ?> <select id="month3" name="month"> <option value="nil">Month</option> <?php foreach($arrMonth as $value) { echo '<option value="'.$value.'"'.($value==$_POST['month'] ? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> <?php $arrDay = array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'); ?> <select id="day3" name="day"> <option value="nil">Day</option> <?php foreach($arrDay as $value) { echo '<option value="'.$value.'"'.($value==$_POST['day'] ? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> </select> <label id="service">Service:</label><br /> <select id="serviceSelect" name="service"> <option value="<?php $_POST['service']; ?>">Service<?=$service_option?> </option> </select> </p> <p> <label id="unit">Unit:</label><br /> <?php $arrUnit = array('Day','Hour'); ?> <select id="radioHour" name="unit"> <option value="nil">Unit</option> <?php foreach($arrUnit as $value) { echo '<option value="'.$value.'"'.($value==$_POST['unit']? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> <label id="quantity">Quantity:</label> <?php $arrQuant = array('01','02','03','04','05','06','07'); ?> <select id="quantSelect" name="quantity"> <option value="nil">[00]</option> <?php foreach($arrQuant as $value) { echo '<option value="'.$value.'"'.($value==$_POST['quantity'] ? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> </select> <label id="eventStartTime">Start Time:</label><br/> <?php $arrStartTimeHr = array('09','10','11','12','13','14','15','16'); ?> <select id="hour" name="start_time_hr"> <option value="nil">Hr</option> <?php foreach($arrStartTimeHr as $value) { echo '<option value="'.$value.'"'.($value==$_POST['start_time_hr'] ? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> </select> <?php $arrStartTimeMin = array('00','15','30','45'); ?> <select id="minute" name="start_time_min"> <option value="nil">Min</option> <?php foreach($arrStartTimeMin as $value) { echo '<option value="'.$value.'"'.($value==$_POST['start_time_min'] ? ' selected="selected"' : '').'>'.$value.'</option>'; } ?> </select> <label id="AddressFirstLine">Address [first line]:</label><br /> <input id="AddressFirstLineInput" name="AdressFirstLine" value="<?php $_POST['address_first_line']; ?>"/> <label id="PostCode">Post Code [zip code]:</label><br /> <input id="PostCodeInput" name="PostCode" value="<?php $_POST['post_code']; ?>"/> <label id="Country">Country:</label><br /> <input id="CountryInput" name="CountryInput" value="<?php $_POST['country']; ?>"/> <label id="extraInfo">Extra Information:</label><br /> <textarea id="extraInfoInput" name="extra_info" value="<?php $_POST['extra_info']; ?>" onkeypress="return imposeMaxLength(this, 250);"></textarea> <p> <label id="con4">Consultant [1]:</label><br /> <select id="conSelect4" name="con_id1"><option value="<?php $_SESSION['values']['con_id1']; ?>">Select<?=$consultant_option1?></select> <label id="con2">Consultant [2]:</label><br /> <select id="conSelect2" name="con_id2"><option value="<?php $_SESSION['values']['con_id2']; ?>">Select<?=$consultant_option2?></select> <label id="con3">Consultant [3]:</label><br /> <select id="conSelect3" name="con_id3"><option value="<?php $_SESSION['values']['con_id3']; ?>">Select<?=$consultant_option3?></select> <input id= "check1" type ="submit" name="Check" onClick="document.pressed=this.value" value="Check"></p> <p><input id="book" type ="submit" name="Book" onClick="document.pressed=this.value" value="Book"/></form> </div> </body> </html> book.php <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $unit = $_POST['unit']; $service = $_POST['service']; $quantity = $_POST['quantity']; $con_id1 = $_POST['con_id1']; $con_id2 = $_POST['con_id2']; $con_id3 = $_POST['con_id3']; $client_id = session_id(); $extra_info = $_POST['extra_info']; $start_time_hr = $_POST['start_time_hr']; $start_time_min = $_POST['start_time_min']; $time_sec = '00'; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $country = $_POST['country']; $month = $_POST["month"]; $day = $_POST["day"]; $year = $_POST["year"]; $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit=='Hour'){ $end_time = ($start_time + ($quantity * (60*60))); $event_end = $event_start; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } else{ $event_end = ($event_start + ($quantity * ((60*60)*24))); $end_time = $start_time; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$event_end."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')"; echo $sql; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: bookingform.php"); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Book</title> </head> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824336 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 This is my bookingform.php, which passes the values to the book.php, this works fine but its the calculation for creating the $end_time, $end_date that is not working. Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824338 Share on other sites More sharing options...
DjMikeS Posted May 2, 2009 Share Posted May 2, 2009 I don't see event_time and event_end form items ? Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824341 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 That is because I am using the start_date or start_time, unit: hour or day to determine the values of them. Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824344 Share on other sites More sharing options...
wildteen88 Posted May 2, 2009 Share Posted May 2, 2009 For starters you code makes no sense. You're trying to to add a string and integer together which you cannot do (highlighted in red) $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit=='Hour'){ $end_time = ($start_time + ($quantity * (60*60))); $event_end = $event_start; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } else{ $event_end = ($event_start + ($quantity * ((60*60)*24))); $end_time = $start_time; $end_time=$_POST["end_time"]; $event_end=$_POST["event_end"]; } Also the code in orange does not make sense either. You need to explain what you're trying to do here, as what you're currently doing is wrong Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824355 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 Ok. So when the user inputs a date on the form, and selects either the day or hour for the value of unit, and then decides the amount of units necessary so I would like to be able to calculate the end_date or time depending on these specifications. So if the user wants to book an appointment on 21/02/2010 and for 2 days, I want the book.php script to calculate the end date, or on the other hand if the user was to select hour then 2 hours would be added to the start_time to calculate the end_time. Hope this explains it well enough. Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824364 Share on other sites More sharing options...
wildteen88 Posted May 2, 2009 Share Posted May 2, 2009 You can do this with strtotime. However your date will need to be in US English format mm/dd/yyyy for it to work. $start_date = '02/21/2010'; $end_date = strtotime("+2 Days", strtotime($start_date)); echo 'Start Date: '.date('D j M Y', strtotime($start_date)) .'<br />'; echo 'End Date: '.date('D j M Y', $end_date); Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824376 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 Ok, but i would need the "+2 days" to be a variable because the user can book 1-7 days, or 1-7 hours. Or would I just do an if statement, with something like: if (unit == 'Day'){ if (quantity=='01'){ $end_date = strtotime("+1 Days", strtotime($start_date)); } else if (quantity=='02'){ $end_date = strtotime("+2 Days", strtotime($start_date)); } else if (quantity=='03'){ $end_date = strtotime("+3 Days", strtotime($start_date)); } else if (quantity=='04'){ $end_date = strtotime("+4 Days", strtotime($start_date)); } else if (quantity=='05'){ $end_date = strtotime("+5 Days", strtotime($start_date)); } else if (quantity=='06'){ $end_date = strtotime("+6 Days", strtotime($start_date)); } else (quantity=='07'){ $end_date = strtotime("+7 Days", strtotime($start_date)); } else (unit == 'Hour'){ if (quantity=='01'){ $end_date = strtotime("+1 Hours", strtotime($start_date)); } else if (quantity=='02'){ $end_date = strtotime("+2 Hours", strtotime($start_date)); } else if (quantity=='03'){ $end_date = strtotime("+3 Hours", strtotime($start_date)); } else if (quantity=='04'){ $end_date = strtotime("+4 Hours", strtotime($start_date)); } else if (quantity=='05'){ $end_date = strtotime("+5 Hours", strtotime($start_date)); } else if (quantity=='06'){ $end_date = strtotime("+6 Hours", strtotime($start_date)); } else (quantity=='07'){ $end_date = strtotime("+7 Hours", strtotime($start_date)); } Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824389 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 Sorry I meant something like this, the last one was a bit rushed. <?php if (unit == 'Day'){ if (quantity=='01'){ $end_date = strtotime("+1 Days", strtotime($start_date)); } else if (quantity=='02'){ $end_date = strtotime("+2 Days", strtotime($start_date)); } else if (quantity=='03'){ $end_date = strtotime("+3 Days", strtotime($start_date)); } else if (quantity=='04'){ $end_date = strtotime("+4 Days", strtotime($start_date)); } else if (quantity=='05'){ $end_date = strtotime("+5 Days", strtotime($start_date)); } else if (quantity=='06'){ $end_date = strtotime("+6 Days", strtotime($start_date)); } else (quantity=='07'){ $end_date = strtotime("+7 Days", strtotime($start_date)); } } else (unit == 'Hour'){ if (quantity=='01'){ $end_time = strtotime("+1 Hours", strtotime($start_time)); } else if (quantity=='02'){ $end_time = strtotime("+2 Hours", strtotime($start_date)); } else if (quantity=='03'){ $end_time = strtotime("+3 Hours", strtotime($start_time)); } else if (quantity=='04'){ $end_time = strtotime("+4 Hours", strtotime($start_time)); } else if (quantity=='05'){ $end_time = strtotime("+5 Hours", strtotime($start_time)); } else if (quantity=='06'){ $end_time = strtotime("+6 Hours", strtotime($start_time)); } else (quantity=='07'){ $end_time = strtotime("+7 Hours", strtotime($start_time)); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824394 Share on other sites More sharing options...
wildteen88 Posted May 2, 2009 Share Posted May 2, 2009 You wont need to do that long if/elseif block you'd do it like this if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $end_date = strtotime("+$d Days", strtotime($start_date)); } For time it'll be similar to doing the date. $start_date = '02/21/2010 9:45:30'; $end_date = strtotime("+2 Hours", strtotime($start_date)); echo 'Start Date: '.date('D j M Y G:i:s', strtotime($start_date)) .'<br />'; echo 'End Date: '.date('D j M Y G:i:s', $end_date); Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824398 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 Now I have included this if statement but the script doesn't work. ??? <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $unit = $_POST['unit']; $service = $_POST['service']; $quantity = $_POST['quantity']; $con_id1 = $_POST['con_id1']; $con_id2 = $_POST['con_id2']; $con_id3 = $_POST['con_id3']; $client_id = session_id(); $extra_info = $_POST['extra_info']; $start_time_hr = $_POST['start_time_hr']; $start_time_min = $_POST['start_time_min']; $time_sec = '00'; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $country = $_POST['country']; $month = $_POST["month"]; $day = $_POST["day"]; $year = $_POST["year"]; $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $event_end = strtotime("+$d Days", strtotime($event_start)); } else ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $h = (int) $quantity; $end_time = strtotime("+$h Hours", strtotime($start_time)); } $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$event_end."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')"; echo $sql; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: bookingform.php"); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Book</title> </head> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824406 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $unit = $_POST['unit']; $service = $_POST['service']; $quantity = $_POST['quantity']; $con_id1 = $_POST['con_id1']; $con_id2 = $_POST['con_id2']; $con_id3 = $_POST['con_id3']; $client_id = session_id(); $extra_info = $_POST['extra_info']; $start_time_hr = $_POST['start_time_hr']; $start_time_min = $_POST['start_time_min']; $time_sec = '00'; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $country = $_POST['country']; $month = $_POST["month"]; $day = $_POST["day"]; $year = $_POST["year"]; $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $event_end = strtotime("+$d Days", strtotime($event_start)); } else ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7) { $h = (int) $quantity; $end_time = strtotime("+$h Hours", strtotime($start_time)); } $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$event_end."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')"; echo $sql; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: bookingform.php"); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Book</title> </head> <body> </body> </html> JUst noticed an error. Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824416 Share on other sites More sharing options...
wildteen88 Posted May 2, 2009 Share Posted May 2, 2009 You should use elseif not elsehere else ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7) Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824419 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 OK so that is now semi working, I entered into my form: Date:2009-08-05 Unit: Hour Quantity: 03 Start Time: 10:00:00 and when I echoed out the values: ('2009-8-5','Hour','03','10:00:00 ','1241265600') Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824427 Share on other sites More sharing options...
wildteen88 Posted May 2, 2009 Share Posted May 2, 2009 strtotime returns a unix timestamp, which is the number of seconds passed since 1st Jan 1970. You'll need to use date to convert the timestamp into a readable date (as I have done in my code examples). Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824432 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 This is now working. Thanks for the help. <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $unit = $_POST['unit']; $service = $_POST['service']; $quantity = $_POST['quantity']; $con_id1 = $_POST['con_id1']; $con_id2 = $_POST['con_id2']; $con_id3 = $_POST['con_id3']; $client_id = session_id(); $extra_info = $_POST['extra_info']; $start_time_hr = $_POST['start_time_hr']; $start_time_min = $_POST['start_time_min']; $time_sec = '00'; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $country = $_POST['country']; $month = $_POST["month"]; $day = $_POST["day"]; $year = $_POST["year"]; $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $event_end = strtotime("+$d Days", strtotime($event_start)); $end_date = date('D j M Y', $event_end); } else if ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7) { $h = (int) $quantity; $end_time = strtotime("+$h Hours", strtotime($start_time)); $end_Time = date('G:i:s', $end_time); } $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$end_date."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_Time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')"; echo $sql; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: bookingform.php"); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Book</title> </head> <body> </body> </html> One last thing I want to set the end_date to the same as the start_date if the unit equals hour, and the end_time to equal the start_time should the unit equal day. I was thinking something like this: <?php if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $event_end = strtotime("+$d Days", strtotime($event_start)); $end_date = date('D j M Y', $event_end); $end_Time = $start time; } else if ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7) { $h = (int) $quantity; $end_time = strtotime("+$h Hours", strtotime($start_time)); $end_Time = date('G:i:s', $end_time); $end_date = $event_start; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824449 Share on other sites More sharing options...
matt.sisto Posted May 2, 2009 Author Share Posted May 2, 2009 <?php session_start(); if (!isset($_SESSION['username'])) { header("Location: login.php"); exit(); } require "dbconn2.php"; $unit = $_POST['unit']; $service = $_POST['service']; $quantity = $_POST['quantity']; $con_id1 = $_POST['con_id1']; $con_id2 = $_POST['con_id2']; $con_id3 = $_POST['con_id3']; $client_id = session_id(); $extra_info = $_POST['extra_info']; $start_time_hr = $_POST['start_time_hr']; $start_time_min = $_POST['start_time_min']; $time_sec = '00'; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $country = $_POST['country']; $month = $_POST["month"]; $day = $_POST["day"]; $year = $_POST["year"]; $event_start = $year."-".$month."-".$day." ".$_POST["event_start"]; $start_time = $start_time_hr.":".$start_time_min.":".$time_sec." ".$_POST["start_time"]; if ($unit == 'Day' && $quantity >= 1 && $quantity <= 7) { $d = (int) $quantity; $event_end = strtotime("+$d Days", strtotime($event_start)); $end_date = date('D j M Y', $event_end); $end_Time =$start_time; } else if ($unit == 'Hour' && $quantity >= 1 && $quantity <= 7) { $h = (int) $quantity; $end_time = strtotime("+$h Hours", strtotime($start_time)); $end_Time = date('G:i:s', $end_time); $end_date = $event_start; } $sql = "INSERT INTO calendar_events VALUES (0,'".$event_start."','".$end_date."','".$client_id."','".$service."','".$unit."','".$quantity."','".$start_time."','".$end_Time."','".$con_id1."','".$con_id2."','".$con_id3."','".$address_first_line."','".$post_code."','".$country."','".$extra_info."')"; echo $sql; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: bookingform.php"); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Book</title> </head> <body> </body> </html> Sorted, thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/156563-solved-using-an-if-statement-to-calculate-a-new-variable/#findComment-824451 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.