486974 Posted May 27, 2009 Share Posted May 27, 2009 Hello All I have a simple form which has start date and days the idea is someone inputs there start date and how many days they are staying and it works out the price can someone tell me how i get what someone types into the box which then gets the figure which corresponds to that date then if someone types in 5 days then the script adds the next 5 figures together. I have added a piece of code which i copied from a tutorial and have tried changing it so it just multiplies the start date figure by how many days are inputted i couldn't get the code to get the date value whatever i tried. <form action="welcome.php" method="post"> Start Date: <input type="text" name="sdate" /> Days: <input type="text" name="days" /> <input type="submit" /> </form> <br><br> <?php echo $_POST['sdate']; ?> <?php echo $_POST['days']; ?> <?php $array = array( 'January 01' => 100, 'January 02' => 120, 'January 03' => 90, 'January 04' => 100, 'January 05' => 120, ); echo $array[$_POST['sdate']]; ?> <?php function mySum($numX, $numY){ $total = $numX * $numY; return $total; } $myNumber = 0; echo "Before the function, myNumber = ". $myNumber ."<br />"; $myNumber = mySum($array[$_POST['sdate']];,$_POST['days']; // Store the result of mySum in $myNumber echo "After the function, myNumber = " . $myNumber ."<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/ Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 1. Use tags. 2. What's your issue? I didn't quite understand what you meant by "i couldn't get the code to get the date value". Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843612 Share on other sites More sharing options...
486974 Posted May 27, 2009 Author Share Posted May 27, 2009 Each day has a value assigned and if i type in January 01 into the date box it returns the figure 100 ideally what i want the script to do if i type in 3 in the days box i want it to add January 01 to January 02 and then January 03 and produce the answer 310. 'January 01' => 100, 'January 02' => 120, 'January 03' => 90, 'January 04' => 100, 'January 05' => 120, Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843620 Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 This will be one heck of a loop. <?php $date = 'January 02'; $days = 3; $h_days = $days; $total_price = 0; foreach ($array as $key => $price) { if ($key === $date || $days < $h_days) $total_price += $price; if (--$days === 0) break; } echo $total_price; Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843630 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 Whatever i change days figure too thats what it changes the price to on my webpage if i type the days the code does not work out the price <?php $date = 'January 02'; $days = 3; Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843719 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 What? Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843729 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 Sorry didnt explain that too well. If i add more days to the code as not everyone wants 3 days it doesnt work <?php $date = 'January 02'; $days = 3; $days = 4; $days = 5; //Dont know if im putting days in right place $h_days = $days; $total_price = 0; foreach ($array as $key => $price) { if ($key === $date || $days < $h_days) $total_price += $price; if (--$days === 0) break; } echo $total_price; http://www.disneysvacationvillarentals.net/3_Bedroom/BL1099/welcome.php Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843738 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Uh... it was an example. You're just overwriting the variable $days. <?php echo $_POST['sdate']; ?> <?php echo $_POST['days']; ?> See those $_POST values you posted up in your code? Replace $date and $days with them. Common sense is tedious. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843739 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 thanks for your help ken2k7 still cant get it to work obviously no common sense Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843758 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 What's the code? Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843833 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 <form action="welcome.php" method="post"> Start Date: <input type="text" name="sdate" /> Days: <input type="text" name="days" /> <input type="submit" /> </form> <br><br> <?php echo $_POST['sdate']; ?> <font> X </font> <?php echo $_POST['days']; ?> <font> Days = </font> <?php $array = array( 'January 01' => 100, 'January 02' => 100, 'January 03' => 100, 'January 04' => 100, 'January 05' => 100, 'January 06' => 100, 'January 07' => 100, 'January 08' => 100, 'January 09' => 100, 'January 10' => 100, 'January 11' => 100, 'January 12' => 100, 'January 13' => 100, 'January 14' => 100, 'January 15' => 100, ); $date = 'January 01'; $days = 3; $h_days = $days; $total_price = 0; foreach ($array as $key => $price) { if ($key === $date || $days < $h_days) $total_price += $price; if (--$days === 0) break; } echo $total_price; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843969 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 Sorry wrong code above when i said it didnt work it does when you type in January 01 and 5 days returns 500 but if you type in January 02 and 5 days it returns 400 and that should be 500 <?php echo $_POST['sdate']; ?> <font size="3"> X </font> <?php echo $_POST['days']; ?> <font size="3"> Days = </font> <?php $array = array( 'January 01' => 100, 'January 02' => 100, 'January 03' => 100, 'January 04' => 100, 'January 05' => 100, 'January 06' => 100, 'January 07' => 100, 'January 08' => 100, 'January 09' => 100, 'January 10' => 100, 'January 11' => 100, 'January 12' => 100, 'January 13' => 100, 'January 14' => 100, 'January 15' => 100, 'January 16' => 100, 'January 17' => 100, 'January 18' => 100, 'January 19' => 100, 'January 20' => 100, 'January 21' => 100, 'January 22' => 100, 'January 23' => 100, 'January 24' => 100, 'January 25' => 100, 'January 26' => 100, 'January 27' => 100, 'January 28' => 100, 'January 29' => 100, 'January 30' => 100, 'January 31' => 100, ); $date = $_POST['sdate']; $days = $_POST['days']; $h_days = $days; $total_price = 0; foreach ($array as $key => $price) { if ($key === $date || $days < $h_days) $total_price += $price; if (--$days === 0) break; } echo $total_price; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-843994 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Remember to use code tags! <?php $date = 'January 02'; $days = 3; $h_days = $days; $total_price = 0; foreach ($array as $key => $price) { if ($days-- === 0) break; if ($key === $date || $days < $h_days) $total_price += $price; } echo $total_price; Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-844178 Share on other sites More sharing options...
486974 Posted May 28, 2009 Author Share Posted May 28, 2009 Thank you very much for the help Ken works a treat Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-844416 Share on other sites More sharing options...
sasa Posted May 28, 2009 Share Posted May 28, 2009 Ken's code just sum first 3 value in array try to change value for Jan 1st and you can see try $date = 'January 03'; $days = 3; $start = false; $total_price = 0; foreach ($array as $key => $price) { if ($days === 0) break; if ($key === $date || $start){ $total_price += $price; $start = true; $days--; } } echo $total_price; Quote Link to comment https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/#findComment-844470 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.