Jump to content

Recommended Posts

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 />";

?>

Link to comment
https://forums.phpfreaks.com/topic/159948-something-simple-for-those-who-know/
Share on other sites

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,

 

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;

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 

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

?>

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;

 

?>

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;

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;

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.